依赖注入和ModelStateWrapper [英] Dependency Injection and ModelStateWrapper

查看:146
本文介绍了依赖注入和ModelStateWrapper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在教程与产品服务层构造验证服务是这样的:

in tutorial Validating with a service layer constructor for Product Service looks like this:

ProductService(IValidationDictionary validationDictionary, IProductRepository repository)

和其默认的控制器构造实例这样创建:

and its instance in default controller constructor is created like this:

public ProductController() 
{   
    _service = new ProductService(new ModelStateWrapper(this.ModelState), new roductRepository());

}

如果我想使用统一的DI,第二个构造显然应该被使用。

If I want to use Unity for DI, second constructor should obviously be used.

public ProductController(IProductService service)
{
    _service = service;
}

但后来我不知道配置统一注入ProductServise的第一个参数,因为ModelStateWrapper使用的ModelState从控制器,这是内部控制器创建,不能injected.Is有可能注入这种依赖性ProductService?

But then I do not not know to configure Unity to inject first parameter of ProductServise,because ModelStateWrapper uses ModelState from controller, which is created inside controller and cannot be injected.Is it possible to inject such dependency to ProductService?

推荐答案

想。

下面就是你想做什么:


  • 在为了创建产品控制器,你需要的产品服务

  • 在为了创造的产品服务需要产品控制器

你有一个恶性循环,这就是为什么你不能做到这一点。

you have a vicious circle, that's why you can't do it.

我不知道关于实施统一的,但在概念上,你需要打破这个圈子,就像这样:

I dunno about implementation Unity, but conceptually, you need to break the circle, like this:


  • 没有通过产品服务,它创建产品控制器

  • 创建的产品服务,并通过产品控制器的模型状态给它

  • 通过属性注入注入产品服务于产品控制

据我所知团结不支持财产注射,但它需要你把属性上的属性。如果我是你,我会考虑使用微创容器(pretty很多其他更好)。

AFAIK unity does support property injection, but it requires you to put attribute onto the property. If I were you, I'd consider using a less invasive container (pretty much any other is better).

这篇关于依赖注入和ModelStateWrapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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