简体   繁体   English

使用服务层验证 asp.net core mvc 2.2

[英]asp.net core mvc 2.2 validating with a service layer

I've found this documentation which is actually what I'm looking for, but I'm having some issues with it: https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/models-data/validating-with-a-service-layer-cs我发现这个文档实际上是我正在寻找的,但我遇到了一些问题: https : //docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions- 1/models-data/validating-with-a-service-layer-cs

When decoupling the service layer the code on the website does this:在解耦服务层时,网站上的代码是这样做的:

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

When I try to translate this to my asp.net core mvc 2.2 project with service I have:当我尝试将其转换为带有服务的 asp.net core mvc 2.2 项目时,我有:

// ctor from service
  public ProductService(IProductRepository repo, IValidationDictionary validationDictionary) : IProductService {
 // ...
}

// ctor from controller
public ProductController(IProductService service) {

}


  public class ModelStateWrapper : IValidationDictionary
  {
    private ModelStateDictionary _ModelState;

    public ModelStateWrapper(ModelStateDictionary modelState)
    {
      _ModelState = modelState;
    }

    #region IValidationDictionary members

    public bool IsValid => _ModelState.IsValid;

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

    #endregion
  }

The problem I have is that I cannot inject the IValidationDictionary validationDictionary parameter.我遇到的问题是我无法注入 IValidationDictionary validationDictionary 参数。

Now I have to manually pass a parameter new ModelStateWrapper(this.ModelState) each time I call a method from the service that requires validation.现在,每次从需要验证的服务调用方法时,我都必须手动传递参数new ModelStateWrapper(this.ModelState) (can't seem set it once because of async service methods) (由于异步服务方法,似乎无法设置一次)

Is this something that could be solved?这是可以解决的事情吗? As I like the idea of having the validation in the service.因为我喜欢在服务中进行验证的想法。

If I could figure out how to pass a parameter of the object to the newly created instance, that would be great:如果我能弄清楚如何将对象的参数传递给新创建的实例,那就太好了:

// ctor from controller
public ProductController(IProductService service[use this.ModelState here and pass it to the constructor of the service]) {

}

Here is how I did it with .NET Core 5.0这是我如何使用 .NET Core 5.0 做到的

In Startup.cs在 Startup.cs 中

public void ConfigureServices(IServiceCollection services)
{
    ...
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
    ...
}

In Controller在控制器中

AccountService _service;

public AccountController(IActionContextAccessor actionContextAccessor)
{
      //this.actionContextAccessor = actionContextAccessor;
      _service = new AccountService(actionContextAccessor);
 }

In Service Layer class在服务层类中

public class AccountService
    {    
        private readonly IActionContextAccessor _actionContextAccessor;
        public AccountService(IActionContextAccessor actionContextAccessor)
        {
            _actionContextAccessor = actionContextAccessor;
        }

        public void Login(string emailAddress, string password)
        {  
            _actionContextAccessor.ActionContext.ModelState.AddModelError("Email", "Your error message");
        }
    }

In Action, you use like在行动中,你使用像

   _service.Login(model.Email, model.Password);

  if(!ModelState.IsValid)
      return View(model);

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

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