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?