Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>The "Quick" Answer</strong></p> <p>I would stick with interfaces. They are <em>designed</em> to be contracts for consumption for external entities.</p> <p>@JakubKonecki mentioned multiple inheritance. I think this is the biggest reason to stick with interfaces as it will become very apparent on the consumer side if you force them to take a base class... no one likes base classes being thrust upon them.</p> <p><strong>The Updated "Quick" Answer</strong></p> <p>You have stated issues with interface implementations outside your control. A good approach is to simply create a new interface inheriting from the old one and fix your own implementation. You can then notify the other teams that a new interface is available. Over time, you can deprecate older interfaces.</p> <p>Don't forget you can use the support of <a href="http://msdn.microsoft.com/en-us/library/ms173157%28v=vs.100%29.aspx">explicit interface implementations</a> to help maintain a nice divide between interfaces that are logically the same, but of different versions.</p> <p>If you want all this to fit in with DI, then try not to define new interfaces and instead favour additions. Alternatively to limit client code changes, try to inherit new interfaces from old ones.</p> <p><strong>Implementation vs. Consumption</strong></p> <p>There is a difference between <em>implementing</em> the interface and <em>consuming</em> it. Adding a method breaks the implementation(s), but does not break the consumer.</p> <p>Removing a method obviously breaks the consumer, but does not break the implementation - however you wouldn't do this if you are backwards-compatibility conscious for your consumers.</p> <p><strong>My Experiences</strong></p> <p>We frequently have a 1-to-1 relationship with interfaces. It is largely a formality but you occasionally get nice instances where interfaces are useful because we stub / mock test implementations, or we actually provide client-specific implementations. The fact that this frequently breaks that one implementation if we happen to change the interface isn't a code smell, in my opinion, it is simply how you work against interfaces.</p> <p>Our interface-based approach is now standing us in good stead as we utilise techniques such as the factory pattern and elements of DI to improve an aged legacy code base. Testing was able to quickly take advantage of the fact that interfaces existed in the code base for many years before finding a "definitive" use (ie, not just 1-1 mappings with concrete classes).</p> <p><strong>Base Class Cons</strong></p> <p>Base classes are for sharing implementation details to common entities, the fact they are able to do something similar with sharing an API publicly is a by-product in my opinion. Interfaces are designed to share API publicly, so use them.</p> <p>With base classes you could also potentially get leakage of implementation details, for example if you need to make something public for another part of the implementation to use. These are no conducive to maintaining a clean public API.</p> <p><strong>Breaking / Supporting Implementations</strong></p> <p>If you go down the interface route you may run into difficulty changing even the interface due to breaking contracts. Also, as you mention, you could break implementations outside of your control. There are two ways to tackle this problem:</p> <ol> <li>State that you won't break consumers, but you won't support implementations.</li> <li>State that once an interface is published, it is never changed.</li> </ol> <p>I have witnessed the latter, I see it come in two guises:</p> <ol> <li>Completely separate interfaces for any new stuff: <code>MyInterfaceV1</code>, <code>MyInterfaceV2</code>.</li> <li>Interface inheritance: <code>MyInterfaceV2 : MyInterfaceV1</code>.</li> </ol> <p>I personally wouldn't <em>choose</em> to go down this route, I would choose to not support implementations from breaking changes. But sometimes we don't have this choice.</p> <p><strong>Some Code</strong></p> <pre><code>public interface IGetNames { List&lt;string&gt; GetNames(); } // One option is to redefine the entire interface and use // explicit interface implementations in your concrete classes. public interface IGetMoreNames { List&lt;string&gt; GetNames(); List&lt;string&gt; GetMoreNames(); } // Another option is to inherit. public interface IGetMoreNames : IGetNames { List&lt;string&gt; GetMoreNames(); } // A final option is to only define new stuff. public interface IGetMoreNames { List&lt;string&gt; GetMoreNames(); } </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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