asp.net MVC controller activation system Detailed: IOC application [Next]

Source: Internet
Author: User

[Previous] In addition to introducing the IOC through custom controllerfactory, it is possible to use the default defaultcontrollerfactory to make an IOC based controller active. The main way is to customize Controlleractivator and Dependencyresolver.

Iv. controlleractivator v.s. dependencyresolver

As the following code fragment shows, Defaultcontrollerfactory has two constructor overloads, one with a parameter of type Icontrolleractivator interface. We will implement the type of the interface known collectively as Controlleractivator.

   1:public class Defaultcontrollerfactory:icontrollerfactory
2: {
3: //other Members
4: Public defaultcontrollerfactory ();
5: Public defaultcontrollerfactory (Icontrolleractivator controlleractivator);
6:}

As the name suggests, Controlleractivator is controller's "activator", and controller activation is implemented in the unique create method. As shown in the following code, the method has two parameters (RequestContext and Controllertype) that represent the current request context and the type of the resolved target controller.

   1:public Interface Icontrolleractivator
2: {
3: icontroller Create (RequestContext requestcontext, Type controllertype);
4:}

By default (calling the Defaultcontrollerfactory default constructor or specifying a null parameter), the controller activation system uses an object of type Defaultcontrolleractivator by default. As the following code fragment shows, Defaultcontrolleractivator is a icontrolleractivator private type, and we can't use it directly in a programmatic way.

   1:private class Defaultcontrolleractivator:icontrolleractivator
2: {
3: Public defaultcontrolleractivator ();
4: Public defaultcontrolleractivator (Idependencyresolver resolver);
5: Public icontroller Create (RequestContext requestcontext, Type controllertype);
6:}

The constructor of Defaultcontrolleractivator has a parameter of type Idependencyresolver, this is an important interface, we will implement the type of this interface collectively as Dependencyresolver. As shown in the following code fragment, the Idependencyresolver interface has two methods GetService and getservices, which are used to get single or multiple instances based on the specified type. In fact, Defaultcontrolleractivator is simply by invoking the GetService method to obtain the specific Controller object's

   1:public Interface Idependencyresolver
2: {
3: Object GetService (Type servicetype);
4: ienumerable<object> getservices (Type servicetype);
5:}

If the parameter passed in to construct the Defaultcontrolleractivator object is null, Then the controller activation system uses the static read-only property of Dependencyresolver to represent Dependencyresolver. It is to be reminded that the Dependencyresolver type does not implement the Idependencyresolver interface, but rather the encapsulation of an object that implements the Idependencyresolver interface type.

 1:public class Dependencyresolver 
2: {
3:private static dependencyresolver _instance;
4:
5:public void Innersetresolver (object commonservicelocator);
6:public void Innersetresolver (Idependencyresolver resolver);
7:public void Innersetresolver (Func<type, object> getservice, Func<type, Ienumerable<object>> ; GetServices);
8:
9:public static void Setresolver (object commonservicelocator);
10:public static void Setresolver (Idependencyresolver resolver);
11:public static void Setresolver (Func<type, object> getservice, Func<type, ienumerable<object>& Gt GetServices);
A:
13:public static idependencyresolver current {get;}
14:public idependencyresolver innercurrent {get;}
:}

This encapsulated dependencyresolver (refers to the type of a type that implements the interface Idependencyresolver, not the Dependencyresolver type object, and for the latter I will use " The Dependencyresolver type object is represented by a read-only property innercurrent, and three Innersetresolver method overloads are used to initialize the property. The static field _instance represents the current Dependencyresolver type object, and the static read-only property, currently, represents the encapsulated Dependencyresolver object inside the object. And it is initialized with three static setresolver.

If we do not display settings by calling the Dependencyresolver static method setresolver The current dependencyresolver represented by the present property, This property returns a Defaultdependencyresolver object by default. As the following code fragment shows, Defaultdependencyresolver is a private type that implements the Idependencyresolver interface, in the GetService method of implementation, It directly creates and returns the corresponding object in the form of reflection based on the specified type, so we say that defaultcontrollerfactory the corresponding instance in the form of reflection based on the parsed controller type is confirmed here. As for the GetServices method, a collection of empty objects is returned.

   1:private class Defaultdependencyresolver:idependencyresolver
2: {
3: Public object GetService (Type servicetype)
4: {
5: if (servicetype.isinterface | | servicetype.isabstract)
6: {
7: return null;
8: }
9: Try
%: {
One: Return activator.createinstance (servicetype);
: }
: Catch
: {
: Return null;
: }
: }
18:
: Public ienumerable<object> getservices (Type servicetype)
: {
: Return enumerable.empty<object> ();
: }
23:}

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.