Ioc Autofac experience and iocautofac experience

Source: Internet
Author: User

Ioc Autofac experience and iocautofac experience

I am not very familiar with this container injection, and many do not understand it yet. I will only perform basic operations and record it in a few days. I believe that I will take it to another level soon.

The following is a record of the application process.

Steps:

1. Add an application

2. Rewrite the factory (Here we talk about the common constructor injection method)

1 public class CreateAutofacFactory: DefaultControllerFactory 2 {3 public override IController CreateController (RequestContext requestContext, string controllerName) 4 {5 // get container 6 IContainer ic = CreateControllers. createController (). icontainer; 7 // obtain the Controller Type 8 Type t = this. getControllerType (requestContext, controllerName); 9 // parse the object 10 return ic from the container. resolve (t) as IController; 11} 12}View Code 1 public class CreateControllers 2 {3 private CreateControllers () {} 4 private static CreateControllers createController; 5 public static CreateControllers CreateController () 6 {7 if (createController = null) 8 createController = new CreateControllers (); 9 return createController; 10} 11 private IContainer iContainer {get; set ;} 12 public IContainer Icontainer13 {14 get 15 {16 if (iContainer = null) 17 {18 ContainerBuilder cb = new ContainerBuilder (); 19 // use the code to register type 20 // cb. registerType <StudentDao> (). as <IStudentDao> (); 21 // cb. registerType <StudentManager> (). as <IStudentManager> (); 22 // cb. registerType <HomeController> (); 23 24 // register type 25 cb from the configuration file. registerModule (new ConfigurationSettingsReader ("autofac"); 26 iContainer = cb. build (); // create container 27} 28 return iContainer; 29} 30} 31}View Code

3. Start (replace) the factory

1 public class MvcApplication: System. web. httpApplication 2 {3 protected void Application_Start () 4 {5 AreaRegistration. registerAllAreas (); 6 7 WebApiConfig. register (GlobalConfiguration. configuration); 8 FilterConfig. registerGlobalFilters (GlobalFilters. filters); 9 RouteConfig. registerRoutes (RouteTable. routes); 10 BundleConfig. registerBundles (BundleTable. bundles); 11 12 // change ControllerFactory (Change Factory as soon as the server starts) 13 ControllerBuilder. current. setControllerFactory (new CreateAutofacFactory (); 14} 15}View Code

4. Add a configuration file

1 <configSections> 2 <! -- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink? LinkID = 237468 --> 3 <section name = "entityFramework" type = "System. data. entity. internal. configFile. entityFrameworkSection, EntityFramework, Version = 5.0.0.0, Culture = neutral, PublicKeyToken = b77a5c561934e089 "requirePermission =" false "/> 4 5 <! -- Add the configuration node name in the configuration file. The label name of the configuration node is 6. type is the class format used to read the content of the configuration node: namespace name. class name, assembly name --> 7 <section name = "autofac" type = "Autofac. configuration. sectionHandler, Autofac. configuration "/> 8 9 </configSections> 10 11 <autofac> 12 <components> 13 <component type =" Winning. tjgi. backstage3G. UI. controllers. homeController, Winning. tjgi. backstage3G. UI "14/> 15 <component16 type =" test. dal. userDao, test. dal "17 service =" test. IDal. IUserDao, test. IDal "/> 18 <component19 type =" test. bll. userManager, test. bll "20 service =" test. IBll. IUserManager, test. IBll "/> 21 </components> 22 </autofac>View Code

5. Constructor

1 IUserManager uMan = null; 2 public HomeController (IUserManager uMan) 3 {4 this. uMan = uMan; 5}View Code

In five steps, if the process goes wrong, it is basically a configuration file configuration error. In addition, you often forget to update the dll class library. We recommend that you change the output path of the Class Library to the UI project, I often remember what I was doing, and I was always trying to get out of my own path. Although I was weak, I did not retreat.


Where is the Repository project development (Repository mode) course sharing based on aspnet wpf technology and MVP mode?

This course is divided into five parts: 1. use ADO. NET to Operate Excel table 2. Using WPF special effects and animation technology to build a superb Interface 3. Using WPF controls for Data Binding 4. Using Repository mode, MVP and MVVM frameworks to build a system architecture 5. IOC injection objects. Course outline: 1st: project requirement analysis 2nd Lecture: Project Database File design 3rd Lecture: Data File Access Technology for Construction Projects-excel operation technology 4th Lecture: Project multi-layer architecture design 5th Lecture: build a project's data file access layer 1-IDAL 6th lecture: Build a project's data file access layer 2-reflection 7th lecture: Build a project's data file access Layer 3-Repository mode 8th Lecture: model layer 9th of project construction: Project Interface Structure Design Technology 1-layout painter 10th: Project Interface Structure Design Technology 2-resource use 11th: project UI Structure Design Technology 3-animation 12th Lecture: Project login implementation 13th Lecture: MVP mode login 14th Lecture: Project dialog box implementation 15th Lecture: Project main interface implementation 16th Lecture: project main interface implementation-use Viewport2DVisual3D control 17th Lecture: customer Basic Information Management Module-add and query 18th Lecture: Customer Basic Information Management Module-menu, modify, delete 19th Lecture: MVVM mode for customer query 20th Lecture: Customer Financial Information Management Module 21st Lecture: Customer insurance record information management module 22nd Lecture: customer Family Information Management Module 23rd Lecture: Visiting customer record module 24th Lecture: IOC framework Autofac 25th: Project Development Summary

How to Use autofacmvc4




 

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.