1

I'm relatively new to C# so this may be a somewhat naive question.

Does there exist a way, or can one even be constructed, to construct an interface containing all the public methods/properties of a class?

I find myself in a project using the mocking framework Moq. Moq has an apparently rather common limitation in that it can only handle interfaces and virtual methods. The project's architect has decided to go the interface route, which means every class in the project has an accompanying interface. This means there are loads of interfaces implemented by a single class. Furthermore, the style mandates that interfaces go into their own files. This means there are loads of files in the project.

In my opinion it would be a real improvement if these interface-and-files-just-for-Moq could be a bit less intrusive. Is there no way to have the system (Visual Studio/.Net/C#) create them.

For instance, if writing this

[ExtractAndImplement("IFoo")]
public class Foo
{
    public int Bar(int baz)
    {
        ...
    }
}

would be equivalent to

public interface IFoo
{
    int Bar(int baz);
}

public class Foo : IFoo
{
    public int Bar(int baz)
    {
        ...
    }
}

NB No, Refactor -> Extract Interface does not do what I want. First off, it creates an interface in source code somewhere, so it doesn't reduce the clutter of singly-implemented interfaces. Second, it's an interface I need to maintain explicitly; when I add a public method in the class I need to extract that new method to the correct interface. No, I'd like to have something that's implicit, i.e. interfaces are created on the fly without cluttering the source or the project.

I'm guessing that in Lisp/Scheme it'd be done using macros, and in Haskell using templates.

Magnus
  • 4,644
  • 1
  • 33
  • 49
  • 1
    VS does have the `Refactor > Extract Interface` command. Is that what you're after? – Dai Apr 07 '14 at 08:14
  • There are several visual studio add on's that will do this for you. [Code rush express being one](https://documentation.devexpress.com/#CodeRush/CustomDocument2892) – Liam Apr 07 '14 at 08:15
  • On a side note, the reason MOQ only lets your mock interfaces or abstract classes is that it needs to build a class on the fly that overrides functions of the mocked class. It can only do this for non-concrete classes. – Liam Apr 07 '14 at 08:20
  • 1
    [Are Using Interfaces Soley to Facilitate Stubing and Mocking in Unit Tests Now Obsolete?](http://stackoverflow.com/q/3869002/706456) and similar questions might be useful. If you just use interfaces for mocking, perhaps you can think about TDD and state testing, in contrast to BDD and behaviour testing. – oleksii Apr 07 '14 at 08:26
  • I was considering adding a note about Refactor -> Extract Interface *not* being what I'm after, but I decided against it since since I had trouble wording it in a nice way. I guess that ought to teach me. – Magnus Apr 07 '14 at 14:11

2 Answers2

1

You can do this in Visual Studio (not in the express version). Use Refactor -> Extract Interface. The cursor needs to be placed on the classname.

For more information:

http://msdn.microsoft.com/en-us/library/fb3dyx26.aspx

You could also look at ReSharper for this option or SharpDevelop.

Ronald Meijboom
  • 1,534
  • 3
  • 17
  • 37
1

You are probably asking for

The interface language is in Italian (It says "Extract Interface"), sorry, but you got a hint I hope.

enter image description here

Community
  • 1
  • 1
Tigran
  • 61,654
  • 8
  • 86
  • 123
  • Is this part of core VS? I couldn't remember if one of my add on's did this or not? – Liam Apr 07 '14 at 08:17
  • 1
    @Liam: yes, it's part of VS core functionality, but I'm afraid it's not present in *express* versions. – Tigran Apr 07 '14 at 08:18