The great God does not spray, the small God silently learns.
Will be a trivial thing, will not be absolutely tall on.
Finally upload the source code. Hope to bring some new understanding and knowledge to the reader.
Haven't been on the headlines yet. Ladies and gentlemen, please support my little brother.
Updated continuously. Update until you will!!
I am not talking, I only focus on the point out, the details can not understand the message to discuss. In the end of this series, I will unify the questions of everyone, listed in a new blog. Work needs to be planned, and so is blogging.
Demand
① Please keep the EF context thread unique. Prevent dirty temporary data from appearing
② Please design the program extensibility. After the EF framework may be changed to spring framework
③ when the service layer calls the storage layer. DbContext for Unified Entrance
③.1 facilitates programmer development and provides a unified portal. Wrap the EF into this portal.
③.2 prevent the appearance of dirty temporary data, "also" please keep this portal within the thread unique.
③.3 want this uniform entrance. To remove the coupling between the service layer and the storage layer
Ideas
① put EF in CallContext. Key-value pairs are stored in the form.
② interface-oriented thinking
Create a new factory interface + factory. Use a "factory method" mode, not "simple factory", nor "abstract factory" design pattern. If you do not know a few design patterns of the factory, please refer to the design mode I have written a blog.
③③.1 creates a new class, named Dbcontact, which has a property type of ObjectContext, because context inherits ObjectContext, and then "points" multiple warehouses.
③.2 put into the callcontext inside. Key-value pairs are stored.
③.3 Create a new idbcontact interface, let dbcontact inherit. Inherit an interface factory created with a factory. "Factory method" design pattern
thought
Interface + Factory mode + thread unique 2 abstract methods factory function: Thread unique + extensibility (interface oriented)
Detail code
ISingleEFCntextFactory.cs
using System.Data.Objects; namespace publicinterface isingleefcntextfactory {//no public ObjectContext Getcurrentefcontext (); }}
SingleEFCntextFactory.cs
usingLKBS.CommunicationPlatform.IDAL;usingSystem.Data.Objects;//Add Reference System.Data.EntityusingSystem.Runtime.Remoting.Messaging;usingLKBS.CommunicationPlatform.Model;namespacelkbs.communicationplatform.factory{ Public classSingleefcntextfactory:isingleefcntextfactory { PublicObjectContext Getcurrentefcontext () {//Place the context (the base class) inside a thread. Let a single request (an action) execute a few steps in the crud, a common context.ObjectContext Callefcontext = (ObjectContext) callcontext.getdata ("Efcontext");//thread slots Hold "key-value pairs"//Read and write first. No, just a new context if(Callefcontext = =NULL) {Callefcontext=NewModelcontainer (); } callcontext.setdata ("Efcontext", Callefcontext); returnCallefcontext; } }}
BaseRespository.cs
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Data.Entity;usingLKBS.CommunicationPlatform.Model;usingLKBS.CommunicationPlatform.IDAL;usingSystem.Data;usingLKBS.CommunicationPlatform.Factory;usingSystem.Data.Objects;namespacelkbs.communicationplatform.dal{ Public classBaserespository<t>whereR |class,New() {isingleefcntextfactory singleeffactory=Newsingleefcntextfactory (); //Add a factory in the middle, where "remove" the coupling of EF and specific warehousing. You can change EF. and Respositiry DB does not changeObjectContext DB;//if it is not assigned, that is the question of assignment. To find the corresponding assignment of the factory or factory has not been compiled Publicbaserespository () {ObjectContext db=Singleeffactory.getcurrentefcontext (); } //Modelcontainer db = Effactory.getcurrentefcontext (); //The field initializer cannot reference a non-static field, method, or property. //so we're assigning values within the constructor method ..... CRUD}}
To facilitate the invocation of the business logic layer, establish a unified portal.
IDBContact.cs
using System.Data.Objects; // namespace lkbs. communicationplatform.idal{ public Interface Idbcontact { // The properties in the interface are {} ObjectContext currentefcontext {get ; set ;} Iuserinforrespository userinforresponsity { get ; set // ... Warehousing of multiple other tables, add OK
DBContact.cs
usingLKBS.CommunicationPlatform.IDAL;usingSystem.Data.Objects;//the namespace of the contextusingLKBS.CommunicationPlatform.Factory;namespacelkbs.communicationplatform.dal{ Public classDbcontact:idbcontact { PublicObjectContext Currentefcontext {Get;Set; } Isingleefcntextfactory singleefcntextfactory=NewSingleefcntextfactory ();//Singleefcntextfactory do a single context Publicdbcontact () {Currentefcontext= Singleefcntextfactory.getcurrentefcontext ();// } PublicIuserinforrespository userinforresponsity {Get;Set; } }}
ISingleDBContactFactory.cs
namespace lkbs.communicationplatform.idal{ publicinterface isingledbcontactfactory { Idbcontact Createdbcotactuseef (); }}
SingleDBContactFactory.cs
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingLKBS.CommunicationPlatform.IDAL;usingSystem.Data.Objects;usingSystem.Runtime.Remoting.Messaging;usingLKBS.CommunicationPlatform.DAL;namespacelkbs.communicationplatform.factory{//inherit interface ① interface to compile, ② the assembly used to introduce the interface + using classSingledbcontactfactory:isingledbcontactfactory { Publicidbcontact Createdbcotactuseef () {//ObjectContext currentefcontext = new ObjectContext ("Currentefcontext"); //return currentefcontext;Idbcontact dbcotact = (dbcontact) callcontext.getdata ("currentdbcontact"); if(dbcotact==NULL) {dbcotact=Newdbcontact (); } callcontext.setdata ("currentdbcontact", dbcotact); returndbcotact; } }}
ASP. NET MVC Series Framework (ii) Optimization of storage layer