c# - Using DbContext as a Repository -
c# - Using DbContext as a Repository -
currently working on project in read, process , store products. using entity framework 6 read , write mysql database.
after building prototype , fetching statistics, found storing new products in database takes (relatively) much time. have been asked improve this, can't figure out best alternative is.
currently, every read , write happens in using
block. since first time using entity framework 6, did research , vast bulk of stackoverflow said should always utilize using
block. did.
code snippet of how looks now;
public int getsomeid(string somestringtomatchwith) { using (var db = new mydbcontext()) { homecoming db.sometable.where(t => t.somestring == somestringtomatchwith).firstordefault().id; } } public void savesomedata(int someid) { using(var db = new mydbcontext()) { db.sometable.add(new sometable{ id = someid }); db.savechanges(); } }
i have been told mysql work faster if exposed mass info rather single info inserts. also, after reading this question, figured might improve programme not write info database (and therefore, not utilize using
), create simple repository
saves info , write database after amount of time. since willing acccess repository
through multiple threads, figured singleton design satisfy.
there 1 nasty requirement though; products have matched values, , could happen product #2 has match product #1. in other words, always need able access recent data.
something came mind;
public class repository { private static readonly object lock = new object(); private mydbcontext context { get; set; } private repository() { context = new mydbcontext(); } private static repository _instance; public static repository instance { { if (_instance == null) { lock(lock) { if(_instance == null) { _instance = new repository(); } } } homecoming _instance; } } //this method called 1 time in while public void commit() { context.savechanges(); context.dispose(); //get rid of entities context = new mydbcontext(); //create fresh dbcontext } //other read/write methods }
there few questions, actually;
is hard task create whole class thread safe? need add together lock every table so, or there smarter way? would increment performance? linked question above create me think does. since not recommended way , of (probably) disagree; there improve way implement this?please note current version works as-is. programme needs process 2.500.000 products , bottleneck seems writing database. final note, have read implementing repository , unit of work patterns... tells me how it, not give me info why should or should not utilize it.
ef powerful , allows designer of application determine how deep want go! mean? means can either through ef or when needed can interface straight sql... below few examples of this:
//the normal ef pattern is: using(var db = new myentities()){//do here}
the method above favors strong typing way, allows perfect linq integration , produces nicely (perfectly?) formed queries.
//however can utilize ef nail sql directly. using(var db= new myentiteis()){ var stuff = db.database<passinstrongtype>(query,parms)
the query parameter string, parameter placeholders this.
"select * table field== @parm1" //works inserts, updates , deletes too.
the sec value sqlparameter array this:
var parms = new list<sqlparameter>() parms.add(new sqlparameter { name="parm1", value = "myvalue" }).toarray();
now beautify of hybrid solution can absolute fastest sql response because hitting sql directly, pick ability have results returned using strongly-typed models.
any type of query allowed, updates, inserts, deletes...
this hybrid approach get's closest sql layer whild still staying within ef. collection returned still allows linq. me best of 2 worlds.
c# entity-framework
Comments
Post a Comment