c# - Where to put the method. Service layer ( BL ) over repository? -


i'm confused 1 thing. i've used repository pattern (not generic) in previous mvc apps , used include kind of business logic there. @ moment read service layer pattern should included bl. don't know if there more abstraction , code instead of cleary/readable , efficient code.

i want implement method

public void changeactivefield(bool isactive, int id) {   var objecttoupdate = _context.firstordefault(x=>x.id==id);   objecttoupdate.isactive - isactive;   _context.entry(objecttoupdate).state = system.data.entity.entitystate.modified;   _context.save(); } 

in code there bit of business logic change state of 1 field , after update this. should make in service layer , use simple repository update method liek this: ?

public class myservice {   private readonly imyrepository = _myrepo;    myservice(imyrepository myrepo) //it's injectable   {     _myrepo = myrepo;   }    public void changeactivefield(bool isactive, int id)    {      var myobject = _myrepo.getmyobject(id);      myobject.isactive = isactive;     _myrepo.update(myobject);   }  } 

is better aproach? make better separation? or it's sophisticated , overwrite? thank help. best regards.

in general repository should encapsulate only logic of accessing database (initialization of context, transactions, connections , etc.). common create generic crud repository , reuse business entities. business related logic should placed in business layer(service layer). major benefits of approach are:

  1. testability - can test business logic without relying on concrete repository implementation injecting fake repositories(stubs).
  2. decoupling - neither business layer nor ui not coupled specific database, if tomorrow decide migrate database example sql server redis(nosql database), changes contained on repository layer only.
  3. maintainability - there clear separation of concerns each layer: ui - interaction user, bl - implementation of business logic, repository - interaction db.

in experience having business layer (no matter how simple @ beginning - it's grows project) idea.

by way developers consider repository unnecessary layer of abstraction in case use ef (in way ef database context repository...).

one thing i've learned, major effort not development phase of project it's maintenance , upgrades - , here having business layer significant impact.

disclaimer: subjective opinion based on experience


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -