2009-08-15 2 views
5

StructMap 등록자 생성자에서 (다른 레지스트리에 등록 된) 일부 유형의 인스턴스를 얻으려면 어떻게해야합니까?StructureMap 등록자 생성자에서 인스턴스를 가져 오는 방법은 무엇입니까?

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이 응용 프로그램 설정에 대한 인터페이스입니다 : 나는 그런 코드를 사용하고 싶습니다.

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

그리고 두 레지스트리는 부트 스트 래퍼에 등록 :이 인터페이스 (현재 ConfigFileApplicationSettings 클래스)를 구현하는 구체적인 유형은 다음과 같이 다른 레지스트리에 등록되어 내가의 인스턴스를 얻으려고

 #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 

IApplicationRegisty in registry constructor 오류가 발생했습니다. 나는 StructMap을 올바른 방법으로 사용하는 방법을 철저히 이해하지 못합니다. 나는 그 일을 다른 방식으로해야 할 수도 있습니다. 그러나 어쨌든 어떤 유형의 인스턴스를 Registry 생성자에 일찍 등록 할 수 있습니까?

답변

7

오늘 같은 문제가 발생했습니다. Jeremy Miller (관계 없음 :)의 대답은 ConfigurationMap이 구성시 인스턴스를 만들도록 설정되어 있지 않다는 것입니다.

그가 권장 해결 방법을 내가 사용되는 단지 설정에 대한 컨테이너를 만드는 것이 었습니다. 여기 내 해결책이있다.

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. 
    } 
} 

모든 설정을 자체 레지스트리로 이동하고 설정 레지스트리가 종속 레지스트리보다 먼저 구성되도록했습니다.

희망이 도움이됩니다.

관련 문제