2017-01-02 24 views
0

我正在尋找一些關於使用Prism Unity加載查詢表(如狀態代碼)的好設計的建議嗎?我的視圖庫是專注於領域的,我擁有通​​過IUnityContainer傳遞的模塊。在初始化部分,我向容器註冊RegisterType,例如IStateCode,StateCode。何時是使用棱鏡/統一加載查找表的最佳時間?

我應該registerType,然後加載狀態對象,然後使用RegisterInstance?這應該在每個域(dll)中完成,還是應該集中加載一次表以及在哪裏?我曾想過在mainwindow的Load中加載查找表,但我必須引用該模塊中的所有查找類。如果我使用中央位置來加載查找表,我不必擔心查找表爲空,並且它位於一個區域中。你怎麼說?

回答

1

我採取的這種方法是在解決方案中創建一箇中心項目;這可以稱爲Core.UI(或任何你喜歡的)。在那裏,我創建了一個類,它以container的身份註冊爲singleton,它在應用程序啓動時加載它需要的數據(通過Initialize調用;參見代碼)。這通常被稱爲服務。

你可以在數據加載時靈活,只要你喜歡。在應用程序加載時,或第一次訪問該屬性時。我在前面做了我的工作,因爲數據不是很大,並且不會經常改變。您甚至可能也想在這裏考慮某種緩存機制。

我也爲產品做了類似的事情。以下是美國州代碼。

public class StateListService : IStateListService // The interface to pass around 
{ 
    IServiceFactory _service_factory; 
    const string country = "United States"; 
    public StateListService(IServiceFactory service_factory) 
    { 
     _service_factory = service_factory; 
     Initialize(); 
    } 

    private void Initialize() 
    { 
     // I am using WCF services for data 
     // Get my WCF client from service factory 
     var address_service = _service_factory.CreateClient<IAddressService>(); 
     using (address_service) 
     { 
      try 
      { 
       // Fetch the data I need 
       var prod_list = address_service.GetStateListByCountry(country); 
       StateList = prod_list; 
      } 
      catch 
      { 
       StateList = new List<AddressPostal>(); 
      } 
     } 
    } 

    // Access the data from this property when needed 
    public List<AddressPostal> StateList { get; private set; } 
} 

編輯:

要註冊上面一個單在棱鏡6,這行代碼添加到您用來初始化容器的製造方法。通常在bootstapper中。

RegisterTypeIfMissing(typeof(IStateListService), typeof(StateListService), true);