Fourth chapter Spring.net How to manage the manual assembly of your Class ___ objects

Source: Internet
Author: User

Before we know what an object is, what is an object factory, and what is the application context. This time we'll look at the assembly of the object.

There are many ways to assemble objects in spring.net, and the word assembly may be more scholarly, and we can understand the creation of objects.

The assembly methods commonly used in Spring.net are manual assembly and automatic assembly. Manual assembly is based on the configuration file and then assembly of the object, and automatic assembly refers to the spring.net according to the specified automatic mode to find the relevant properties and automatically assemble. These two kinds of assembly methods are divided into the assembly by the properties, through the construction of the Assembly, through the static factory assembly, through the case of factory assembly, generic assembly and so on. These assembly methods are described below.

4.4.1 Manual Assembly

In Spring.net, manual assembly is divided into various assembly methods, we introduce several commonly used assembly methods:

(1) Assembly by attributes

(2) Assembly by means of a construction device

(3) through static factory assembly

(4) Assembly by Example Factory

(5) Assembly of the array

(6) Assembly of the generic set (List,dictionary)

(7) Assembly of generic objects

(8) Assembly of events

4.4.1.1 Assembly by Attributes

In this section, we illustrate, by way of example, how to assemble objects through attributes, we only discuss simple properties, such as arrays, collections, custom classes, which we put together in a later discussion, and this is the usual person and dog story.

A person class, with properties such as Id,name,isstudent,dog (puppy), wake-up Method (sleeplightly), and a ToString () method to output information

 1 using System; 2 using System.Collections.Generic;  3 4 Namespace Cnbloglesson_4_4_1.model 5 {6//<summary> 7//Human 8//</summary> 9 public Class Person10 {one person ()} {}12 public person (string name) {. Name = name;14}15 public person (int ID, string name, bool Isstudent,dog Dog) {. Id = id;17 this. Name = name;18 this. Isstudent = isstudent;19 this. Dog = dog;20}21//<summary>23///&LT;/SUMMARY&GT;25 public I NT Id {get; set;} &LT;SUMMARY&GT;28////</summary>30 public string name {get; SE T }31//&LT;SUMMARY&GT;33///IS student//</summary>35 public bool Isstudent {GE T Set }36 Notoginseng///<summary>38///Small dog//</summary>40 public Dog Dog {get; set;}  &LT;SUMMARY&GT;43////&LT;/SUMMARY&GT;45 public object[] Objarray { Get Set }46///<summary>48//&LT;/SUMMARY&GT;50 public list<string> BOOKS;51///<summary>53///</summary>55 public dictionary<s Tring, string> Friends {get; set;} *///&LT;SUMMARY&GT;58///</summary>60//<param name= "args" >  </param>61 public void sleeplightly (String args) Console.WriteLine ("{0} called, awakened the master", args);}65//<summary>67///Rewrite ToString Method//</summary>69/ <returns></returns>70 public override String ToString () (+) (Dog = null) 7 3 {Console.WriteLine ("I am {0}, my id is: {1}, I am not a student:{2}, I do not have a puppy ", Name, ID, isstudent);}76 else {Console.WriteLine (" I am {0}, my id is: {      1}, I am not a student: {2}, my puppy is called: {3} ", Name, Id, Isstudent, dog.name);}79 return string.empty;80}81 }82}

The puppy has a name attribute, an event, and a method called:

1 namespace Cnbloglesson_4_4_1.model 2 {3//     <summary> 4//     Small Dog 5//     </summary> 6 public     C Lass Dog 7     {8 public         Dog () {} 9 Public         Dog (string name) {             . Name = name;11}12//<summary>14//         puppy name//         </summary>16         Public String Name {get; set;} All public         Event sleeplightly sleeplightly;19//<summary>21///         </summary >23 public         void Cry ()         28 {             if (sleeplightly! = null): +                 sleeplightly.invoke ("cat");             }29         }30     }31}

Program Main Method:

1 using System; 2 using Spring.context; 3 using Spring.Context.Support; 4  5 Namespace Cnbloglesson_4_4_1 6 {7     class program 8     {9         static void Main (string[] args)             Iapplicationcontext to configure             iapplicationcontext context = Contextregistry.getcontext ();                //4.4.1.1 Assemble by attribute the             Hexu = (person) context. GetObject ("Hexu");             Hexu. ToString ();             Console.readkey ();         }19     }20}

We will then create a object.xml to configure the object, set the Object.xml as an embedded resource, (it must be set as a dive resource).

The Assembly of a property is typically represented by the <property name= "Id" value= "1"/> label, which indicates the name of the property to set, such as setting Id,value to represent the value of the id attribute.

To assemble a simple property:

1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <objects xmlns= "Http://www.springframework.net" > 3  4   <!--4.4.1.1 is assembled by properties--5    6   <!--human objects--7   <object id= "Hexu" type= "cnbloglesson_4_4_1. Model.person,cnbloglesson_4_4_1 "> 8     <!--set number--9     <property name=" id "value=" 1 "/>10     <!--set name-->11     <property name= "name" value= "Hexu"/>12     <!--setting is student-->13     < Property Name= "Isstudent" value= "false"/>14     <!--My pet is an object, the reference to this object is the dog     with the ID Kaqi-->16 <property name= "Dog" ref= "Kaqi"/>17 </object>18   <!--pet object-->20 <object id=   " Kaqi "type=" Cnbloglesson_4_4_1.model.dog,cnbloglesson_4_4_1 ">21     <property name=" name "value=" Kacip "/>   </object>23 </objects>

To assemble a custom object:

Tag <property name= "Dog" ref= "Kaqi"/> Attribute ref can set this property to a reference, which is an OBJECT tag with an ID of Kaqi.

By monitoring the variables, we can see that we have successfully assembled the properties.

4.4.1.2 assembly through the builder

In the previous section we discussed how to assemble objects through attributes, and this section discusses how to assemble through constructors. This time, we have added a parametric constructor to the person class and the Dog class, respectively.

With the constructor assembly, you need to use the <constructor-arg name= "name" value= "Hexu"/> label. Name indicates the constructor parameter name, and value represents the value assigned by the parameter.

Objects.xml for the following:

1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <objects xmlns= "Http://www.springframework.net" > 3   <!-- 4.4.1.2 assembly by Constructor--4  5   <!--have a constructor for a parameter--6   <object id= "hexu_2_1" type= "cnbloglesson_4_4_ 1.model.person,cnbloglesson_4_4_1 "> 7     <constructor-arg name=" name "value=" Hexu "/> 8   </object > 9   <!--constructors with multiple parameters-->11   <object id= "hexu_2_2" type= "CnblogLesson_4_4_1.Model.Person, Cnbloglesson_4_4_1 ">12     <constructor-arg name=" Id "value=" 1 "/>13     <constructor-arg name=" name " Value= "Hexu"/>14     <constructor-arg name= "Isstudent" value= "true"/>15     <constructor-arg name= " Dog "ref=" kaqi_2 "/>16   </object>17   <!--pet object _2-->19   <object id=" kaqi_2 "type=" Cnbloglesson_4_4_1.model.dog,cnbloglesson_4_4_1 ">20     <property name=" name "value=" Kacip "/>21   < /object>22 </objects>

As you can see through the runtime monitoring variable, loading the object through the constructor has succeeded:

4.4.1.3 assembly through a static factory

To assemble from a static factory, you must have a factory object, and we'll start by creating a static factory object.

1 using Cnbloglesson_4_4_1.model; 2 namespace Cnbloglesson_4_4_1.factory 3 {4 public     static class Staticfactory 5     {6 public         static person Crea Teinstance () {7             dog dog = new Dog ("Kacip"); 8 person Person             = new person (1, "Hexu", False,dog); 9             return person;10         }11     }12}

Let's take a look at Objects.xml:

1 <?xml version= "1.0" encoding= "Utf-8"? >2 <objects xmlns= "http://www.springframework.net" >3   <!-- The 4.4.1.3 is assembled through a static factory-->4   <object name= "staticfactory" type= "CnblogLesson_4_4_1.Factory.StaticFactory, Cnbloglesson_4_4_1 "factory-method=" CreateInstance "/>5 </objects>

With static factory assembly, you need to configure a factory object and set the method for static factory-created objects Factory-method the method for creating objects in the class.

With runtime monitoring variables, loading objects through a static factory has succeeded:

4.4.1.4 assembly through an instance factory

Loading objects through the instance factory method is similar to how the objects are loaded through a static factory method. At this point, the object where the instance factory method resides must also be configured in the same container (or parent container).

If you are loading an object from an instance factory method, the object definition cannot contain the type attribute, but the object that contains the factory method is referenced with the Factory-object property. Note that the property value must be the name of the object that contains the factory method, and the object must be defined in the current container or in the parent container. The method name of the factory method is specified by the Factory-method property. (Why use Factory-object instead of type?) This is spring.net the rules they define. Here we also like to assemble through a static factory, through an instance factory assembly, you need to define an instance factory object.

Instance Factory class:

1 using Cnbloglesson_4_4_1.model; 2 namespace Cnbloglesson_4_4_1.factory 3 {4 Public     class Instancefactory 5     {6 "Person         CreateInstance" ( {7             dog dog = new Dog ("Kacip"); 8 person Person             = new person (1, "Hexu", False,dog); 9             return person;10         }11
   }12}

The Objects.xml configuration is as follows:

With instance factory assembly, you need to configure a factory object. such as: id= "Instancefactory"

You then need an instance object id= "Instanceperson" and set the instance to be created by the factory object, set factory-object= "Instancefactory", and set the method for the factory to create the instance factory-method= "CreateInstance".

Let's take a look at Objects.xml:

1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <objects xmlns= "Http://www.springframework.net" > 3   4   <!--4.4.1.4 assembled from an instance factory-5  6   <!--factory--7   <object id= "instancefactory" type= " CnblogLesson_4_4_1.Factory.InstanceFactory, Cnbloglesson_4_4_1 "/> 8   <!--created objects, Factory-object points to Instancefactory, which represents the CreateInstance method of Instancefactory factory to create this object--9   <object id= " Instanceperson "factory-method=" CreateInstance "factory-object=" Instancefactory "/>10 </objects>

With runtime monitoring variables, loading objects through a static factory has succeeded:

4.4.1.5 assembling an array

We discussed the assembly of properties together, but we discussed some simple properties before. In this section we will discuss how arrays are assembled.

The Objects.xml configuration is as follows:

1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <objects xmlns= "Http://www.springframework.net" > 3   <!-- 4.4.1.5 Assembly of Arrays--4   <!--human objects-   -5 <object id= "Hexu_2_5" type= "CnblogLesson_4_4_1.Model.Person, Cnbloglesson_4_4_1 > 6     <!--set name--7     <property name= "name" value= "Hexu"/> 8     <property Name= "Objarray" > 9       <set>10         <value> family </value>11         <value> Friends </value>12         <value> Work </value>13         <value> program </value>14       </set>15     </ property>16   </object>17 </objects>

With runtime monitoring variables, loading objects through a static factory has succeeded:

4.4.1.6 List of assemblies for generics

In this section, we will discuss the loading of the list collection together.

The Objects.xml configuration is as follows:

1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <objects xmlns= "Http://www.springframework.net" > 3  4   <!--4.4.1.6 Assembly list of generics--5   <!--Human objects--6   <object id= "Hexu_2_6" type= "cnbloglesson_4_4_1. Model.person,cnbloglesson_4_4_1 > 7     <!--set name--8     <property name= "name" value= "Hexu"/> 9     <property name= "Books" >10       <!--sets the type of collection-->11 <list element-type=       "string" >12         <value> Reconstruction </value>13         <VALUE>WCF Comprehensive analysis </value>14         <value> design pattern: The basis of reusable object-oriented software </value>15       </list>16     </property>17   </object>18 </objects>

Label <list element-type= "string" >....</list> represents List<string>,value in C # represents the value of an element

With runtime monitoring variables, loading objects through a static factory has succeeded:

4.4.1.7 the dictionary of the Assembly of the generic type Collection

In this section, we will discuss the loading of the dictionary collection together.

The Objects.xml configuration is as follows:

1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <objects xmlns= "Http://www.springframework.net" > 3  4   <!--4.4.1.7 Dictionary assembly of Generics--5    6   <!--human objects--7   <object id= "hexu_2_7" type= " Cnbloglesson_4_4_1.model.person,cnbloglesson_4_4_1 > 8     <!--set name--9     <property name= "name" Value= "Hexu"/>10     <property name= "Friends" >11       <!--set the type of collection-->12       <dictionary Key-type= "string" value-type= "string" >13         <entry key= "ZG" Value= "Zhang Brother"/>14         <entry key= "LG" value = "Mfullyaware"/>15         <entry key= "WG" value= "Wang Go"/>16       </dictionary>17     </property>18   </object>19   </objects>

Label <dictionary key-type= "string" value-type= "string" >....</dictionary> represents dictionary<string in C #, String>,key-type represents the type of the key, and Value-type represents the type of the value. The entry represents each element.

With runtime monitoring variables, loading objects through a static factory has succeeded:

4.4.1.8 assembly of Generic objects

The method of creating generic objects in Spring.net is the same as for normal objects. But there is a very slight difference.

When specifying the type attribute for a generic class object, be aware that:

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 using type aliases.

First look at the definition of GenericClass.cs:

1 namespace Cnbloglesson_4_4_1.generic2 {3 public     class Genericclass<t>4     {5 public         T obj {get; set;} 6     }7}

The Objects.xml configuration is as follows:

1 <?xml version= "1.0" encoding= "Utf-8"? >2 <objects xmlns= "Http://www.springframework.net" >3 4   <!- -4.4.1.8 generic class assembly such as: Genericclass<string>-->5   <object id= "hexu_2_8" type= "Cnbloglesson_4_4_1". Generic.genericclass&lt;string>, cnbloglesson_4_4_1 ">6     <property name=" obj "value=" Generic "/> 7   </object>8   9 </objects>

With runtime monitoring variables, loading objects through a static factory has succeeded:

5.4.1.9 Assembly of Events

Spring.net's IOC framework also provides an injection of events. By injecting the event, the coupling of the architecture system can be minimized. Still use an example: The owner is sleeping, the thief came, and then the puppy found the thief barking, the host was awakened by the barking of the puppy.

Create a wake-up delegate:

1 using system;2 using system.collections.generic;3 using system.linq;4 using System.text;5 6 namespace cnbloglesson_4_4_ 1.MODEL7 {8 public     delegate void sleeplightly (String args); 9}

To configure the Objects.xml file:

1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <objects xmlns= "Http://www.springframework.net" > 3   <!-- The 4.4.1.9 event is injected with 4  5   <!--first load the Puppy object--6   <object id= "Observer_dog" type= "cnbloglesson_4_4_1. Model.dog, Cnbloglesson_4_4_1 "/> 7   <!--loading host object, the host needs to monitor the trigger of the puppy on the  awakened event, when the puppy barks, the master calls the method of waking up--8   < Object Id= "Observer_person" type= "CnblogLesson_4_4_1.Model.Person, Cnbloglesson_4_4_1" > 9     <!-- Using the listener listener, ref indicates the host to listen to the puppy, event indicates which event the owner listens to the puppy, method means that when the alarm heard the event triggered, call their own wake-up event-->10     <listener event= " sleeplightly "method=" sleeplightly ">11       <ref object=" Observer_dog "/>12     </listener>13   </object>14   </objects>

Assembly events need to be used to <listener event= "sleeplightly" method= "sleeplightly" >...</listener> tags, due to Dog.cs and Person.cs have already written the code, now only need to use Objects.xml to dynamically set the parameters. <listener event= "sleeplightly" method= "sleeplightly" >...</listener> tag, event is used to specify which event in the Dog.cs class needs to be set (Above is the setting of the Sleeplightly event), which means that the value of this event is which method. Using <ref object= "Observer_dog"/> Represents the arguments for this event. You see, in the Dog.cs class, I passed a string "cat" as the parameter, the final output is "cat barking, the host awakened"!

With runtime monitoring variables, loading objects through a static factory has succeeded:

So far, most of the manual assembly methods in Spring.net are in the above article, in the following chapter, will talk about the less commonly used automatic assembly.

Fourth chapter Spring.net How to manage the manual assembly of your Class ___ objects

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.