作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何在 StructureMap Registy 构造函数中获取某种类型的实例(在不同的注册表中注册)?我想使用这样的代码:
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));
}
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
最佳答案
我今天遇到了同样的问题。 Jeremy Miller 的回答(没有关系 :))是 StructureMap 未设置为在配置时创建实例。
他推荐和我使用的解决方法是为设置创建一个容器。这是我的解决方案。
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.
}
}
关于.net - 如何在 StructureMap Registy 构造函数中获取实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1282521/
我是一名优秀的程序员,十分优秀!