.NET Tools: Extract Interface and Implement Wrapper Class

c#, interface, unit-testing, wrapper

Solution

Found a way around it for non-sealed classes.

1 - Inherit from the external class

class MyWrapper : ExternalClass

2 - Extract interface for all public methods

class MyWrapper : ExternalClass, IExternalClass

3 - Remove the inheritance from the external class

class MyWrapper : IExternalClass

4 - You will get a hint on the class name about members from the interface not being implemented. Alt + Enter on it and let Resharper automatically implement them

5 - Use this code template to wrap properties

    get { return $INNERCOMPONENT$.$NAME$; }
    set { $INNERCOMPONENT$.$NAME$ = value; }

6 - Use this code template to wrap methods

return $INNERCOMPONENT$.$NAME$($SIGNATURE$);

Problem

Is there a tool that can generate extract and generate interfaces for existing classes? I know Visual Studio will extract an Interface for an existing class. However, I would also like to generate a wrapper class that implements that functionality. I believe this would help tremendously for unit testing. Example Existing Class: ``` public class ThirdPartyClass { public void Method1(){} public void Method2(){} } ``` This can be generated by Visual Studio (Extract Interface): ``` public interface IThirdPartyClass { void Method1(); void Method2(); } ``` I would like to go one step further: ``` public class ThirdPartyClassWrapper : IThirdPartyClass { private tpc = new ThirdPartyClass(); public void Method1() { tpc.Method1(); } public void Method2() { tpc.Method2(); } } ``` Update: This would be especially useful for static classes. As Morten points out I can just use a stub, however, I would like to break up my coupling if possible.

Original source

Related problems