내 ViewModel에 비어 있지 않은지 확인하는 유효성 검사 속성이 있습니다. - 충분합니까? 아니면 내가 작성한 코드 계약도 ActionResult에 포함시켜야합니까?MVC + 유효성 검사 속성이 충분합니까?
예 :
// CreateCaseViewModel.cs
public class CreateCaseViewModel
{
[Required]
public string Topic { get; set; }
[Required]
public string Message { get; set; }
}
// CaseController.cs
[AuthWhere(AuthorizeRole.Developer)]
[HttpPost]
public ActionResult Create(CreateCaseViewModel model)
{
if(!ModelState.IsValid)
{
// TODO: some cool stuff?
}
if (string.IsNullOrWhiteSpace(model.Message))
{
throw new ArgumentException("Message cannot be null or empty", model.Message);
}
if (string.IsNullOrWhiteSpace(model.Topic))
{
throw new ArgumentException("Topic cannot be null or empty", model.Topic);
}
var success = false;
string message;
var userId = new Guid(_membershipService.GetUserByUserName(User.Identity.Name).ProviderUserKey.ToString());
if(userId == Guid.Empty)
{
throw new ArgumentException("UserId cannot be empty");
}
Case createCase = _caseService.CreateCase(model.Topic, model.Message);
if(createCase == null)
{
throw new ArgumentException("Case cannot be null");
}
if(_caseService.AddCase(createCase, userId))
{
message = ControllerResources.CaseCreateFail;
}
else
{
success = true;
message = ControllerResources.CaseCreateSuccess;
}
return Json(new
{
Success = success,
Message = message,
Partial = RenderPartialViewToString(ListView, GetCases)
});
}
아, 고마워요! 하지만 내 viewmodel의 유효성 검사 속성이 동일하더라도 여전히 ActionResult에 if 문을 codecontract해야한다고 생각합니까? – ebb