GetInstance调用/替代解决方案后如何在Simple Injector中进行注册? [英] How to do a registration in Simple Injector after a GetInstance call / Alternate solution?

查看:126
本文介绍了GetInstance调用/替代解决方案后如何在Simple Injector中进行注册?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下示例:

public class CommunicationClient : IClient
{
    public CommunicationClient(IServerSettings settings) { ... }
    // Code         
}

public class SettingsManager : ISettingsManager
{
    SettingsManager(IDbSettingManager manager)

    // Code
    public IDictionary<string, string> GetSettings() { ... }
}

问题: 在执行注册(使用SimpleInjector)时,我需要提供从SetingsManager实例获得的值并填充ServerSettings实例(IServerSettings的具体类型),但是如果我在注册GetInstance<ISettingsManager> >,这给了我一个我做不到的错误
错误: 在第一次调用GetInstance,GetAllInstances和Verify之后不能更改容器.)

Problem: While performing registrations (using SimpleInjector), I need to provide values that are obtained from an instance of SetingsManager and fill ServerSettings instance (concrete type for IServerSettings) but if I call GetInstance<ISettingsManager> before registering CommunicationClient, it gives me an error that I cannot do that
Error: The container can't be changed after the first call to GetInstance, GetAllInstances and Verify.)

一种解决方案可能是将ISettingsManager作为对CommunicationClient的依赖项注入,但我真的不希望通过它,因为它会为它提供超出要求的信息.

One solution could be to inject ISettingsManager as a dependency to CommunicationClient but I really don't want to pass it as it would provide more than required information to it.

编辑:容器注册

container.Register(typeof(ICommunicationClient), typeof(CommunicationClient));
ISettingsManager settingsManager = container.GetInstance<ISettingsManager>();

string url = settingsManager.GetSetting("url");
string userName = settingsManager.GetSetting("username");
string password = settingsManager.GetSetting("password");

container.Register(typeof(IServerConfiguration), () => 
      new ServerConfiguration(url, userName, password);

关于如何以更简洁的方式实现上述目标的任何建议/替代解决方案?谢谢.

Any suggestions/alternative solutions on how to achieve above in a cleaner way? Thanks.

推荐答案

简单注入器在首次使用后会锁定容器以进行进一步更改.这是一个明确的设计选择,此处 .这意味着您不能在调用GetInstance之后再调用Register,但是永远没有理由这样做.换句话说,总是可以用不需要的方式来重写您的配置.在您的情况下,您的配置可能看起来像这样:

Simple Injector locks the container for further changes after its first use. This is an explicit design choice, which is described here. This means that you can't call Register after you called GetInstance, but there should never be a reason to do this. Or in other words, your configuration can always be rewritten in a way that you don't need this. In your case your configuration will probably look something like this:

var settingsManager = new SettingsManager(new SqlSettingManager("connStr"));

container.RegisterSingle<ISettingsManager>(settingsManager);
container.Register<ICommunicationClient, CommunicationClient>();

string url = settingsManager.GetSetting("url");
string userName = settingsManager.GetSetting("username");
string password = settingsManager.GetSetting("password");

container.Register<IServerConfiguration>(() => 
      new ServerConfiguration(url, userName, password));

在那里您看到SettingsManager不是由容器构建的.使用DI容器时,不需要让DI容器为您构建每个实例.让容器自动连接实例可以减轻组成根的维护负担,并且更容易将跨领域关注点(例如使用装饰器)应用于相关类组.在SettingsManagerSqlSettingsManager类的情况下,它们的构造函数不太可能经常更改,因为这会增加您的合成根"的维护负担.因此,最好一次手动创建这些实例.

There you see that SettingsManager is not built-up by the container. When using a DI container, you are not required to let the DI container build up every instance for you. Letting the container auto-wire instances for you is done to lower the maintenance burden of your Composition Root and makes it easier to apply cross-cutting concerns (using decorators for instance) to groups of related classes. In the case of the SettingsManager and SqlSettingsManager classes, it is very unlikely that their constructor will change that often that it will increase the maintenance burden of your Composition Root. It's therefore perfectly fine to manually create those instances once.

这篇关于GetInstance调用/替代解决方案后如何在Simple Injector中进行注册?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆