c# - applying unit of work in a generic repository -
i'm learning on doing repository unit of work. know how di/ioc. problem can't figure out apply unit of work
in code. below generic repository.
public abstract class repository<t, c> : //idisposable, irepository<t> t : class c : dbcontext, new() { private c entities = new c(); public c context { { return this.entities; } set { this.entities = value; } } public virtual void insert(t entity) { this.entities.set<t>().add(entity); } // remove code brevity }
what had tried far:
make
unit of work
classpublic class unitofwork : iunitofwork { private readonly foocontext _dbcontext; public unitofwork() { _dbcontext = new datacontext; } public void save() { _dbcontext.savechanges(); } // dispose method }
in service:
public class productservice : repository<product, foocontext>, iproductservice { private readonly iproductrepository _prodrepo; private readonly iunitofwork _uow; public productservice(iunitofwork uow, iproductrepository prodrepo) { _uow = uow; _prodrepo = prodrepo; } public override void insert(item entity) { base.insert(entity); save(); } public void save() { uow.save(); } // remove code brevity }
there's no error when build it. , when try apply in controller
doesn't give me error. when try run , debug it, in intellitrace, doesn't give me insert
statement , again, not give me error.. have gone wrong?
any appreciated. thanks!
we saw should separate service repository.
here, problem seems come fact use 2 different dbcontext instances. 1 in repository , 1 in unitofwork.
instead, should inject same dbcontext instance in repository , unitofwork.
edit: should write different layers this:
public class productservice { private readonly iproductrepository productrepository; private readonly iunitofwork unitofwork; public productservice(iproductrepository productrepository, iunitofwork unitofwork) { this.productrepository = productrepository; this.unitofwork = unitofwork; } public ienumerable<product> getcurrentproductsonorderforcustomer(int customerid) { // etc. } }
the controller layer should this:
public class productcontroller : controller { private readonly iproductservice prodservice; public productcontroller(iproductservice prodservice) { this.prodservice = prodservice; } }
here corrected unitofwork:
public class unitofwork : iunitofwork { private readonly idbcontext _dbcontext; public unitofwork(idbcontext dbcontext) { _dbcontext = dbcontext; } public void save() { _dbcontext.savechanges(); } // dispose method }
here example of repository
public class productrepository { private readonly idbcontext _context; public productrepository(idbcontext dbcontext) { this._context = context; } public virtual void insert(t entity) { this._context.products.add(entity); } // remove code brevity }
and create context class inherits dbcontext, , inject in uow , repos.
public class myapplicationcontext : dbcontext { public myapplicationcontext(string connectionstring) { // configure context want here } public dbset<product> products { get; set; } }
Comments
Post a Comment