c# - Autofac 运行时参数

标签 c# inversion-of-control ioc-container autofac

我是 autofac 的新手,希望了解将运行时值传递给构造函数的最佳实践。我已经阅读了一堆 stackoverflow 问题,其中提出了这个问题,但没有一个是完全充实的。我们是否应该使用委托(delegate)、工厂来创建服务等。我知道传递容器并不是实现此目的的最佳方式。

在我的特殊情况下,我有一个访问多个依赖项的服务,比如日志记录、数据提供者等。除了传递的少数服务外,我还需要捕获运行时参数,比如用户 ID、密码。 SomeService 需要用户 ID 和密码,并在 Web 查看器执行特定操作时查找它们。以下是我所拥有的,突出显示的是问题。

public class SomeService : ISomeService
{
    private readonly IDataProvider _dataProvider;
    private readonly ILog _log;
    private readonly string _username;  
    private readonly string _password;

    public SomeService(IDataProvider dataProvider, ILog log,
        string username, string password)
    {
      _dataProvider = dataProvider;
      _log = log;
      _username = username;
      _password = password;
    }
}

dataprovider、log在autofac中配置

builder.RegisterType<DataProviderService>().As<IDataProvider>()
builder.RegisterType<SomeLogService>().As<ILog>()

此“SomeService”的大部分功能都需要用户名和密码才能在执行任务之前进行验证,因此认为最好在创建时将其传递给构造函数,但从未处理过 autofac 的运行时要求。我已经审查了问题 Autofac - resolving runtime parameters without having to pass container around它似乎接近我的需要,但需要更多关于实现此目标的最佳方法的反馈。

最佳答案

AutoFac通过Parameterized Instantiation的概念支持解析带有运行时参数的服务.

在依赖于具有特定运行时参数的服务的客户端的构造函数中,将您的依赖项声明为 Func它根据其强类型参数返回该依赖项。

例如Func<string, ISomeService> myService

当 AutoFac 看到 Func它创建一个委托(delegate),充当创建服务的工厂方法。

来自文档:

If type T is registered with the container, Autofac will automatically resolve dependencies on Func as factories that create T instances through the container.

您的依赖项的参数列表中不可能有重复类型,ISomeService 就是这种情况。在你的问题中。例如。 Func<string, string, ISomeService>不管用。在这种情况下,您需要提供 custom delegate factory .

来自文档:

Factory adapters provide the instantiation features of the container to managed components without exposing the container itself to them.

实现这种方法的一种方法是在您的类型定义旁边声明一个委托(delegate)类型,AutoFac 将用作工厂方法。

例如

public class SomeService : ISomeService
{
    // Factory method
    public delegate SomeService Factory(string userName, string password);

    public SomeService(IDataProvider dataProvider,
        ILog log,
        string username,
        string password)
    {
        // ..

您的 ISomeService 客户端将如下所示:

public class MyClient
{
    public MyClient(SomeService.Factory serviceFactory)
    {
        // Resolve ISomeService using factory method
        // passing runtime parameters
        _myService = serviceFactory("this", "that");
    }
}

请注意,服务的所有非运行时参数(在您的情况下为 IDataProvider 和 ILog)将继续由容器自动解析。

关于c# - Autofac 运行时参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25253381/

相关文章:

c# - 响应式(Reactive) UI + WPF : ViewModelViewHost does not stretch inside its parent

c# - 将 MySql 查询保存到 XML 文件并避免重复

inversion-of-control - StructureMap 指定显式构造函数参数

asp.net-mvc - 在混合 ASP.NET WebForms MVC 项目中配置 IoC 容器

c# - 获取继承类型列表并不适用于所有类型

c# - WPF 错误 : Property elements cannot be in the middle of an element's content. 它们必须在内容之前或之后

.net - 将 InterceptionBehaviour 添加到 IoC 容器中注册的所有内容

design-patterns - 构 build 计良好的依赖关系的注意事项

entity-framework - Entity Framework CTP5 和 Ninject 作为我的 IOC

c# - 什么是构造函数解析顺序?