C# recursive generics in generics -
i playing ddd time , have found ayende's base class construction entity. construct base assembly necessary objects. came (simplified without implementation):
public abstract class baseentity<t, tkey> t : baseentity<t, tkey> { } public interface iaggregateroot { } public interface iunitofwork { void commit(); } // open changes if public interface iwriteunitofwork : iunitofwork { void insert<taggregateroot, t, tkey>(taggregateroot aggregateroot) taggregateroot : baseentity<t, tkey>, iaggregateroot t : baseentity<t, tkey>; }
everything fine untill use constructions in project. specifics of project simplified without implementation:
public abstract class entity : baseentity<entity, guid> { } public class customer : entity, iaggregateroot { } public abstract class context : iwriteunitofwork { public void insert<taggregateroot, t, tkey>(taggregateroot aggregateroot) taggregateroot : baseentity<t, tkey>, iaggregateroot t : baseentity<t, tkey> { } public void commit() { } } public class mycontext : context { } public class examplethatworks { public void something() { iwriteunitofwork mycontext = new mycontext(); var customer = new customer(); // works mycontext.insert<customer, entity, guid>(customer); } } public class examplethatiwant { public void something() { iwriteunitofwork mycontext = new mycontext(); var customer = new customer(); // doesn't work mycontext.insert(customer); } }
my problem insert method iwriteunitofwork interface. written don't have write generic stuff every time want use in examplethatiwant. yet, wouldn't change base assembly if there isn't reason it.
is possible , how? open other approaches well.
thanks.
would work you?
public abstract class baseentity<t, tkey> t : baseentity<t, tkey> { } public interface iaggregateroot { } public interface iunitofwork { void commit(); } public interface iwriteunitofwork : iunitofwork { void insert<t>(t aggregateroot) t : baseentity<t, guid>, iaggregateroot; } public abstract class entity<t, tkey> : baseentity<t, tkey> t : baseentity<t, tkey> { } public class customer : entity<customer, guid>, iaggregateroot { } public abstract class context : iwriteunitofwork { public void insert<t>(t aggregateroot) t : baseentity<t, guid>, iaggregateroot { } public void commit() { } } public class mycontext : context { } public class examplethatiwant { public void something() { iwriteunitofwork mycontext = new mycontext(); var customer = new customer(); mycontext.insert(customer); } }
Comments
Post a Comment