简体   繁体   English

Autofac:注册类型,其中参数取决于 ApiController 的属性

[英]Autofac: Register type where parameter is dependant on propertie of ApiController

I want to register Repository with Autofac so that it is the same as in parameterless constructor?我想用 Autofac 注册 Repository 以便它与无参数构造函数中的相同吗? Which I then intend to remove.然后我打算删除它。 Example code:示例代码:

public class GroupController : ApiController
{
    protected IRepository Repository;
    protected ISettings Settings;

    protected GroupController()
    {
        Settings = new Settings();
        Repository = new Repository(User.Identity.IsAuthenticated ? Settings.ConfigAuth : Settings.Config)
    }

    public GroupController(ISettings settings, IRepository repository)
    {
        Repository = repository;
        Settings = settings;
    }
}

This is an api controller in .Net framework.这是 .Net 框架中的 api 控制器。 Settings are written in web.config.设置写在 web.config 中。 This is how autofac config looked like before I had only one config.在我只有一个配置之前,这就是 autofac 配置的样子。

var builder = new ContainerBuilder();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
var settings = new Settings();
Builder.RegisterType<Repository>()
   .As<IRepository>()
   .WithParameter(new TypedParameter(typeof(RepoConfig), settings.Config))
   .InstancePerRequest();

There is currently only one implementation of IRepository.目前只有一种 IRepository 实现。

public class Repository : IRepository
{
   private readonly RepoConfig _config;

   public(RepoConfig config)
   {
      _config = config;
   }
}

You can use Lambda Registrations.您可以使用 Lambda 注册。 From Autofac documentation来自 Autofac 文档

You can register components using lambda expressions and make a runtime choice right in the registration for how to handle things.您可以使用 lambda 表达式注册组件,并在注册时就如何处理事情做出运行时选择。 Note this may have an effect on performance depending on the expense of the runtime check and how often it gets executed, but it's an option.请注意,这可能会对性能产生影响,具体取决于运行时检查的费用及其执行频率,但这是一个选项。

http://docs.autofac.org/en/latest/register/registration.html (Check lambda expression components)http://docs.autofac.org/en/latest/register/registration.html (检查 lambda 表达式组件)

Based on your example using Lambda Registrations, you can do autofac reg as below (I have not tested)根据您使用 Lambda 注册的示例,您可以按如下方式执行 autofac reg(我尚未测试)

        var builder = new ContainerBuilder();

        builder.Register(c => new Settings()).As<ISettings>();

        builder.Register<Repository>((c) =>
            {
                ISettings s = c.Resolve<ISettings>();
                Settings repoSettings = User.Identity.IsAuthenticated ? s.ConfigAuth : s.Config;
                return new Repository(repoSettings);
            }
        )
       .As<IRepository>()
       .InstancePerRequest();

        Container = builder.Build();

Then remove your parameter-less constructor and add然后删除您的无参数构造函数并添加

public GroupController(IRepository repository)
{
    Repository = repository;
}

hope it helps.希望能帮助到你。

I implemented an workaround for this implementation.我为这个实现实现了一个解决方法。 Not the best way but it works.不是最好的方法,但它有效。 This way initialization of Repository doesn't need parameter and I set it with a propertie in constructor of Controller.这样 Repository 的初始化不需要参数,我在 Controller 的构造函数中设置了一个属性。

public class GroupController : ApiController
{
    protected IRepository Repository;
    protected ISettings Settings;

    public GroupController(ISettings settings, IRepository repository)
    {
        Settings = settings;
        Repository = repository;
        Repository.RepoConfig = User.Identity.IsAuthenticated ? Settings.ConfigAuth : Settings.Config
    }
}

public class Repository : IRepository
{
   private readonly RepoConfig _config;

   public RepoConfig RepoConfig
   {
       get => _config;
       set
       {
           _config = value;
       }
   }
   ...
}

And for autofac there is standard DI refister:对于 autofac,有标准的 DI 寄存器:

var builder = new ContainerBuilder();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
Builder.RegisterType<Repository>()
   .As<IRepository>()
   .InstancePerRequest();

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

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