Since, an interface doesn’t encompass a static method, is there any way to make a static method mandatory to be implemented in the implemented/derived class?
If not, is there any other way to achieve this objective?
I am actually making all my database classes look like this:
class MyClass : IMyClass, IPersistant
{
public int ID {get;set}
.....
.....
public int SaveOrUpdate(){}//returns the ID
public static MyClass Get(int id){}
public static IEnumerable<MyClass> Get(){}
public bool Delete(){}
}
No, there isn’t. Static methods aren’t polymorphic.
There’s currently no good way of doing this, really. You can have parallel type hierarchies – one for instances and one sort of “meta” hierarchy with instance methods where you’d otherwise have static ones, but that gets pretty ugly too.
If you could say more about what you’re trying to do, we may be able to come up with alternative suggestions.
EDIT: It looks like you really want a
Repository<T>parallel hierarchy for fetching individual items or collections.