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
Post a Comment