[EnterpriseLibrary]Microsoft.Practices.ObjectBuilder來建立Singleton對象

來源:互聯網
上載者:User
  上次我們從總體上瞭解了一下Microsoft.Practices.ObjectBuilder這個類庫,今天我們來看看它是怎樣來完成對象的建立以及我們怎麼應用它來為我們建立我們所需要的對象的,從源碼中我們可以看到可以建立Singleton對象等等,每個對象的建立都對應著每一個策略以及相應的方針,下面我們通過例子來為各個對象的建立進行逐個說明。

  首先就是先看看上次的一個例子,建立一個Singleton對象,代碼如下:
 

 1     public class OBSample
 2     {
 3         public bool CreateSingletonSample() {
 4             // 我們需要一個定位器,一個策略鏈和一個方針列表
 5             Locator locator = new Locator();
 6             BuilderStrategyChain strategyChain = new BuilderStrategyChain();
 7             PolicyList policies = new PolicyList();
 8             //我們要建立一個Singleton對象,需要一個Singleton策略,同時Sigleton策略有Singleton方針來完成。
 9             strategyChain.Add(new SingletonStrategy());
10             strategyChain.Add(new CreationStrategy());
11             policies.Set<ISingletonPolicy>(new SingletonPolicy(true), typeof(TestObject), null);
12             policies.SetDefault<ICreationPolicy>(new DefaultCreationPolicy());
13             // 我們需要一個LifetimeContainer用來放置這個對象。
14             locator.Add(typeof(ILifetimeContainer), new LifetimeContainer());
15             // 建立上下文。
16             BuilderContext context = new BuilderContext(strategyChain, locator, policies);
17             object obj1 = strategyChain.Head.BuildUp(context, typeof(TestObject), "TestObject_Singleton", null);
18             object obj2 = strategyChain.Head.BuildUp(context, typeof(TestObject), "TestObject_Singleton", null);
19             if (obj1 == obj2) {
20                 return true;
21             }
22         }
23     }
24 
25     public class TestObject
26     {
27         public TestObject() {
28         }
29     }

  上面的例子就是通過ObjectBuilder來建立Singleton模式的對象,大部分利用ObjectBuilder來建立對象的過程都和上面的過程差不多。為了建立一個對象,我們要建立一個上下文,而一個上下文又要求一個定位器,以及策略鏈,和一個方針略表。從代碼我們可以知道要建立一個Singleton對象,我們需要一個SingletonStrategy策略,而SingletonStrategy又要有一個SingletonPolicy方針。

  在OB中包含了很多設計模式,例如上面的策略模式,責任鏈模式等等都需要我們對其有所瞭解。上面還用到了一個定位器Locator,它能知道怎樣去尋找已經註冊過的對象。在建立Singleton對象的還需要我們知道的是,定位器知從當前搜尋,即為local而沒有從其父定位器進行搜尋。代碼如下:

 1 public class SingletonStrategy : BuilderStrategy
 2  {
 3     public override object BuildUp(IBuilderContext context, Type typeToBuild, object existing, string idToBuild)
 4     {
 5 
 6        DependencyResolutionLocatorKey key = new DependencyResolutionLocatorKey(typeToBuild, idToBuild);
 7 
 8  
 9 
10        if (context.Locator != null && context.Locator.Contains(key, SearchMode.Local))
11        {
12             TraceBuildUp(context, typeToBuild, idToBuild, "");
13              return context.Locator.Get(key);
14        }
15 
16  
17 
18        return base.BuildUp(context, typeToBuild, existing, idToBuild);
19     }
20  }

  從上面的代碼我們可以看到,在SingletonStrategy策略中先判斷上下文中是否包含一個定位器,如果包含則通過SearchMode.Local進行檢索,也就是告訴定位器不要再父定位器重搜尋。

  我們還可以看出在建立對象時,我們還用到了CreationStrategy和CreationPolicy,從CreationStrategy類中,我們可以發現BuildUp方法檢測了是否需要建立一個新對象或是已經存在一個新對象。在我們的例子裡是第一次建立對象所以將調用BuildUpNewObject方法。這個方法是通過ICreationPolicy來擷取一個對象從而來建立上下文,如果不存在或找不到則將拋出錯誤,也就是說我們需要註冊一個對象個CreationPolicy方針,從而告訴OB怎樣去建立一個對象。在我們的例子中我們用到了DefaultCreationPolicy方針,如果存在一個ICreationPolicy,那麼那個方法將建立一個為初始化的類型existing = FormatterServices.GetSafeUninitializedObject(typeToBuild);然後在調用RegisterObject()來註冊對象。

  就如你所見到的一樣,CreationStrategy在當前的Locator中尋找一個Lifetime Container,如果存在將尋找一個已被將要建立的對象註冊的SingletonPolicy。說來說去我們還是在所我們要建立一個Singleton對象,我們需要一個SingletonStrategy和一個SingletonPolicy。也就是說我們要建立一個對象時,我們要註冊他,當我們要建立一個對象時,我們要先檢查對象是否已經被建立。

  如果有不對的地方請指出,謝謝!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.