POCO - if POCO means pure .net class with only properties, where i can write validations in MVC

后端 未结 3 1435
遇见更好的自我
遇见更好的自我 2021-01-17 03:26

Very new to POCO, find some google links but found many different stories. Some connected with Entity framework, lazy loading etc. Some says its a pure .det class. Atleast

3条回答
  •  萌比男神i
    2021-01-17 03:42

    Personally I like to make my POCOs partial classes with the basic properties needed to define that model and then put and validation logic in a separate class. e.g:

    public partial class Wizard
    {
        public string UserName { get; set; }
        public string EmailAddress { get; set; }
    }
    

    and then if I wanted to add validation to UserName:

    public partial class Wizard
    {
        [Required]
        [StringLength(20)]
        public string UserName { get; set; }
    }
    

    I know the complier just amalgamates the two classes anyway and you may be repeating properties but I think its the cleanest approach.

    Another option is to use the MetadataType attribute.

提交回复
热议问题