Preface
has been using other people's Open source project framework, the location of the exception will be very troublesome, and even do not know whether the exception from my code or the framework itself. The framework of others is somewhat restrictive, and it may be that I do not have a deep understanding of those frameworks, because these open source frameworks are also hard to find efficient and prescriptive documentation on the web. For example, other people's framework may call the Enterprise Library to achieve the authentication of permissions, but in my project, the permission validation has reusable modules, so in the integration will be very inflexible ....
Referring to a lot of excellent online framework, after reading a few books, suddenly realized that ease of use is the starting point and standpoint of the development and usage framework, the framework is not the more complex the better, evaluation of a framework is good or bad, should consider its learning costs, use costs and maintenance costs. Consider the above factors and then intend to do a set of light-weight framework out to facilitate future expansion, although the framework has been very simple, but there may still be problems, I hope the great God can guide the maze.
Development Background
The biggest problem I have to face is high concurrency, with no particularly complex business logic. This is the reason why I abandoned the original framework.
Because the business logic is not complex, so the use of Dbfirst design ideas, on the one hand, the project follow-up development when others good easier to use, on the one hand, for Codefirst I also just stay in the stage, for the advanced ddd, understanding is very limited, may introduce a lot of wrong way of thinking.
The development environment for the project is: Windows7 SP1, Visual Studio 2013, SQL Server 2012,. NET Framework4.5.
Project backing ASP. NET MVC + WebAPI, data storage uses EntityFramework.
Overall framework
This is the project solution, from top to bottom into the web layer, core layer, model layer and infrastructure layer, the functions are as follows
- Web: Hosting MVC and WEBAPI, responsible for presenting
- Core: Working with specific business logic
- Model: is divided into Entity data entities and Webmodel,webmodel mainly includes the web layer and core layer data transfer model and the entity on the Web page
- Infrastructure: Basic implementation layer, storing some reusable classes and methods
The dependency of the project is this, using AUTOFAC injection. The original use of spring.net, this time instead of a lighter autofac to do the dependency injection, more efficient (Online said (-:), can also find a lot of help documents. and AUTOFAC provides MVC with each request as the injection basis of the scheme, the entity as a parameter in the form of a controller, very practical.
Create a process
In the entity assembly, the EDMX is generated from the database, which also contains the model of the entity object. The next step is to create the entities DbContext entity in the core.
In my original system, I usually create a DAL (Database access layer) on the EF, used to store the common methods of adding and deleting, but then gradually realized that the EF framework itself has been well encapsulated in the traditional DAL layer of the method, if you put a DAL layer there is a repeat encapsulation suspicion. Here is not a detailed discussion of this layer of DAL encapsulation is necessary, because different people's habits will be different, if you are the traditional three layer over, perhaps put a dal will be more handy, of course, there are no advantages, such as business in the operation of the database can be more flexible, coding less layer interface can also appear more intuitive. But without the DAL, the operation of the database can not be encapsulated, resulting in another seemingly tricky issue, if you change the database later, you need to change the core layer of code? This leads to high coupling. I had a long struggle with the problem, but turned around, the Entity framework is an open source framework that has been supported for mainstream databases including Oracle, and the change database only needs to regenerate the edmx of the corresponding database type, which has no impact on the business core.
I created a coresession class in the business layer that is used to hold a collection of business operations to the Web.
Here is the interface, define a common SaveChanges method, as for what to do with, I will say later.
1 Public Interface icoresession 2 {3 int SaveChanges ();
4 }
Here is the implementation, which has a DB attribute, an entity object that holds the instance, and a new instance in the most constructed method.
1 Public classCoreSession:EDUA_ICore.ICoreSession2 {3 PrivateDbContext DB {Get;Set; }4 Publiccoresession ()5 {6db =Newentity.eduaentities ();7 }8 Public intSaveChanges ()9 {Ten returndb. SaveChanges (); One } A}
One of the simplest core layers has been built, although there is no way to do it in the meantime. The next step is to create the core object in the Web tier. So can the core be a singleton in the Web layer? The single core example also means a singleton for the EF entity, because the instance process of EF is in the core construction method. The EF itself is not guaranteed to be thread-safe. When I was talking to my predecessors about the question of whether EF should be a single case, almost two people also told me not to, they are all in the middle, if the simplest static to achieve the simple, not only high concurrency, but as long as the concurrency will be problematic. Of course there is another way, using CallContext to put the core object into the thread, each time it takes from the thread, this way to ensure the EF thread security, which I used a more reliable single-case method, and for the moment there is no problem. But in order to ensure high concurrency performance, and drawing on the mechanisms provided by AUTOFAC (IOC), I decided to create a core object for each request.
The process of configuring my AUTOFAC is as follows
1. Download and introduce the AUTOFAC assembly
: http://autofac.org/
Here the two assemblies need to be introduced in the Web, the former is the core of AUTOFAC, the latter is its MVC plug-in, if you need to write the injected mapping object into the configuration file, but also need to introduce Autofac.Configuration.dll this assembly, Because I wrote the mappings directly in the code, there was no citation.
2. Create the Registerautofacforsingle class in the Web layer, although the name is single, but only when registering the individual, not to say that the object is a singleton.
1 Public classRegisterautofacforsingle2 {3 Public Static voidREGISTERAUTOFAC ()4 {5Containerbuilder Builder =NewContainerbuilder ();6 Builder. Registercontrollers (assembly.getexecutingassembly ());7 8 //Register to Controller9Builder. Registertype<edua_core.coresession> (). As<edua_icore.icoresession>(). Instanceperhttprequest ();Ten One //Register to filter ABuilder. Registertype<edua_core.coresession> (). As<edua_icore.icoresession>(); -Builder. Registertype<filters.myauthorizeattribute>(). SingleInstance (); - Builder. Registerfilterprovider (); the - varcontainer =Builder. Build (); -Dependencyresolver.setresolver (NewAutofacdependencyresolver (Container)); - } + -}
Line 6th registers the version of the controller, line 9th method Instanceperhttprequest () creates a new instance for each new request, followed by the behavior that is registered to the filter, which is the global singleton, because there is no write operation in my filter, There are no read and write problems when concurrency occurs.
3. Register AUTOFAC in the Globa.asax file
1 Public classMvcApplication:System.Web.HttpApplication2 {3 protected voidApplication_Start ()4 {5 //Register AUTOFAC6 Registerautofacforsingle.registerautofac ();7 8 Arearegistration.registerallareas ();9 Ten Webapiconfig.register (globalconfiguration.configuration); One filterconfig.registerglobalfilters (globalfilters.filters); A routeconfig.registerroutes (routetable.routes); - bundleconfig.registerbundles (bundletable.bundles); - } the}
It should be noted that due to the registerautofacforsingle in the filter registered injection, so the registration AUTOFAC need to be placed before the registration filter, or will be error. I put AUTOFAC's registration at the front.
4. Add a core object instance to the controller and filter
1 Private icoresession icoresession; 2 Public Myauthorizeattribute (icoresession icoresession) 3 {4this . icoresession = icoresession; 5
At this point, the content in the core can be called directly into the controller and filter.
In the next article I will give a login example to expand the core layer, and in the filter to implement the right to judge.
MVC+EF+AUTOFAC (Dbfirst) Light Project framework