天天看點

SPRING.NET 1.3.2 學習4 --執行個體化對象

在Spring.NET下執行個體化對象有幾種方法

1.使用構造器

<object id="exampleObject" type="Examples.ExampleObject, ExamplesLibrary"/>      

 Examples.ExampleObject為編譯成ExamplesLibrary程式集中的類,ExampleObject定義在Examples命名空間中

如果你用定義一個嵌套類,可以使用一個+号,例如Examples.ExampleObject類裡定義了一個嵌套類Person,那麼XML應像如下定義

<object id="exampleObject" type="Examples.ExampleObject+Person, ExamplesLibrary"/>      

2.使用靜态工廠方法

下面的XML是使用Examples.ExampleObjectFactory類中的靜态方法CreateInstance來執行個體化對象

<object id="exampleObject" type="Examples.ExampleObjectFactory, ExamplesLibrary" factory-method="CreateInstance"/>      

3.使用工廠方法執行個體建立對象

下面是使用一個工廠方法執行個體(使用依賴注入來配置這個類)來執行個體化對象

SPRING.NET 1.3.2 學習4 --執行個體化對象
<!-- the factory object, which contains an instance method called 'CreateInstance' -->
<object id="exampleFactory" type="...">
  <!-- inject any dependencies required by this object -->
</object>
<!-- the object that is to be created by the factory object -->
<object id="exampleObject"
      factory-method="CreateInstance"
      factory-object="exampleFactory"/>      
SPRING.NET 1.3.2 學習4 --執行個體化對象

可見這個方法為方法2的更新版,可以配置工廠對象

4.泛型類型的對象建立

假如有一個這樣的類

SPRING.NET 1.3.2 學習4 --執行個體化對象
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>();
        }
    }
}      
SPRING.NET 1.3.2 學習4 --執行個體化對象

xml配置應該像這樣

<object id="myFilteredIntList" type="GenericsPlay.FilterableList&lt;int>, GenericsPlay">
  <property name="Name" value="My Integer List"/>
</object>      

上面的";int"表示執行個體化的時候泛型為int,Name屬性賦了一個初值

 5.使用靜态工廠方法建立泛型類型對象

下面是一個例子:

需要建立的類

SPRING.NET 1.3.2 學習4 --執行個體化對象
1 public class TestGenericObject<T, U>
 2 {
 3     public TestGenericObject()
 4     {
 5     }
 6     private IList<T> someGenericList = new List<T>();
 7     private IDictionary<string, U> someStringKeyedDictionary =
 8         new Dictionary<string, U>();
 9     public IList<T> SomeGenericList
10     {
11         get { return someGenericList; }
12         set { someGenericList = value; }
13     }
14     public IDictionary<string, U> SomeStringKeyedDictionary
15     {
16         get { return someStringKeyedDictionary; }
17         set { someStringKeyedDictionary = value; }
18     }
19 }      
SPRING.NET 1.3.2 學習4 --執行個體化對象

工廠類

?

public

class

TestGenericObjectFactory

{

public

static

TestGenericObject<V, W> StaticCreateInstance<V, W>()

{

return

new

TestGenericObject<V, W>();

}

public

TestGenericObject<V, W> CreateInstance<V, W>()

{

return

new

TestGenericObject<V, W>();

}

}

下面使用XML配置檔案把兩個類關聯起來

<object id="myTestGenericObject"
        type="GenericsPlay.TestGenericObjectFactory, GenericsPlay"
        factory-method="StaticCreateInstance&lt;System.Collections.Generic.List&lt;int>,int>"
/>      

上面的type為工廠類,factroy-method則為名為StaticCreateInstance的方法,System.Collections.Generic.List&lt;int表示第一個泛型類型為List<int>,第二個泛型依然為int

再看下面這個XML

<object id="exampleFactory" type="GenericsPlay.TestGenericObject&lt;int,string>, GenericsPlay"/>
<object id="anotherTestGenericObject"
        factory-object="exampleFactory" 
        factory-method="CreateInstance&lt;System.Collections.Generic.List&lt;int>,int>"/>      

同上一個XML檔案類似,唯一不同的是,使用了非靜态方法建立對象,建立的執行個體為TestGenericObject<List<int>,int>

繼續閱讀