Starting from scratch, build a blog system MVC5+EF6 build Framework (2), test add data, integrate AUTOFAC dependency Injection

Source: Internet
Author: User

First, testing the storage layer, the business layer can achieve the operation of the database table

1. Create Isysuserinforepository interface to inherit ibaserepository parent interface

  1namespace Wchl.WMBlog.IRepository  2 {  3public      Interface Isysuserinforepository:ibaserepository<sysuserinfo>  4     {  5   6     }   7 }
View Code

2. Create Sysuserinforepository class inheritance Baserepository parent class, and Isysuserinforepository interface

Sysuserinforepository class:

  1namespace Wchl.WMBlog.Repository  2 {  3public      class Sysuserinforepository:baserepository<sysuserinfo>, Isysuserinforepository  4      {  5  6     }  7 }
View Code

3. Create service Interface isysuserinfoservices inherit ibaseservices interface

  1namespace Wchl.WMBlog.IServices  2 {  3      public  Interface isysuserinfoservices:ibaseservices<sysuserinfo>  4     {  5      }  6 }
View Code

4. Create service class sysuserinfoservices inherit baseservices, and Isysuserinfoservices interface

  1namespace Wchl.WMBlog.Services  2 {  3       public  Class Sysuserinfoservices:baseservices<sysuserinfo>, isysuserinfoservices  4     {   5  6     }  7 }  8
View Code

5, the service layer of the parent class baseservices in Basedal need new a warehouse parent generic type

Public ibaserepository<tentity> Basedal = new baserepository<tentity> ();

6. Create an added instance under the Mvc,home controller index in the UI layer

1Isysuserinfoservices Userinfoservice =NewSysuserinfoservices ();2 3          PublicActionResult Index ()4{5             Try6{7                 //var usreinfo = Userinfoservice. Querywhere (c = c.uid > 2). FirstOrDefault ();8                  for(inti = 0; I < 10; i++)9{TenUserinfoservice. ADD (NewSysUserInfo () One{ AUloginname = "Admin"+ I, -Uloginpwd = "123456", -Urealname = "Super Admin"+ I, theUcreatetime = DateTime.Now, -Uupdatetime = DateTime.Now, -Uremark = "Test Add feature" -}); +} -  +Userinfoservice. Saverchanges (); A                 returnContent ("Add Data Success"); at} -             Catch(Exception ex) -{ -  -                 returnContent ("error message:"+ Ex. Message);; -} in  -}
View Code

7. Add a database link string to the WebUI Web. config

8. Then start the system page prompt

Indicates that the data can be entered into the database normally, view the database

The data in the database can already be inserted properly, and the field constraints on the table are also successful.

Second, using AUTOFAC Dependency Injection separation interface and the relationship between the instance, to achieve the purpose of decoupling

1. Download AUTOFAC using NuGet management tool, installed in WebUI

2, due to the need to start the system to come to all classes to create the appropriate objects

Need to add a configuration file under the global file to implement the IOC and DI for the MVC Project

3. then create the Autofacconfig file in the App_start file

Autofacconfig file

1 namespaceWchl.WMBlog.WebUI2{3      Public classAutofacconfig4{5         // <summary>6         ///responsible for invoking the AUTOFAC framework to implement the creation of type objects in the business logic layer and data warehousing layer assemblies7         ///The object responsible for creating the MVC Controller class (invoking the parameter constructor in the controller), taking over the work of Defaultcontrollerfactory8         // </summary>9          Public Static voidRegister ()Ten{ One             //instantiation of a AUTOFAC creation container Avar builder =NewContainerbuilder (); -             //Tell the AUTOFAC framework which assembly the Controller class will be created in the Future (Wchl.CRM.WebUI) -Assembly Controllerass = Assembly.Load ("Wchl.WMBlog.WebUI"); theBuilder. Registercontrollers (Controllerass); -  -             //Tell the AUTOFAC framework to register an object instance of all classes in the assembly where the data warehousing layer is located -Assembly Respass = Assembly.Load ("Wchl.WMBlog.Repository"); +             //Create instance for all classes in Respass to be stored in this class's implementation interface -Builder. Registertypes (Respass.gettypes ()). Asimplementedinterfaces (); +  A             //Tell the AUTOFAC framework to register an object instance of all classes in the assembly where the business logic layer is located atAssembly Serpass = Assembly.Load ("Wchl.WMBlog.Services"); -             //Create instance for all classes in Serass to be stored in this class's implementation interface -Builder. Registertypes (Serpass.gettypes ()). Asimplementedinterfaces (); -  -             //Builder. Registertype<> (). As<> (); -  in             //Create a AUTOFAC container -var container = Builder. Build (); to             //Put the Controller object instance of MVC into AUTOFAC to create +Dependencyresolver.setresolver (NewAutofacdependencyresolver (container)); -} the} *}
View Code

4, modify the repository assembly and the Services assembly to build the roadbed to the WebUI layer of the bin directory, you can also directly refer to the way.

5, modify the HomeController controller under the Userinfoservice generation mode, instead of creating a constructor when the generation.

6. Modify the service layer Sysuserinfoservices constructor to generate the corresponding object when it is created

The DAL was copied to the Dal in order to get the special method of Sysuserinfoservices. Base.basedal = dal; A common method for getting the parent class.

The Baseservices class is also modified to not require a new instance class

7, change the HomeController to query data, return to the page.

The display name indicates that the AUTOFAC dependency injection succeeded.

Next article is to add the Nlog log framework and cache caching mechanism using Microsoft's MemoryCache and Redis, I hope we have a lot of spit groove, criticism, thank you.

Starting from scratch, build a blog system MVC5+EF6 build Framework (2), test add data, integrate AUTOFAC dependency Injection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.