Asp.NET MVC - Validate a Model Outside of the Controller

后端 未结 4 1220
萌比男神i
萌比男神i 2021-02-04 08:03

I am building an ASP.NET MVC project and going for the following Architecture:

  • A Core project that has models, validation, dto, logic, etc.
  • <
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-04 09:04

    I usually keep my view models in the Web project and do the input validation in the controller using the ModelState property. If this succeeds, I map them to domain models (which live in the Core layer) and send those to my services in the Service (could also be Core) layer. The service layer validates business rules and if it succeeds it calls a repository to perform the desired action and returns the result of the operation to the controller.

    Keeping your view models in the Web project also allows you to use MVC validation attributes such as RemoteAttribute.

    I'm not saying that your design smells, but I do think it's good to separate presentation logic from your Core layer.

提交回复
热议问题