architecture - How to avoid circular data-access object dependencies when you need to do lazy loading (and using an IOC Container)? -


note: examples below c# problem should not specific language in particular.

so building object domain using variant of s# architecture. unfamiliar it, , save reading time idea have data access object interface each of domain objects responsible loading to/from persistence layer. might ever need load/save given object accepts object's data access interface dependency. example can have following product lazy load customer purchased needed:

public class product {   private icustomerdao _customerdao;   private customer _customer;   public product(icustomerdao customerdao) {_customerdao = customerdao;}   public int productid {get; set;}   public int customerid {get; set;}   public customer customer {         get{             if(_customer == null) _customer = _customerdao.getbyid(customerid);             return _customer;         } } public interface icustomerdao {    public customer getbyid(int id); } 

this , until reach situation 2 objects need able load each other. example many-to-one relationship where, above, product needs able lazy load customer, customer needs able list of products.

public class customer {   private iproductdao _productdao;   private product[] _products;   public customer(iproductdao  productdao) {_productdao = productdao;}   public int customerid {get; set;}   public product[] products {         get{             if(_products == null) _products = _productdao. getallforcustomer(this);             return _products;         } }   public interface iproductdao {    public product[] getallforcustomer(customer customer); } 

i know common situation relatively new @ this. stumbling block when implementing data access objects. because customer has dependency on iproductdao, customerdao implementation must also, vice versa true , productdao must take dependency on icustomerdao.

public class customerdao : icustomerdao {       private iproductdao _productdao;       public customerdao(iproductdao  productdao) {_productdao = productdao;}       public customer getbyid(int id) {           customer c = new customer(_customerdao);           // query database , fill out customerid           return c;       }  } public class productdao : iproductdao {       private icustomerdao _customerdao;       public productdao (icustomerdao customerdao) {_customerdao = customerdao;}       public product[] getallforcustomer(customer customer) {           // idea       }  } 

and here have problem. cannot instantiate customerdao without iproductdao , vice versa. inversion of control container (castle windsor) hits circular dependency , chokes.

i have come for-the-time-being solution involves lazy loading dao objects (i post answer) don't it. time-tested solutions problem?

edit: above simplification of architecture i'm using , not recommending pass daos object. better implementation closer doing similar way nhibernate works actual objects simple , above proxy objects inherit , override appropriate fields.

as other posters have suggested, might want rethink architecture - looks making things hard on yourself.

also note:

by changing data access object dependencies properties rather constructor dependencies windsor autofill them after instantiating each object fully.

be careful. in doing this, you've told windsor these dependencies optional (unlike dependencies injected via constructor). seems bad idea, unless these dependencies are optional. if windsor can't fulfill required dependency, want puke.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -