I’m using dependency injection and have the following interface:
public interface IAchievement : IEquatable<IAchievement>
{
string GetName();
string GetDescription();
}
I have the IEquatable for I can define the Equals method (for a list.contains() works properly). Problem is, the Equals method is exactly the same for all classes implementing this interface. Is there a way to define it for the interface using extension methods? I could stick an ‘abstract’ class in the hierarchy between the implementing classes and the interface, but fear that would defeat the purpose of dependency injection.
You could use an extension method to provide a common implementation for all of your classes, but each would still need to implement Equals() themselves and then call the extension method. An extension method can’t be used to satisfy an interface.
Using an abstract class doesn’t contradict DI. You would still pass the interface around. It would just be all the concrete classes would have a common base.