简体   繁体   English

将ModelState传递到业务层

[英]Passing ModelState to Business Layer

I am working with an ASP.NET MVC web site and we are utilizing DI to inject the necessary components into our controllers. 我正在使用ASP.NET MVC网站,我们正在利用DI将必要的组件注入到控制器中。

The challenge I have at present is I want to inject the service provider into the controller and have a "UserRequestContext" object injected into the service provider. 我目前面临的挑战是我想将服务提供者注入控制器,并将“ UserRequestContext”对象注入服务提供者。

The UserRequestContext object encapsulates the current users Id, email address, roles and also passes along the modelstate object (or at least, I would like it to). UserRequestContext对象封装了当前用户ID,电子邮件地址,角色,并且还传递了modelstate对象(或者至少是我希望这样做)。 I want to perform all validation operations in my service provider layer. 我想在我的服务提供者层中执行所有验证操作。

The problem of course, is that my service provider object must be instantiated before the controller and because ModelState does not exist until the controller is created, I cannot create the UserRequestContext object. 当然,问题在于我的服务提供者对象必须在控制器之前实例化,并且因为在创建控制器之前不存在ModelState,所以无法创建UserRequestContext对象。

My goal here is to eliminate the need to pass in an IUserRequestContext object to every method of the IServiceProvider. 我的目标是消除将IUserRequestContext对象传递给IServiceProvider的每个方法的需要。

Instead of this: void ServiceProvider.CreateUser(User user, IUserRequestContext userRequestContext); 代替此方法:void ServiceProvider.CreateUser(User user,IUserRequestContext userRequestContext);

Use this: void ServiceProvider.CreateUser(User user) 使用此方法:void ServiceProvider.CreateUser(User user)

Here is the code I've worked up at this point: 这是我目前处理的代码:

public class HomeController
{       
    public HomeController(IServiceProvider provider)
    {
        _provider = provider;
    }

    private IServiceProvider _provider;
}

public class ServiceProvider : IServiceProvider
{
    private IUserRequestContext _userRequestContext;

    public ServiceProvider(IUserRequestContext userRequestContext)
    {
        _userRequestContext = userRequestContext;
    }
}

public class UserRequestContext : IUserRequestContext
{
   private ModelStateDictionary _modelState;

   public UserRequestContext(ModelStateDictionary modelState)
   {
       _modelState = modelState;
   }

   public void AddError(string key, string errorMessage)
   {
       _modelState.AddModelError(key, errorMessage);
   }

   // the rest removed for brevity
}

I don't see anything wrong with this. 我没有发现任何问题。 I've followed a very similar pattern in my code. 我在代码中遵循了非常相似的模式。

I created an IModelState interface that I passed into my service. 我创建了一个传递给服务的IModelState接口。 I created an extension method for ModelStateDictionary that encapsulated it into a help class that implemented the interface. 我为ModelStateDictionary创建了一个扩展方法,将该方法封装到实现该接口的帮助类中。 I could now do this: 我现在可以这样做:

public class mycontroller
{
    private readonly IService _service;

...

    public ActionResult myaction()
    {
        _service.dowork(ModelState.ToWrapper())
 ....

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

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