c# - Moving away from Repository pattern. How should I use DBContext? -


on mvc / entity framework project moving away repository pattern.

first, has given me few problemas ... , think dbcontext , idbset kind of implement unitofwork , repository.

so starting use commands , queries. here example:

public class listpostsquery {    public listpostsquery() {   }    public list<post> execute(int currentpage, int pagesize) {    } } 

how should integrate or inject dbcontext in queries / commands?

maybe using wrapper dbcontext save method , exposing sets?

or should create new context in execute method?

  public list<post> execute(int currentpage, int pagesize) {      using (context = new dbcontext) {     }    } 

could someone, please, advice me on this?

use constructor injection, i.e. pass context service classes via constructor.

public class listpostsquery {    private dbcontext ctx;    public listpostsquery( dbcontext ctx ) {      this.ctx = ctx;   }    public list<post> execute(int currentpage, int pagesize) {       return ctx....    } } 

this way control lifetime of context once, e.g. in controller factory or using ioc framework of choice.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -