Spring.net generic interface injection + generic interface combined with covariant injection

Source: Internet
Author: User
Tags aliases

First look at the description of the type aliases in the Spring.net operating manual

  

4.12.3. Registering type aliases

As a substitute for the full name of the type, the type alias simplifies the spring.net configuration file. Aliases can be registered in the config file, or can be registered programmatically, and can be used anywhere in the object definition where the full name of the type is required. You can also define type aliases for generic classes.

One way to configure the type alias is to add the Typealiases node and the corresponding node processor in the Web/app.config file. See the example at the beginning of this section, where a type alias is defined for the Webserviceexporter class. Once you have defined the type aliases, you can use them as full names:

<object id= "MyWebService" type= "Webserviceexporter" >    ... </object><object id= "CacheAspect" type= "Defaultpointcutadvisor" >    <property name= "Pointcut" >        <object type= "Attributepointcut" >            <property name= "Attribute" value= "Cacheattribute"/>               </object>    </property>    <property name= "Advice" ref= "Aspnetcacheadvice"/></object>

For information on how to define type aliases for generic types, see 4.2.4, creating objects of generic types.

Alternatively, you can register the type alias with the Spring.Objects.Factory.Config.TypeAliasConfigurer class. This method of definition is more modular, and if there is no app/web.config, the alias can be registered in this way. Take a look at the following example:

<object id= "Mytypealias" type= "Spring.Objects.Factory.Config.TypeAliasConfigurer, Spring.core" >      < Property name= "Typealiases" >          <dictionary>              <entry key= "Webserviceexporter" value= " Spring.Web.Services.WebServiceExporter, Spring.web "/>              <entry key=" Defaultpointcutadvisor "value=" Spring.Aop.Support.DefaultPointcutAdvisor, Spring.aop "/>              <entry key=" MyType "value=" MyCompany.MyProject.MyNamespace.MyType, myassembly "/>          </dictionary>      </property></ Object>
Look at the generic interface injection description 4.2.4. Object creation for generic classes

Generic objects are created the same way as normal objects.

4.2.4.1. Creating a generic object from a constructor

The following is the code for a generic class:

namespace genericsplay{public    class filterablelist<t>    {        private list<t> List;                private String name;        Public list<t> Contents        {            get {return List;}            set {list = value;}        }        Public String name        {            get {return Name;}            set {name = value;}        }                Public list<t> ApplyFilter (string filterexpression)        {//            should really apply filter to List;)            return new list<t> ();}}}    

The following is the object definition for this class:

<object id= "myfilteredintlist" type= "GENERICSPLAY.FILTERABLELIST&LT;INT> GenericsPlay" >  < Property name= "Name" value= "My Integer List"/></object>

Note that when specifying the type attribute for a generic class object: First, the left angle bracket < is replaced with the string "&lt;" because the left angle brackets in the XML are considered less than the number. In terms of readability, we all know that this is not the ideal way. Second, the type parameter value cannot contain the name of the Assembly, because the assembly name requirement and the type full name are separated by commas, where commas have been used to separate the type parameters of the generic class. Other characters may be substituted for these two symbols in the future, but no more readable solution has been found yet. To improve readability, we recommend that you use type aliases, as follows:

<typeAliases> <alias name= "genericdictionary" type= "system.collections.generic.dictionary&lt;,>"/ > <alias name= "mydictionary" type= "system.collections.generic.dictionary&lt;int,string>"/></ Typealiases>

Then, the following object is defined:

<object id= "Mygenericobject"         type= "genericsplay.examplegenericobject&lt; System.collections.generic.dictionary&lt;int, String>>, Genericsplay "/>

It can be shortened to:

<object id= "Myothergenericobject"         type= "genericsplay.examplegenericobject&lt; Genericdictionary&lt;int, String>>, Genericsplay "/>

or shorter:

<object id= "Myotherothergenericobject"         type= "genericsplay.examplegenericobject&lt; Myintstringdictionary>, Genericsplay "/>

    /// <summary>    ///Model Interface/// </summary>     Public InterfaceIModel {}/// <summary>    ///Model Implementation/// </summary>     Public classModel:imodel {}/// <summary>    ///generic interface
/// </summary> /// <typeparam name= "T" ></typeparam> Public Interfacegenericsinterface< outT>whereT:imodel {}/// <summary> ///generic interface Implementation class/// </summary> /// <typeparam name= "T" ></typeparam> Public classGenericsclass<t>: genericsinterface<t>whereT:imodel {}
public class Genericsclasstest    {public        dictionary<int, genericsinterface<model>> dic {get; set;} Public        Dictionary<int, genericsclass<model>> dic1 {get; set;}        Public Dictionary<int, genericsinterface<imodel>> dic2 {get; set;}        Public list<genericsinterface<model>> List {get; set;}        Public list<genericsclass<model>> List1 {get; set;}        Public genericsinterface<model> gi {get; set;}        <summary>        //Success-because this is a covariant interface///        </summary> public        genericsinterface<imodel> Gi1 {get; set;}    }

Look at the configuration file

<!--configuration--><objects xmlns= "http://www.springframework.net" ><!--type alias injection--><object id= " Mytypealias "Type=" Spring.Objects.Factory.Config.TypeAliasConfigurer, Spring.core "><property name=" Typealiases "><dictionary><entry key=" type1 "value=" Spring.net.model,spring.net "></entry> <entry key= "type2" value= "Spring.net.imodel,spring.net" ></entry></dictionary></property> </object><object name= "GC1" type= "Spring.net.genericsclass<type1>,spring.net" ></object> <object name= "P1" type= "spring.net.genericsclasstest,spring.net" ><property name= "GI" ref= "GC1" ></ Property><property name= "Gi1" ref= "GC1" ></property><property name= "dic" ><dictionary key-type= "int" value-type= "spring.net.genericsinterface<type1>,spring.net" ><entry key= "1" value-ref= " GC1 "></entry></dictionary></property><property name=" Dic1 "><dictionary key-type=" int "value-type="Spring.net.genericsclass<type1>,spring.net" ><entry key= "1" value-ref= "GC1" ></entry></ Dictionary></property><property name= "Dic2" ><dictionary key-type= "int" value-type= " Spring.net.genericsinterface<type2>,spring.net "><entry key=" 1 "value-ref=" GC1 "></entry>< /dictionary></property><property name= "list" ><list element-type= "Spring.net.GenericsInterface <type1>,spring.net "><ref object=" GC1 "/><ref object=" GC1 "/></list></property> <property name= "List1" ><list element-type= "Spring.net.genericsclass<type1>,spring.net" ><ref object= "GC1"/><ref object= "GC1"/></list></property></object>

Others do not make a statement

The properties in the Dic2 class and the GI1 use the generic interface while the parameters are also interfaces

So when we declare, the parameters of the specified generic interface are covariant parameters

This will allow you to handle

Spring.net generic interface injection + generic interface combined with covariant injection

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.