5
如何在StructureMap註冊構造函數中獲取某種類型的實例(在不同的註冊表中註冊)?我想要使用這樣的代碼:如何在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是應用程序設置的接口。實現此接口(目前ConfigFileApplicationSettings類)的具體類型註冊在另一個註冊表這樣的:
public ApplicationServicesRegistry()
{
ForRequestedType<IApplicationSettings>().TheDefault.IsThis(
new 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我有一個錯誤(當然)。我沒有完全理解如何正確使用StructureMap。可能是我應該做一些不同的方式。但無論如何,我可以獲得一個在註冊表構造函數中提前註冊的某種類型的實例嗎?