控制器的ModelState与ModelStateWrappper [英] Controller ModelState with ModelStateWrappper
问题描述
HI,所有的,我是用结构图来实现依赖注入。我创建ModelStateWrapper类在服务层,它在构造函数中得到参考控制器的ModelState发送模式状态。在bootswrapper我注册了我喜欢的类型:
to all, i am use Structure Map to implement dependency-injection. I created ModelStateWrapper class to send Model state in service layer, which in constructor get reference to ModelState of controller. In bootswrapper i registered my type:
ForRequestedType<ISourceService>()
.TheDefaultIsConcreteType<SourceService>();
ForRequestedType<IValidationDictionary>()
.TheDefaultIsConcreteType<ModelStateWrapper>();
我怎么可以给控制器的模型状态的参考这里ModelStateWrapper?
How i can give reference of controller's model state to ModelStateWrapper here ?
P.S。对不起,我的英语:)
p.s. sorry for my english :)
推荐答案
您需要提供更多的信息,但是这是我最好的猜测,你有什么:
You need to provide more information, but this is my best guess as to what you have:
public class ModelStateWrapper : IValidationDictionary
{
...
private readonly ModelState _modelState;
public ModelStateWrapper(ModelState modelState)
{
_modelState = modelState;
}
...
}
如果你想给一个变量(在这种情况下,控制器的模型状态)传递给ModelStateWrapper你几乎肯定需要通过调用的ObjectFactory做明确。
If you want to pass a variable (the controller's model state in this case) to the ModelStateWrapper you almost certainly need to do that explicitly by calling the ObjectFactory.
例如:
MyController : Controller
{
...
public MyAction()
{
...
IValidationDictionary validationDictionary = ObjectFactory
.With<ModelState>(this.ModelState)
.GetInstance<IValidationDictionary>();
...
}
...
}
请参阅本文档的详细信息:
See this documentation for details:
在运行时将参数传递给StructureMap
Passing Arguments to StructureMap at Runtime
这篇关于控制器的ModelState与ModelStateWrappper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!