如何通过控制器的ModelState与Autofac我的服务的构造? [英] How to pass controller's ModelState to my service constructor with Autofac?

查看:119
本文介绍了如何通过控制器的ModelState与Autofac我的服务的构造?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ModelStateDictionary包装这我的所有服务接受。是否有可能配置autofac到控制器ModelStateDictionary注入包装的构造函数,然后将其注入服务的构造函数?

I have a wrapper on ModelStateDictionary which all my services accept. Is it possible to configure the autofac to inject the controller ModelStateDictionary into the constructor of the wrapper and then inject it into service constructor?

//code
public class ModelValidation : IModelValidation { 
public ModelValidation(ModelStateDictionary msd){...}
..
..
}

public class CustomerService{
public CustomerService(IModelValidation mv){...}
..
}

感谢

推荐答案

根据您的意见,我在此修改我的答案:)

Based on your comments I hereby revise my answer :)

ModelStateDictionary 显然不是的服务应该由容器来解决,而是数据这应该是在实例化时提供。我们可以知道从ModelState中由每个控制器实例拥有,因此不向容器在解析时间的事实。

ModelStateDictionary is clearly not a service that should be resolved by the container, but rather data that should be provided at instantiation time. We can tell that from the fact that ModelState is owned by each Controller instance and is thus not available to the container at "resolve time".

此外,每个 ModelValidation 实例将被绑定到一个 ModelStateDictionary 实例,因此也被认为是< STRONG>数据

Furthermore, each ModelValidation instance will be bound to a ModelStateDictionary instance and is thus also to be considered as data.

在Autofac,当数据必须被传递给构造函数(可选除了其他依赖),我们必须使用厂家代表。这些代表将处理依赖和数据传递给构造函数。甜蜜的事情与Autofac是,这些代表可自动生成。

In Autofac, when data must be passed to constructors (optionally in addition to other dependencies), we must use factory delegates. These delegates will handle dependency and data passing to the constructor. The sweet thing with Autofac is that these delegates can be autogenerated.

我提出以下解决方案:

由于两个ModelValidation并为CustomerService需要它们的构造的数据,我们需要两个工厂代表(注:该参数名称必须在相应的构造函数的名称相匹配):

Since both ModelValidation and CustomerService requires data in their constructors, we need two factory delegates (note: the parameter names must match the names in their corresponding constructor):

public delegate IModelValidation ModelValidationFactory(ModelStateDictionary msd);
public delegate CustomerService CustomerServiceFactory(ModelStateDictionary msd);

由于你的控制器不应该知道这些代表来自他们应该传递给控制器​​构造函数的依赖关系:

Since your controllers shouldn't know where these delegates comes from they should be passed to the controller constructor as dependencies:

public class EditCustomerController : Controller
{
    private readonly CustomerService _customerService;

    public EditCustomerController(CustomerServiceFactory customerServiceFactory
        /*, ...any other dependencies required by the controller */
          )
    {
        _customerService = customerServiceFactory(this.ModelState);
    }

}

本的CustomerService应该有一个类似的构造函数(可以处理一些这在ServiceBase类):

The CustomerService should have a constructor similar to this (optionally handle some of this in a ServiceBase class):

public class CustomerService
{
    private readonly IModelValidation _modelValidation;

    public CustomerService(ModelStateDictionary msd,
              ModelValidationFactory modelValidationFactory)
    {
        _modelValidation = modelValidationFactory(msd);
    }

要做到这一点,我们需要建立我们的容器是这样的:

To make this happen we need to build our container like this:

var builder = new ContainerBuilder();

builder.Register<ModelValidation>().As<IModelValidation>().FactoryScoped();
builder.Register<CustomerService>().FactoryScoped();

builder.RegisterGeneratedFactory<ModelValidationFactory>();
builder.RegisterGeneratedFactory<CustomerServiceFactory>();

builder.Register<EditCustomerController>().FactoryScoped();

所以,当控制器解决(例如,使用的MvcIntegration模块)工厂代表将被注入到控制器和服务。

So, when the controller is resolved (eg when using the MvcIntegration module), the factory delegates will be injected into the controllers and services.

更新:以减少需要code甚至更多,你可以替换 CustomerServiceFactory 与通用厂委托我好象 href=\"http://peterspattern.com/generate-generic-factories-with-autofac\">。

Update: to cut down on required code even more, you could replace CustomerServiceFactory with a generic factory delegate like I've described here.

这篇关于如何通过控制器的ModelState与Autofac我的服务的构造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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