Iii. iresolver <tservice> and singleserviceresolver <tservice>: iresolver <tservice> and multiserviceresolver <tservice>: iresolver <ienumerable <tservice>
The iresolver <tservice> interface has only one current read-only attribute of the tservice type and returns an object of the corresponding type. This is a lightweight interface with lazy meanings.
In the implementation class of a simple iresolver <tservice> interface, current can return the instance created by activator. createinstance. This makes sense in interface-oriented programming.
The singleserviceresolver class is the implementation of iresolver <tservice>. It is used to obtain a single object. Its implementation principle is: when constructing an object, method internally or as a parameter to specify a default object creation (obtain) "method" -- Method 1, as a parameter to input another creation (obtain) "method" -- method 2 and when
If neither of the two methods is successful (both methods are successful, an exception is thrown), a default object is provided.
Specifically, in its public constructor, the specified method 1 is dependencyresolver. Current (idependencyresolver ).
When the current attribute of the singleserviceresolver class is called, The dependencyresolver is used to determine if method 1 is not null. current. getservice (typeof (tservice) obtains a tservice object. If the object is created (obtained) successfully, method 2 is created (obtained). If the object is also created successfully, an exception is thrown.
If method 1 is null, the execution result of method 2 is obtained. If the result is not null, the result is returned. Otherwise, the default object is returned.
In its internal constructor, you can also pass in the default creation (obtain) method to assign mode 1. In the two constructor methods, you can also input a string to indicate which method is used to create the singleserviceresolver object (or the object returned by the current ). When an exception occurs, you can pass the string into the exception object.
Multiserviceresolver class is also the implementation of iresolver <tservice>. It is used to obtain a set object. Its implementation principle is: when constructing an object, method internally or as a parameter to specify a default object creation (obtain) "method" -- Method 1, as a parameter to input another creation (obtain) "method" -- method 2.
Returns the set created (obtained) in two ways.
Specifically, in its public constructor, the specified method 1 is dependencyresolver. Current (idependencyresolver ).
When the current attribute of the multiserviceresolver class is called, The dependencyresolver is used to determine if method 1 is not null. current. getservices (typeof (tservice) obtains an ienumerable <tservice> object, which can have no elements but cannot be null,
Then, combine the set created in method 2 with the set created in method 1 (obtained) and return the result.
Iv. controllerbuilder
In MVC2, controller is created through controllerfactory, while controllerfactory is obtained through the getcontrollerfactory () method of controllerbuilder. Current. A defaultcontrollerfactory object is initialized in the default constructor of the class, packaged into a func <icontrollerfactory> instance through the setcontrollerfactory method, and assigned to _ factorythunk private delegate, when the getcontrollerfactory method is called, the execution result of the delegate is returned.
In mvc3, the constructor of the controllerbuilder class is different from the getcontrollerfactory method.
Controllerbuilder has a private variable of the iresolver <icontrollerfactory> type _ serviceresolver. From the above analysis, we know that iresolver <icontrollerfactory> has a unique read-only attribute t current. Calling _ serviceresolver. Current will return an icontrollerfactory object.
The default constructor no longer directly creates the defaultcontrollerfactory object, but simply calls the newly added constructor that accepts the iresolver <icontrollerfactory> interface parameter. This method is currently internal.
Internal controllerbuilder (iresolver <icontrollerfactory> serviceresolver ){
_ Serviceresolver = serviceresolver ?? New singleserviceresolver <icontrollerfactory> (
() => _ Factorythunk (),
New defaultcontrollerfactory {controllerbuilder = this },
"Controllerbuilder. getcontrollerfactory"
);
}
Within the parameter constructor, if the input serviceresolver parameter is null, A singleserviceresolver <tservice> Class Object implementing the iresolver <icontrollerfactory> interface is created and assigned to the private Variable _ serviceresolver. Controllerbuilder. Current is an object created using the default constructor. It uses the singleton mode. So here the serviceresolver parameter is null.
Go deep into the singleserviceresolver class and we know that it will first try from dependencyresolver. current. getservice (typeof (icontrollerfactory) gets the icontrollerfactory object. By default, activator is called. createinstance (typeof (icontrollerfactory), of course, will be null. Therefore, the defaultcontrollerfactory will be created as our default controllerfactory.
Assume that an unitycontrollerfactory: icontrollerfactory is defined.
First, we can use setcontrollerfactory to set icontrollerfactory as in MVC2.
Assume that we have implemented an unitydependencyresolver: idenpendencyresovler and set it to the current idenpendencyresovler.
Ing unitycontrollerfactory in unitycontainer to the icontrollerfactory interface can also achieve the purpose of setting controllerfactory.
In addition, the getcontrollerfactory method no longer returns the execution result of _ factorythunk in MVC2, but the current attribute of _ serviceresolver.