简体   繁体   English

简单注入器有条件地向代表注册

[英]Simple Injector Conditionally Register With Delegates

I'm writing an app that makes use of classes defined by the following interface: 我正在编写一个使用以下接口定义的类的应用程序:

public interface ICredentialProvider
{
    string GetUsername();
    string GetPassword();
}

This is done so methods can be called to retrieve the username/password instead of it being statically held in memory and defined in code/config file. 这样做是为了可以调用方法来检索用户名/密码,而不是将其静态保存在内存中并在代码/配置文件中定义。

Currently in development I'm using this very simple implementation: 当前正在开发中,我正在使用这个非常简单的实现:

public class FakedCredentialProvider : ICredentialProvider
{
    private readonly string _username;
    private readonly string _password;

    public FakedCredentialProvider(string username, string password)
    {
        _username = username;
        _password = password;
    }

    public string GetUsername()
    {
        return _username;
    }

    public string GetPassword()
    {
        return _password;
    }
}

...which accepts two strings in the constructor. ...在构造函数中接受两个字符串。 Future (actual) implementations of ICredentialProvider will likely need to accept certain strings for connecting to the appropriate external password vault or service (or possibly both; at this point in the project that's unclear). ICredentialProvider未来(实际)实现可能将需要接受某些字符串以连接到适当的外部密码保险库或服务(或可能是两者;这在项目中尚不清楚)。

Further, the project uses multiple instances of ICredentialProvider for the various accounts it needs to connect to (such as SharePoint, ActiveDirectory, and Windows Graph). 此外,该项目将ICredentialProvider多个实例用于其需要连接到的各种帐户(例如SharePoint,ActiveDirectory和Windows Graph)。

To these ends I know how to register via a delegate: 为此,我知道如何通过代理进行注册:

container.RegisterWebApiRequest(() =>
    new FakedCredentialProvider(fakedSharePointUsername, fakedSharePointPassword));

...And how to conditionally register... ...以及如何有条件地注册...

container.RegisterConditional(typeof(ICredentialProvider), typeof(FakedCredentialProvider),
        context =>
        context.Consumer.Target.Name.ToLower().Contains("sharepoint")
    );

...But not how to combine the two. ...但不是如何结合两者。

How does one combine these two approaches? 如何将这两种方法结合起来? Or alternatively: Is there an better design for situations such as these? 或者:在这种情况下是否有更好的设计?

I'm not sure whether I fully understand your constraints and design, but the way to do registration of conditional delegates is using one of the RegisterConditional overloads that accepts a Registration object: 我不确定我是否完全了解您的约束和设计,但是进行条件委托注册的方法是使用接受Registration对象的RegisterConditional重载之一:

var container = new Container();
container.Options.DefaultScopedLifestyle = new WebApiRequestLifestyle();

container.RegisterConditional(typeof(ICredentialProvider),
    Lifestyle.Scoped.CreateRegistration(() => new FakedCredentialProvider(user, pwd), 
        container),
    c => c.Consumer.Target.Name.ToLower().Contains("sharepoint"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM