gpt4 book ai didi

.net - 如何在 StructureMap Registy 构造函数中获取实例?

转载 作者:行者123 更新时间:2023-12-04 14:21:10 25 4
gpt4 key购买 nike

如何在 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));

}

IApplicationSettings 是应用程序设置的接口(interface)。实现此接口(interface)的具体类型(当前为 ConfigFileApplicationSettings 类)在另一个注册表中注册,如下所示:
    public ApplicationServicesRegistry()
{
ForRequestedType<IApplicationSettings>().TheDefault.IsThis(
new ConfigFileApplicationSettings());
}

并且两个注册表都在 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

当我尝试在注册表构造函数中获取 IApplicationRegisty 的实例时,我遇到了错误(当然)。我不完全了解如何以正确的方式使用 StructureMap。也许我应该以不同的方式做这件事。但无论如何,我可以在 Registry 构造函数中获得某种类型的早期注册实例吗?

最佳答案

我今天遇到了同样的问题。 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com