管理构造函数依赖注入 (MS Unity)
我正在构建一个多层应用程序,并尝试尽可能保留各层,因此我使用 IoC 容器来实现此目的。无论如何,我正在尝试扩展这篇文章 将我的业务逻辑验证移至服务层。我设法解决了除 ModelStateWrapper 类对 ModelState 本身的依赖之外的所有依赖问题。以下是我的类:
public interface IValidationDictionary
{
void AddError(string key, string errorMessage);
bool IsValid { get; }
}
public class ModelStateWrapper : IValidationDictionary
{
private ModelStateDictionary _modelState;
public ModelStateWrapper(ModelStateDictionary modelState)
{
_modelState = modelState;
}
public void AddError(string key, string errorMessage)
{
_modelState.AddModelError(key, errorMessage);
}
public bool IsValid
{
get { return _modelState.IsValid; }
}
}
ModelStateWrapper 类位于我的 MVC3 应用程序的 Services 文件夹中。虽然 IValidationDictionary 位于我的服务层内的抽象文件夹内。在我的 Unity 配置中,我执行了以下操作:
.RegisterType<IValidationDictionary, ModelStateWrapper>(
new HttpContextLifetimeManager<IValidationDictionary>())
那么,现在我可以做些什么来使用 IoC 将 ModelState 对象注入到 ModelStateWrapper 类中吗?或者我是否必须在控制器中显式/手动实例化 ModelStateWrapper 并将 ModelState 作为参数传递?
提前致谢!
I am building a multi-layered application and trying to keep the layers as much as possible so I'm using an IoC container for this purpose. Anyway, I'm trying to expand on this article to move my business logic validation into the service layer. I managed to resolve all dependency issues except the dependency of the ModelStateWrapper class on the ModelState itself. Here are my classes:
public interface IValidationDictionary
{
void AddError(string key, string errorMessage);
bool IsValid { get; }
}
public class ModelStateWrapper : IValidationDictionary
{
private ModelStateDictionary _modelState;
public ModelStateWrapper(ModelStateDictionary modelState)
{
_modelState = modelState;
}
public void AddError(string key, string errorMessage)
{
_modelState.AddModelError(key, errorMessage);
}
public bool IsValid
{
get { return _modelState.IsValid; }
}
}
The ModelStateWrapper class resides in a Services folder in my MVC3 application. While the IValidationDictionary is inside an Abstract folder inside my Services layer. In my Unity configuration I did the following:
.RegisterType<IValidationDictionary, ModelStateWrapper>(
new HttpContextLifetimeManager<IValidationDictionary>())
So, now is there anything I could do to inject the ModelState object into the ModelStateWrapper class using the IoC? Or do I have to explicitly/manually instantiate the ModelStateWrapper in the controllers and pass in the ModelState as an argument?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要将 modelstatewrapper 类移至公共程序集。您可以从服务层、业务逻辑层等引用此公共程序集。公共程序集可以包含域类、dto、服务定义等。您创建一个引导程序类,它将公共程序集中的所有类型注册到容器中。从服务、BL 层等调用此引导程序。
我希望这有
帮助
I think you need to move your modelstatewrapper class to a common assembly. You can reference this common assembly from your service layer, business logic layer, etc. The common assembly can contain your domain classes, dto's, service definitions, etc etc You create a bootstrapper class which registers all types from the common assembly into your container. Call this bootstrapper from the service, BL layer, etc.
I hope this helps
Greetings