Does ASP.Net MVC 2 validation need some more thought in terms of patterns and use?

后端 未结 5 1271
粉色の甜心
粉色の甜心 2021-02-01 07:09

Here is the lay of the land. Like most people I have my domain object and I have my view models. I love the idea of using view models, as it allows for models to be created spec

5条回答
  •  生来不讨喜
    2021-02-01 07:50

    I've been considering this as well for a while now. I totally understand Brad's reply. However, let's assume I want to use another validation framework that is suitable for annotating both domain entities and view models.

    The only solution I can come up with on paper that still works with attributes would be to create another attribute that "points" to a domain entity's property that you are mirroring in your view model. Here's an example:

    // In UI as a view model.
    public class UserRegistration {
      [ValidationDependency(x => x.FirstName)]
      public string FirstName { get; set; }
    
      [ValidationDependency(x => x.LastName)]
      public string LastName { get; set; }
    
      [ValidationDependency(x => x.Username)]
      public string Username { get; set; }
    
      [ValidationDependency(x => x.Password)]
      public string Password { get; set; }
    }
    

    A framework like xVal could possibly be extended to handle this new attribute and run the validation attributes on the dependency class' property, but with your view model's property value. I just haven't had time to flesh this out more.

    Any thoughts?

提交回复
热议问题