ASP. NET Controller Learning Two

Source: Internet
Author: User

4.Controller Default activation

The activation of the controller object is ultimately done through a registered controllerfactory, When we do not display the Setcontrollerfactory method that calls Controllerbuilder to specify the Controllerfactory object, The controller activation system uses a Defaultcontrollerfactory object to activate the target controller, which is the default controller activation mechanism used by ASP.

The target controller object is activated on the premise of getting the real type of the target controller, including some routing information to parse the controller type in defaultcontrollerfactory, such as the controller name, namespace, the default namespace for the current Controllerbuilder, and so on. You may feel that with these 3 conditions, you should be able to get a controller instance by name, but this is not possible. First, the variable value of the controller name in the Routedata is case-insensitive, and the type name is case-sensitive, and for namespaces, it is common to learn from the previous learning that the name is usually preceded by one. * , so we cannot parse the type of the target controller by name. The correct approach is to defaultcontrollerfactory first call the BuildManager static method Getreferencedassemblies get all the available assemblies, and then select all the types of interface IController that intern , finally, the controller name and namespace to match the target name, simple point, the latter way to ensure that the target controller and namespace has not changed, is the most real data.

For performance reasons, a global cache is made for all valid controller types that are parsed. The cache for the controller type is also implemented in Defaultcontrollerfactory, and the list is persisted, that is, after the application restarts. The controllerfactory used to activate the target controller object is not only used to create the target controller object, there are also 2 functions: one is to release and reclaim the activated controller object, The other is to return the Sessionstatebehavior enumeration object that controls the current session state behavior through the Getcontrollersessionbehavior method.

5.IOC applications

control reversal, referred to as the IOC. I used to learn MVC when I heard the word, but did not understand the word deeply, today I will conquer it. The simple point of the IOC is that the application itself is not responsible for the creation and maintenance of dependent objects, and that the task is given to an external container, so that the application is transferred from the application to the external IOC container, so that control is reversed, that is, control inversion is the reverse of control to others. For example, Class A needs to use instances of Class B, and B instances are created not by a, but by external containers. This idea is of great significance in realizing the activation of the target controller.

IOC and Di (Dependency injection) are often associated. DI is an external container that dynamically injects dependent objects into a component at run time. Mr. Jiang prefers to divide dependency injection into a mapping and 3 injections, which I feel is good to understand.

(1) mapping is a type mapping, although we can invoke the method of an object through an interface or an abstract class, but the object itself is a concrete type, so a type registration mechanism is required to solve the matching relationship between the interface/abstract class and the implementation class/concrete subclass.

(2) constructor injection, the IOC container intelligently selects and invokes the appropriate constructors to create dependent objects. If the selected constructor has a corresponding parameter, the IOC container resolves the registered dependency and creates the corresponding Parameter object itself before calling the constructor.

(3) attribute injection, if a property of the dependent object needs to be used, the IOC will initialize the property automatically after the dependent object is created.

(4) method injection, if the dependent object needs to call a method for the corresponding initialization, the IOC container automatically calls the method after the object is created. Let's look at an example

 Public InterfaceIA {} Public InterfaceIB {} Public InterfaceIC {} Public InterfaceID {} Public classA:ia { PublicIB B {Get;Set; }  PublicID D {Get;Set; } //Constructor Injection             PublicA (IB b) { This. B =b; }            //Attribute Injection             PublicIC C;  PublicIC C {Get{returnC;} Set{c =value;} }            //Method Injection             Public voidInitialize (ID d) {}}

we know that when a user requests to come in, if it involves the data business, the controller will call model directly, if need to render the business data, then the corresponding data will be converted to ViewModel, when the controller is directly dependent on the model. It is now possible to apply the IOC's ideas to the controller activation system, and the degree of dependency between the controller and model will be reduced if the Controller object is provided in the form of an IOC for processing requests. We can also define the Imodel interface to abstract the model in this way, allowing the controller to rely on the abstract model interface rather than the concrete model implementation.

ASP. NET Controller Learning Two

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.