Autofac 运行时参数 [英] Autofac runtime parameters

查看:28
本文介绍了Autofac 运行时参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to autofac and looking to see the best practices on passing runtime values to constructor. I've read a bunch of stackoverflow questions where this is asked but none are fully fleshed out. Should we be using delegates, factory to create service etc. I know passing the container around is not the best way to accomplish this.

在我的特殊情况下,我有一个服务可以访问多个依赖项,例如日志记录、数据提供程序等.除了传递的少数服务之外,我还有需要捕获的运行时参数,例如用户 ID、密码.SomeService 需要用户 ID 和密码,并且在 Web 查看器执行特定操作时会查找这些用户 ID 和密码.以下是我所拥有的,并强调了这个问题.

In my particular case I have a service that access multiple dependencies, say logging, dataprovider, etc. Along with the few services being passed I also have run-time parameters I need to capture, say userid, password. The userid and password are required for the SomeService and are looked up when a web viewer performs a particular action. Below is what I have and highlighted is the issue.

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 中配置

The dataprovider, and log are configured in autofac

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

这个SomeService"的大部分功能在执行任务之前需要用户名和密码来验证,所以认为最好在创建时传递给构造函数,但从未处理过 autofac 的运行时要求.我已经查看了问题 ​​Autofac - 无需解析运行时参数不得不传递容器,这似乎与我需要的很接近,但需要更多关于实现此目标的最佳方法的反馈.

Most of the functionality of this "SomeService" requires a username and password to verify before performing tasks, so figured it best to pass into constructor when creating but have never dealt with run-time requirements for autofac. I've reviewed the question Autofac - resolving runtime parameters without having to pass container around and it seems close to what I need but need some more feedback on the best way to accomplish this.

推荐答案

一般来说你应该 防止将运行时值传递给构造函数.这将使您的设计和 DI 配置复杂化很多.构造函数用于依赖项和配置值.通过方法参数传递运行时值或注入允许您检索这些运行时值的服务.以允许检索当前登录用户的名称的 IUserContext 服务为例.

In general you should prevent passing runtime values into constructors. That will complicate your design and your DI configuration a lot. Constructors are for dependencies and configuration values. Pass runtime values either through method arguments or inject a service that allows you to retrieve those runtime values. Take for instance an IUserContext service that allows retrieving the name current logged in user.

这篇关于Autofac 运行时参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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