Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Test-driven development (TDD) is one key reason why you would want to do this. If you have a class that depends directly on your abstract class you cannot test it without writing a subclass that can be instantiated in your unit tests. If, however, your dependent class only depends on an interface then you can provide an 'instance' of this easily using a mocking framework such as Rhino Mocks, NMock, etc.</p> <p>Ultimately I think it's going to be down to how you ship your product. We only ever ship binaries and customers never extend our work. Internally we have interfaces for pretty much everything so classes can be isolated completely for unit testing. This offers huge benefits for refactoring and regression testing!</p> <p><strong>EDIT:</strong> updated with example</p> <p>Consider following code in a unit test:</p> <pre><code>// doesn't work - can't instantiate BaseClass directly var target = new ClassForTesting(new BaseClass()); // where we only rely on interface can easily generate mock in our tests var targetWithInterface = new ClassForTestingWithInterface(MockRepository.GenerateStub&lt;ISomeInterface&gt;()); </code></pre> <p>where the abstract class version is:</p> <pre><code>// dependent class using an abstract class public abstract class BaseClass { public abstract void SomeMethod(); } public class ClassForTesting { public BaseClass SomeMember { get; private set; } public ClassForTesting(BaseClass baseClass) { if (baseClass == null) throw new ArgumentNullException("baseClass"); SomeMember = baseClass; } } </code></pre> <p>and the same stuff but using interface is:</p> <pre><code>public interface ISomeInterface { void SomeMethod(); } public abstract class BaseClassWithInterface : ISomeInterface { public abstract void SomeMethod(); } public class ClassForTestingWithInterface { public ISomeInterface SomeMember { get; private set; } public ClassForTestingWithInterface(ISomeInterface baseClass) {...} } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload