Autofac IoC container basic procedure, autofacioc

Source: Internet
Author: User

Autofac IoC container basic procedure, autofacioc

1. Basic steps:

1. design applications suitable for controlling reverse (IoC)

2. reference the application Autofac.

3. register the component.

4. Create a iner for future use.

5. Create a lifetime scope from the Container.

6. Use this Lifetime Scope to parse the component instance.

Ii. Details:

1. What is control reversal?

Inversion of Control (IoC) is an important Object-Oriented Programming Law to reduce the coupling problem of computer programs. Another name is Dependency Injection ). DI for short. IoC is IoC, not a technology. Like GoF, IoC isDesign Mode.

Interface Driven Design Interface driver, Interface driver has many advantages, can provide different flexible subclass implementation, increase code stability and robustness, etc., but the Interface must be implemented, that is, the following statement will be executed sooner or later: AInterface a = new AInterfaceImp (); in this way, the coupling relationship is generated. After IoC is adopted, we will hand over an instance of the created interface to the IoC container. this achieves decoupling.

Therefore, one of our principles in programming is "programming for interfaces". The "interface" here does not mean an interface type. It can be an interface type in C, you can also create a specific parent class.

   public interface  ILcService    {        string ServiceName { get; }    }    public  class LcService:ILcService    {        public string ServiceName        {            get;            set;        }    }


2. How to add a reference to Autofac?

1) We can download the dll file from the official Autofac website and add references to the project of Visual Studio.

 

2) You can also use NuGet to install the autofac package.

3. How to register components?

In Autofac, we useContainerBuilder class instance to Register our components. Autofac provides the Register Method family for us to Register components.

            ContainerBuilder builder = new ContainerBuilder();            builder.RegisterType<ILcService>().As<LcService>().InstancePerDependency();

4. Create a iner for future use.
Direct callBuild () of the ContainerBuilder class instance.

    var container = builder.Build();

5. Create a Lifetime Sope.

      var lifetimescope = container.BeginLifetimeScope();

6. Get the instance we need.

        ILcService service = lifetimeScope.Resolve<ILcService>();     

 

We can see at the end that after IoC is used, it will meet the requirements in the design mode"For interface programming, not for implementation programming"; Creating an instance is not usedNew KeywordThe task of creating an instance is handed over to the IoC container, which achieves link decoupling. You can replace the specific implementation class in the IoC container at will.

Another question is, "Why don't we directly resolve the specific instance from the iner, instead of creating a lifetimescript and then parsing it from it? "

This is to consider the memory leakage issue. when the lifetimescript is used up, it is released, and the instances in it are also released, so there will be no memory leakage. The Container is a root and will never be released if there is an instance in it. memory leakage may occur.

 


Ask, even if spring's aop is not used, the object generated by spring's ioc container is also a proxy object pair.

I don't know how you understand it?
I have not carefully read the implementation of Spring, but I personally understand that containers do not need to generate a proxy, As long as dependencies are injected into the container, and no additional access control is required.

How to Use the IBatisNet framework and the castle ioc container to build the ASPNET project

On the surface, we are developing a variety of different applications. In fact, the corresponding architecture design is relatively stable. Programming in a good architecture is not only a pleasing thing for developers, but more importantly, the software can show a healthy posture. The architecture design is unreasonable, not only do developers suffer, but the life cycle of the software itself is even more under severe threat. Here I will give a rough Discussion on the System Architecture Design for application development on the Microsoft dotNet platform.

Overall Design
Presentation Layer
The presentation layer consists of the UI (User Interface) and UI control logic.
UI (User Interface)
The UI is the user interface of the client. It receives commands, requests, and data from the user, passes them to the business layer for processing, and then presents the results. Based on different clients, we generally divide the applications into BS (Browser-Server) Browser structure and CS (Client-Server) Desktop Client structure.

The advantage of BS is that you do not have to worry about the client. You only need to deploy and maintain the server. The advantage of CS is its powerful interface interaction and expression capabilities. Rich Internet Application (RIA) is a technology that integrates the advantages of these two structures, it relies on the one-time installation of a universal interpreter on the client to obtain powerful interface interaction and convenience without the need to deploy a specific client. There are many specific implementation technologies, such as Microsoft's SmartClient, avron; Macromedia's Flex; JS-based Bindows; Ajax and so on.

UI control logic
The UI control logic is responsible for processing data interaction between the UI and the business layer, controlling the state process between the UI and simple data verification and formatting functions. Specifically, in the dotNet event-driven programming model, the UI control logic is naturally implemented in event functions, such as PageLoad event functions and ButtonClick event functions. Among these event functions, the main task is to exchange and call the data between the UI control and the business entity. However, in the face of a large amount of data exchange workload and maintenance workload, it becomes the biggest problem. In a complex application system, state and process management must be considered. It includes two aspects: interface and business. If it is written directly in the event function without being encapsulated, the business will depend on the presentation layer. The two questions are discussed below.

1. Data Interaction Between the UI and business entities

In this phase, the business entity responsible for Data exchange is called DTO (Data Transfer Object). However, it must be noted that DTO is not a business Object that only contains Data, it still contains the necessary method to be a complete business entity. When processing input, we enter DTO into the retrieved data from the UI control and then spread it downward. when processing the output, the user sends a request to the business layer to return the data in the form of DTO and then assign it to the UI control for display. Therefore, we need a way to automatically solve the problem of round-trip assignment. Formbean of Structs in Java provides some support for this problem. Unfortunately, although many controls in dotNet support data binding, there is still no ready-to-use solution. One simple way is to design an Adapter to automatically handle such binding according to a certain ing relationship. Such a ing relationship is preferably the prior naming convention between the UI control and the DTO attribute, the conventions in this way can be implemented as mappings without adding any configuration files or configuration work. For example, if you name txtUserName as the Textbox of an input name, and my business entity attribute as UserName, you can find the corresponding string by searching.

2. Status and process management

The status and process of complex businesses can be solved through some workflow engines. Recently, Microsoft independently released its own workflow engine. If you are interested, you can check it out. Generally, you need to manage the status and process on the interface. Coupling is not desirable in the presentation layer. The Model-View-Controller mode provides methods to achieve this goal. Con ...... remaining full text>
 

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.