.net - DAL architecture using generic domain models -
currently i'm facing problem, try design dal .net application, later use sort of nosql database.
nosql databases evaluated differ in type use primary key. example, mongodb uses proprietary objectid while ravendb uses common string. question is, how design such dal can handle different types of domain model id's.
my first approach make generic domain models. looked this:
// domain model public class filter<tid> { public tid id { get; set; } // ... } // dao-interface public interface ifilterdao<tid> { bool persist(filter<tid>); // ... } // problems begin public static class daofactory { public static ifilterdao<t> getfilterdao<t>() { // implementation of ifilterdao<t> can't instantiated because types must known @ compile time } } the comment in dao-factory method describes problem: can't define t @ runtime, because corresponding code generated @ compile time.
my second , third approach define id either object or dynamic. ravendb couldn't work object, because needs string. using dynamic couldn't pass lambdas ravendb api (compiler error "an expression tree may not contain dynamic operation" occured). , building expression trees instead of using lambdas last way out because it's way more time-consuming.
so i'm totally stuck , hope can me out. in advance.
update: got ravendb work object, it's not satisfying solution use object.
you solve problem in static class if knew type @ compile time, correct? means that, necessarily, need 1 daofactory implementation every underlying database.
the answer shouldn't using factory pattern - instead, should using abstract factory pattern.
public class filter<tid> { public tid id { get; set; } } public interface ifilterdao<tid> { bool persist(filter<tid>); } // note: can't static since polymorphism requires instance! public abstract class daofactory<tid> { public abstract ifilterdao<tid> getfilterdao<tid>(); } public sealed class mongodbdaofactory : daofactory<objectid> { public override ifilterdao<objectid> getfilterdao<objectid>() { /* ... */ } } public sealed class ravendbdaofactory : daofactory<string> { public override ifilterdao<string> getfilterdao<string>() { /* ... */ } } i've had similar things , tried using dependency injection pattern selecting appropriate implementation use @ runtime, that's hard because of generics issue. abstract factory best way go here.
Comments
Post a Comment