entity framework - ASP.NET MVC 5 EF 6 - Just buiness logic classes vs repository and unit of work UPDATED -


over past few months i've been learning mvc5 ef6. it's been love/hate affair, not framework itself, microsoft's online tutorials. not all, majority of tutorials seem end final season of television series. viewers @ home scratching heads trying figure out rest of story, or reason why started watching in first place.

anyway, latest stumbling block whether or not should implement repository pattern , unit of work ef6? reason interested in sort of additional layer reduce amount of logic code in controllers.

before considering repository, had simple public class in models folder called productservice.cs allowed me pass product model controller perform data manipulation , return view saved. worked flawlessly point of view, calling additional dbcontext in service class seemed incorrect, think.

after research online, started implement repository allow me perform same data manipulation in productservice.cs, seemed follow established pattern , allow context passed controller depository can perform manipulation before save. it's more code write considering ef save, update, , remove working fine, don't mind writing more code if put me in better place moving forward.

now question how lists dropdowns in controller? quick solution place get() in productdepository each list. worked, seems i'm writing more code need to. see advantage writing individual repositories each model, since may have different store , retrieve methods down road. correct solution using repository create unit of work references each repository needed controller? here's of code

the product repository interface:

public interface iproductrepository : idisposable {     ienumerable<category> getcategories();           ienumerable<manufacturer> getmanufacturers();           ienumerable<producttype> getproducttypes();            ienumerable<availability> getavailabilities();     ienumerable<shipmethod> getshipmethods();            ienumerable<product> getproducts();     ienumerable<product> getproductsbyname(string productname);     product getproductbyid(int productid);     void insertproduct(product product);     void deleteproduct(int productid);     void updateproduct(product product);     void save(); } 

the top portion of contoller:

public class productcontroller : controller { private iproductrepository productrepository;      public productcontroller()     {         this.productrepository = new productrepository(new productcontext())     }      public productcontroller(iproductrepository productrepository)     {         this.productrepository = productrepository;     }      public actionresult create()     {         product product = new product();         product.created = datetime.now;         viewbag.availabilityid = new selectlist(productrepository.getavailabilities(), "availabilityid", "name");         viewbag.categoryid = new selectlist(productrepository.getcategories(), "categoryid", "name");         viewbag.manufacturerid = new selectlist(productrepository.getmanufacturers(), "manufacturerid", "name");         viewbag.producttypeid = new selectlist(productrepository.getproducttypes(), "producttypeid", "name");         viewbag.shipmethodid = new selectlist(productrepository.getshipmethods(), "shipmethodid", "name");         return view(product);     } 

after spending time figure out final solution, keep coming same question. why can't turn iproductrepository , repository iproductservice , productservice?

basically keep crud in controller , call service if it's needed can passed controller final storage or presentation? what's real point of creating bunch of crud methods each entity if ef doing me in controller?

thank in advance help. little confused.

update - wanted show example of original controller , service idea. understand logic simple , in controller, of other services cropping , saving both thumbnail , original image while creating new product take lot of space in controller.

the block controller:

public class producttestercontroller : controller {     private productcontext db = new productcontext();     private productservice service = new productservice();      // get: dashboard/productmanager/details/5     public actionresult detail(int id)     {         product product = db.products.find(id);         if (product == null)         {             return httpnotfound();         }         return view(product);     }      [httppost]     [validateantiforgerytoken]     public actionresult edit(product product)     {         if (modelstate.isvalid)         {             service.updatepid(product, db);             db.entry(product).state = entitystate.modified;             db.savechanges();             return redirecttoaction("detail", new { id = product.productid });         }         viewbag.availabilityid = new selectlist(db.availability, "availabilityid", "name", product.availabilityid);         viewbag.categoryid = new selectlist(db.categories, "categoryid", "name", product.categoryid);         viewbag.manufacturerid = new selectlist(db.manufacturers, "manufacturerid", "name", product.manufacturerid);         viewbag.producttypeid = new selectlist(db.producttypes, "producttypeid", "name", product.producttypeid);         viewbag.shipmethodid = new selectlist(db.shipmethods, "shipmethodid", "name", product.shipmethodid);         return view(product);     } 

and service:

public class productservice {     public product createpid(product product, productcontext context)     {         int lastproductid = context.products.orderbydescending(x => x.productid).first().productid;         product.pid = convert.toint32("" + (100 * product.categoryid) + (lastproductid + 1));         return (product);     }      public product updatepid(product product, productcontext context)     {         product.pid = convert.toint32("" + (100 * product.categoryid) + product.productid);         return (product);     }  } 

i don't create new context inside service, pass context controller , return need context. size of project , lack of real repository/uow experience seems suite situation. mean whole reason service class reduce size of controller , keep simple. there negative effect of doing rather creating repository/uow/service? thanks!

update

after spending alot of time researching stumbled upon blog seems looking for. since post have started using autofac inject context constructors. works charm.

here's link seeking similar solution - making entity framework more unit testable - josh kodroff

i use genericrepository class unitofwork creates instance of genericrepository each model linking through entityframework database. see here: http://www.codeproject.com/articles/825646/generic-repository-and-unitofwork-patterns-in-mvc

here simple example of how have used in past. it's worth noting more complex sql needs use orm dapper awesome! :

dbentities.cs

public class dbentities : dbcontext {     public dbset<product> products{ get; set; }       protected override void onmodelcreating(dbmodelbuilder modelbuilder)     {         modelbuilder.conventions.remove<pluralizingtablenameconvention>();         modelbuilder.entity<product>().haskey(x => x.id);     } } 

genericrepository.cs

public class genericrepository<tentity> tentity :class {     internal dbentities _db;     internal dbset<tentity> dbset;      public genericrepository(dbentities _db)     {         this._db = _db;         this.dbset = _db.set<tentity>();     }      public virtual ienumerable<tentity> get(         expression<func<tentity, bool>> filter = null,         func<iqueryable<tentity>, iorderedqueryable<tentity>> orderby = null,         string includeproperties = "")     {         iqueryable<tentity> query = dbset;          if (filter != null)         {             query = query.where(filter);         }          foreach (var includeproperty in includeproperties.split             (new char[] { ',' }, stringsplitoptions.removeemptyentries))         {             query = query.include(includeproperty);         }          if (orderby != null)         {             return orderby(query);         }         else         {             return query;         }     }      public virtual tentity getbyid(object id)     {         return dbset.find(id);     }      public virtual void insert(tentity entity)     {         dbset.add(entity);     }      public virtual void delete(object id)     {         tentity entitytodelete = dbset.find(id);         delete(entitytodelete);     }      public virtual void delete(tentity entitytodelete)     {         if (_db.entry(entitytodelete).state == entitystate.detached)         {             dbset.attach(entitytodelete);         }         dbset.remove(entitytodelete);     }      public virtual void update(tentity entitytoupdate)     {         dbset.attach(entitytoupdate);         _db.entry(entitytoupdate).state = entitystate.modified;     }  } 

unitofwork.cs

public class unitofwork :idisposable {     private dbentities _db = new dbentities();      private genericrepository<product> productrepository;       public genericrepository<product> productrepository     {                 {             if (this.productrepository == null)             {                 this.productrepository = new genericrepository<product>(_db);             }             return productrepository;         }     }      public void save()     {         _db.savechanges();     }      private bool disposed = false;      protected virtual void dispose(bool disposing)     {         if (!this.disposed)         {             if (disposing)             {                 _db.dispose();             }         }         this.disposed = true;     }      public void dispose()     {         dispose(true);         gc.suppressfinalize(this);     }  } 

a new entry added each model in unitofwork.cs , dbentities.cs files. model class corresponds structurally db table.

simple usage example

var _uow = new unitofwork();  //get products on £100 var lst = _uow.productrepository.get(n=>!n.price>100); _uow.dispose(); 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -