Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EmptyMvcApplication.Models;
using DomainModel.Concrete;
using DomainModel.Entity;
using DomainModel.Abstract;
using DomainModel;
namespace EmptyMvcApplication.Controllers
{
public class ValidController : Controller
{
IUser user = new UserImplement();
public ActionResult Index()
{
return View(UserImplement.user);
}
public ViewResult Create()
{
return View();
}
[HttpPost]
public ViewResult Create(User user)
{
try
{
this.user.Create(user);
}
catch (RuleException re)
{
re.CopyToModelState(ModelState);
}
return ModelState.IsValid ? View("Index", UserImplement.user) : View();
}
}
}
View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<!DOCTYPE html>
<html>
<head runat="server">
<meta name="viewport" content="width=device-width" />
<title>Create</title>
<style>
.input-validation-error
{
border: 1px solid red;
}
</style>
</head>
<body>
<div>
<% using (Html.BeginForm())
{ %>
<fieldset>
<legend>사용자 정보</legend>
<p>
<label for="Name">사용자 이름</label>
<%: Html.TextBox("Name")%>
<%: Html.ValidationMessage("Name")%>
</p>
<p>
<label for="ID">사용자 아이디</label>
<%: Html.TextBox("ID")%>
<%: Html.ValidationMessage("ID")%>
</p>
<p>
<label for="Password">사용자 비밀번호</label>
<%: Html.TextBox("Password")%>
<%: Html.ValidationMessage("Password")%>
</p>
<p>
<input type="submit" value="전송" />
</p>
</fieldset>
<% } %>
</div>
</body>
</html>
Entity
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
namespace DomainModel.Entity
{
public class User
{
public string Name { get; set; }
public string ID { get; set; }
public string Password { get; set; }
public NameValueCollection GetCreateValid()
{
NameValueCollection errors = new NameValueCollection();
if(string.IsNullOrEmpty(this.Name))
{
errors.Add("Name", "이름을 입력해주세요.!");
}
if(string.IsNullOrEmpty(this.ID))
{
errors.Add("ID", "아이디를 입력해주세요.!");
}
if(string.IsNullOrEmpty(this.Password))
{
errors.Add("Password", "비밀번호를 입력해주세요.!");
}
return errors;
}
}
}
Interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainModel.Entity;
namespace DomainModel.Abstract
{
public interface IUser
{
bool Create(User user);
User Read();
User Update();
User Delete();
}
}
Interface 구현 코드
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainModel.Abstract;
using DomainModel.Entity;
using System.Collections.Specialized;
namespace DomainModel.Concrete
{
public class UserImplement : IUser
{
public static List<User> user = new List<User>() {new User { Name = "장정훈", ID = "aswqertrt", Password = "987213" }};
public bool Create(User user)
{
NameValueCollection errors = user.GetCreateValid();
if (errors.Count > 0)
{
throw new RuleException(errors);
}
else
{
//Todo: DB에 사용자 정보를 입력한다.
UserImplement.user.Add(new User { Name = user.Name, ID = user.ID, Password = user.Password });
return true;
}
}
public Entity.User Read()
{
throw new NotImplementedException();
}
public Entity.User Update()
{
throw new NotImplementedException();
}
public Entity.User Delete()
{
throw new NotImplementedException();
}
}
}
RuleException
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
namespace DomainModel
{
public class RuleException : Exception
{
public NameValueCollection Errors { get; set; }
public RuleException(string key, string value)
{
this.Errors = new NameValueCollection { { key, value } };
}
public RuleException(NameValueCollection errors)
{
this.Errors = errors;
}
}
}
RuleException 확장 메서드
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DomainModel;
namespace EmptyMvcApplication.Models
{
public static class RuleExceptionExtension
{
public static void CopyToModelState(this RuleException re, ModelStateDictionary modelstate)
{
foreach(string key in re.Errors)
{
foreach(string value in re.Errors.GetValues(key))
{
modelstate.AddModelError(key, value);
}
}
}
}
}