2009-08-15 33 views
5

Làm thế nào tôi có thể nhận được một thể hiện của một số loại (đăng ký trong một Registry khác) bên trong constructor StructureMap Registy? Tôi muốn sử dụng mã như vậy:Làm cách nào tôi có thể nhận được một cá thể trong hàm tạo Bản đồ Cấu trúc Bản đồ?

public RepositoriesRegistry() 
    { 
     IApplicationSettings lApplicationSettings = 
      ObjectFactory.GetInstance<IApplicationSettings>(); 
     Debug.Assert(lApplicationSettings != null); 

     const string cSupportedDevicesConnectionString = 
      "metadata=res://*/Models.SupportedDevices.Database.SupportedDevicesModel.csdl|res://*/Models.SupportedDevices.Database.SupportedDevicesModel.ssdl|res://*/Models.SupportedDevices.Database.SupportedDevicesModel.msl;provider=System.Data.SqlClient;provider connection string=\"{0}\""; 
     string lSupportedDevicesConnectionString = 
      string.Format(cSupportedDevicesConnectionString, lDatabaseConnectionString); 
     SupportedDevicesEntities lSupportedDevicesEntities = 
      new SupportedDevicesEntities(lSupportedDevicesConnectionString); 
     ForRequestedType<SupportedDevicesEntities>().TheDefault.IsThis(
      lSupportedDevicesEntities); 
     ForRequestedType<ISupportedDevicesRepository>().TheDefault.IsThis(
      new SupportedDevicesRepository(lSupportedDevicesEntities)); 

    } 

IApplicationSettings là giao diện cho cài đặt ứng dụng. Các kiểu dữ liệu cụ thực hiện giao diện này (hiện ConfigFileApplicationSettings lớp) được đăng ký trong sổ đăng ký khác như thế này:

public ApplicationServicesRegistry() 
    { 
     ForRequestedType<IApplicationSettings>().TheDefault.IsThis(
      new ConfigFileApplicationSettings()); 
    } 

Và cả hai cơ quan đăng ký được đăng ký tại Bootstrapper:

 #region IBootstrapper Members 

    public void BootstrapStructureMap() 
    { 
     ObjectFactory.Initialize(InitalizeStructureMapContainer); 
    } 

    #endregion 

    #region Private properties 

    private static bool HasStarted { get; set; } 

    #endregion 

    #region Private methods 

    private void InitalizeStructureMapContainer(IInitializationExpression x) 
    { 
     x.IgnoreStructureMapConfig = true; 
     x.AddRegistry<ViewModelRegistry>(); 
     x.AddRegistry<ApplicationServicesRegistry>(); 
     x.AddRegistry<RepositoriesRegistry>(); 
     x.AddRegistry<DataOperationsRegistry>(); 
    } 

    #endregion 

Khi tôi cố gắng để có được một thể hiện của IApplicationRegisty trong constructor đăng ký Tôi đã có một lỗi (tất nhiên). Tôi không hoàn toàn hiểu làm thế nào để sử dụng StructureMap đúng cách. Có thể tôi nên làm điều này theo một cách khác. Nhưng dù sao tôi có thể nhận được một thể hiện của một số loại đầu đăng ký trong một nhà xây dựng Registry?

Trả lời

7

Tôi đã gặp phải sự cố tương tự này ngay hôm nay. Câu trả lời từ Jeremy Miller (không liên quan :)) là StructureMap không được thiết lập để tạo các cá thể tại thời gian cấu hình.

Cách giải quyết mà anh ấy đề xuất và tôi đã sử dụng là tạo vùng chứa chỉ dành cho cài đặt. Đây là giải pháp của tôi.

public class SettingsRegistry : Registry 
{ 
    public SettingsRegistry() 
    { 
     ForRequestedType<ISettingsProvider>().TheDefault.Is.OfConcreteType<AppSettingsProvider>(); 

     Scan(s => 
     { 
      s.TheCallingAssembly(); 
      s.With<SettingsScanner>(); 
     }); 
    } 
} 

public class RegistryNeedingSettings : Registry 
{ 
    public RegistryNeedingSettings() 
    { 
     var settingsContainer = new Container(new SettingsRegistry()); 
     var coreSettings = settingsContainer.GetInstance<CoreSettings>(); 

     //configuration needing access to the settings. 
    } 
} 

Tôi đã chuyển mọi cài đặt vào đăng ký riêng và đảm bảo đăng ký cài đặt được định cấu hình trước đăng ký phụ thuộc.

Hy vọng điều này sẽ hữu ích.

Các vấn đề liên quan