2013-09-07 2 views
0

고객 필드에 주소 필드가 있습니다. 내가 연구 한 바로는, 나도 사용 속성 수ASP.NET MVC에서 특성 또는 설정 클래스 사용

public class Customer{ 
    //... 
    [Required] 
    public Address CustomerAddress { get; set; } 
} 

또는 다른 방법 :

public class AddressSettings{ 
    public bool AddressRequired { get; set; } 
    //...other settings 
} 

이 두 가지 유효한 방법이 있습니까? 그렇지 않다면 왜 다른 방향으로 나아질까요? 속성을 사용하여 내 의견으로는

답변

2

는 특성을 이용하여,보다 효율적이고 전문적인 관리가 더, 더 유연하고 읽기 쉽게 그리고 그들은 상자 밖으로 많이 가지고 기본적인 검증을위한

1

기능, 당신은 더 나을 수 있습니다 DataAnnotations.

또 다른 옵션은 FluentValidation (내가보기 엔 추천하는)

당신은 아직 검증을위한 별도의 클래스를 가질 수 있지만,이 방법으로 당신의 ViewModel 속성

[Validator(typeof(PersonValidator))] 
public class Person { 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string Email { get; set; } 
    public int Age { get; set; } 
} 

public class PersonValidator : AbstractValidator<Person> { 
    public PersonValidator() { 
     RuleFor(x => x.Id).NotNull(); 
     RuleFor(x => x.Name).Length(0, 10); 
     RuleFor(x => x.Email).EmailAddress(); 
     RuleFor(x => x.Age).InclusiveBetween(18, 60); 
    } 
} 

와 연결을 강력하게 입력 한 경우 훨씬 더 복잡하고 풍부한 검증 논리를 가질 수 있습니다.

+0

또한 유창한 유효성 검사를 권장합니다. 이는 대형 프로젝트에서 속성 스프를 방지합니다. –

관련 문제