Build a blog system MVC5 + EF6 framework from scratch (2), test adding data, integrate Autofac dependency injection, and mvc5autofac

Source: Internet
Author: User

Build a blog system MVC5 + EF6 framework from scratch (2), test adding data, integrate Autofac dependency injection, and mvc5autofac

I. test whether the warehouse layer and business layer can perform operations on database tables

1. Create the IsysUserInfoRepository interface to inherit the IBaseRepository parent interface

1 namespace wchlorophyll. WMBlog. IRepository 2 {3 public partial interface IsysUserInfoRepository: IBaseRepository <sysUserInfo> 4 {5 6} 7}View Code

2. Create the sysUserInfoRepository class to inherit the BaseRepository parent class and the IsysUserInfoRepository interface.

SysUserInfoRepository class:

1 namespace wchlorophyll. WMBlog. Repository 2 {3 public partial class sysUserInfoRepository: BaseRepository <sysUserInfo>, IsysUserInfoRepository 4 {5 6} 7}View Code

3. Create a service interface IsysUserInfoServices to inherit the IBaseServices Interface

1 namespace wchlorophyll. WMBlog. IServices 2 {3 public partial interface IsysUserInfoServices: IBaseServices <sysUserInfo> 4 {5} 6}View Code

4. Create a service class sysUserInfoServices to inherit BaseServices and the IsysUserInfoServices interface.

 

1 namespace wchlorophyll. WMBlog. Services 2 {3 public partial class sysUserInfoServices: BaseServices <sysUserInfo>, IsysUserInfoServices 4 {5 6} 7} 8View Code

5. In baseDal of the parent class BaseServices of the service layer, a new storage parent class generic type is required.

Public IBaseRepository <TEntity> baseDal = new BaseRepository <TEntity> ();

6. Create an added instance under MVC and HOME controller index in the UI Layer.

1 IsysUserInfoServices userinfoservice = new sysUserInfoServices (); 2 3 public ActionResult Index () 4 {5 try 6 {7 // var usreinfo = userinfoservice. queryWhere (c => c. uID> 2 ). firstOrDefault (); 8 for (int I = 0; I <10; I ++) 9 {10 userinfoservice. add (new sysUserInfo () 11 {12 uLoginName = "admin" + I, 13 uLoginPWD = "123456", 14 uRealName = "Super administrator" + I, 15 uCreateTime = DateTime. now, 16 uUpdateTime = DateTime. now, 17 uRemark = "test adding function" 18}); 19} 20 21 userinfoservice. saverChanges (); 22 return Content ("added successfully"); 23} 24 catch (Exception ex) 25 {26 27 return Content ("error message:" + ex. message); 28} 29 30}View Code

7. Add a database link string in WebUI Web. config.

8. Then start the system page and prompt

Indicates that the data can be normally entered into the database and viewed in the database

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

2. Use the Autofac dependency injection isolation interface to decouple the instance

1. Use NuGet to download Autofac and install it in webUI.

2. Because all classes need to be created before the system starts

Add a configuration file under the Global File to implement the IOC and DI of the MVC project.

Create the AutofacConfig file in the App_Start file.

AutofacConfig File

1 namespace wchlorophyll. WMBlog. webUI 2 {3 public class AutofacConfig 4 {5 // <summary> 6 // call the autofac framework to create type objects in the business logic layer and data warehouse layer Assembly 7/ // create an object of the MVC controller class (call the constructor with parameters in the Controller ), take over DefaultControllerFactory's work 8 /// </summary> 9 public static void Register () 10 {11 // instantiate a autofac creation container 12 var builder = new ContainerBuilder (); 13 // inform the Autofac framework of the assembly in which the Controller class to be created will be stored (wchlorophyll. CRM. webUI) 14 Assembly controllerAss = Assembly. load ("wchlorophyll. WMBlog. webUI "); 15 builder. registerControllers (controllerAss); 16 17 // tells the autofac framework to register the object instance 18 Assembly respAss = Assembly of all classes in the Assembly where the data warehouse layer is located. load ("wchlorophyll. WMBlog. repository "); 19 // create instances of all classes in the respAss to store this class's implementation interface 20 builder. registerTypes (respAss. getTypes ()). asImplementedInterfaces (); 21 22 // tell the autofac framework to register the object instance of all classes in the Assembly where the business logic layer is located 23 Assembly serpAss = Assembly. load ("wchlorophyll. WMBlog. services "); 24 // create all the class instances in serAss to store this class's implementation interface 25 builder. registerTypes (serpAss. getTypes ()). asImplementedInterfaces (); 26 27 // builder. registerType <> (). as <> (); 28 29 // create an Autofac container 30 var container = builder. build (); 31 // assign the Controller object instance of MVC to autofac to create 32 DependencyResolver. setResolver (new AutofacDependencyResolver (container); 33} 34} 35}View Code

4. Modify the generate base of the Repository assembly and Services assembly to the Bin directory of the WebUI layer. You can also directly reference this method.

5. Modify the userinfoservice generation method under the HomeController controller to be generated when the constructor is created.

6. Modify the sysUserInfoServices constructor of the service layer to generate corresponding objects during creation.

Copy the dal to obtain its own special method in sysUserInfoServices. Base. baseDal = dal; to obtain the public method of the parent class.

The BaseServices class is also changed to a new instance class.

7. Change homeController to query data and return to the page.

The Autofac dependency injection is successful.

In the next article, we will add the Nlog log framework and the Cache mechanism to use Microsoft's MemoryCache and Redis. I hope you will talk a lot and give me some comments. Thank you.

 

Related Article

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.