I have an Interface. Due to a new requirement, the interface now has a single method that could be implemented.
public static string ToEntityConnectionString(this IEnvironmentProvider provider)
//Converts a standard connection string to EntityFramework compatible string
I could convert the interface to an Abstract class, albeit I’m now restricting myself to this class as the inheriting class. Although, this interface is currently used for Dependency Injection alone.
After some thought, I realized I could append the functionality to the interface via an Extension method. It seems to good to be true. Adding implementation to an interface? Is it considered a bad practice to do such a thing? If so, why? Considering the circumstances, what should be used instead?
When you have an extension method on an interface you shouldn’t think of it as adding an implementation to that interface. It is just a static helper method that takes an interface, and does all of it’s work based on the Interface’s exposed members. This is something you see quite a lot of in OO design; it’s not contrary to OO principles. The ‘extension’ part just makes it look prettier and makes it easier to find/type out. If you start to actually think of the extension method as a part of the interface you’ll have bigger problems, because it really isn’t.