Javascript가 동작하지 않는 브라우져나 서버단에 치명적인 오류가 들어갈 수 있는 값의 경우에는 Server단에서의 Validation Check가 반드시 필요하게 된다. 전에 사용했던 IDataError interface의 경우에는 데이터 모델들의 처리가 되는데. 이는 일반적인 C#에서의 에러처리가 아닌 ASP .NET MVC에서의 Validateion 처리라고 조금은 한정지어서 생각할 수 있다.
생각해보면 C# 언어에 가장 맞는 Validation Check는 try~catch 를 이용한 방법이다. 이러한 방법을 이용해야지만이 만약 Entity를 다른 환경, 예를 들어 SilverLight에서 Entity를 표현하거나 WinForm으로 Entity를 표현할 때에 정확한 Validation Check가 가능하게 된다.
다음은 Product에 대한 Validation check code이다.
Product을 사용하는 Controller의 예
생각해보면 C# 언어에 가장 맞는 Validation Check는 try~catch 를 이용한 방법이다. 이러한 방법을 이용해야지만이 만약 Entity를 다른 환경, 예를 들어 SilverLight에서 Entity를 표현하거나 WinForm으로 Entity를 표현할 때에 정확한 Validation Check가 가능하게 된다.
다음은 Product에 대한 Validation check code이다.
001.
public
class
Product
002.
{
003.
public
string
Name {
get
;
set
; }
004.
public
string
Price {
get
;
set
; }
005.
public
string
Description {
get
;
set
; }
006.
007.
public
void
CheckValidation()
008.
{
009.
ProductException exception =
new
ProductException();
010.
if
(
string
.IsNullOrWhiteSpace(Name))
011.
{
012.
exception.Add(
"name"
,
"Name is empty"
);
013.
}
014.
015.
if
(
string
.IsNullOrWhiteSpace(Description))
016.
{
017.
exception.Add(
"description"
,
"Description is empty"
);
018.
}
019.
020.
if
(
string
.IsNullOrEmpty(Price))
021.
{
022.
exception.Add(
"Price"
,
"Price is empty"
);
023.
}
024.
025.
if
(!exception.IsValid)
026.
{
027.
throw
exception;
028.
}
029.
}
030.
}
031.
032.
public
class
ProductException : Exception, IDictionary<
string
,
string
=
""
>
033.
{
034.
public
bool
IsValid {
get
{
return
_exceptMessages.Count == 0; } }
035.
private
Dictionary<
string
,
string
=
""
> _exceptMessages;
036.
public
ProductException()
037.
{
038.
_exceptMessages =
new
Dictionary<
string
,
string
=
""
>();
039.
}
040.
041.
#region IDictionary<string,string> Members
042.
043.
public
void
Add(
string
key,
string
value)
044.
{
045.
_exceptMessages.Add(key, value);
046.
}
047.
048.
public
bool
ContainsKey(
string
key)
049.
{
050.
return
_exceptMessages.ContainsKey(key);
051.
}
052.
053.
public
ICollection<
string
> Keys
054.
{
055.
get
{
return
_exceptMessages.Keys; }
056.
}
057.
058.
public
bool
Remove(
string
key)
059.
{
060.
return
_exceptMessages.Remove(key);
061.
}
062.
063.
public
bool
TryGetValue(
string
key,
out
string
value)
064.
{
065.
return
_exceptMessages.TryGetValue(key,
out
value);
066.
}
067.
068.
public
ICollection<
string
> Values
069.
{
070.
get
{
return
_exceptMessages.Values; }
071.
}
072.
073.
public
string
this
[
string
key]
074.
{
075.
get
076.
{
077.
return
_exceptMessages[key];
078.
}
079.
set
080.
{
081.
_exceptMessages[key] = value;
082.
}
083.
}
084.
085.
#endregion
086.
087.
#region ICollection<keyvaluepair<string,string>> Members
088.
089.
public
void
Add(KeyValuePair<
string
,
string
=
""
> item)
090.
{
091.
_exceptMessages.Add(item.Key, item.Value);
092.
}
093.
094.
public
void
Clear()
095.
{
096.
_exceptMessages.Clear();
097.
}
098.
099.
public
bool
Contains(KeyValuePair<
string
,
string
=
""
> item)
100.
{
101.
return
_exceptMessages.Contains(item);
102.
}
103.
104.
public
void
CopyTo(KeyValuePair<
string
,
string
=
""
>[] array,
int
arrayIndex)
105.
{
106.
throw
new
NotImplementedException();
107.
}
108.
109.
public
int
Count
110.
{
111.
get
{
return
_exceptMessages.Count; }
112.
}
113.
114.
public
bool
IsReadOnly
115.
{
116.
get
{
throw
new
NotImplementedException(); }
117.
}
118.
119.
public
bool
Remove(KeyValuePair<
string
,
string
=
""
> item)
120.
{
121.
return
_exceptMessages.Remove(item.Key);
122.
}
123.
124.
#endregion
125.
126.
#region IEnumerable<keyvaluepair<string,string>> Members
127.
128.
public
IEnumerator<keyvaluepair<
string
,
string
=
""
>> GetEnumerator()
129.
{
130.
return
_exceptMessages.GetEnumerator();
131.
}
132.
133.
#endregion
134.
135.
#region IEnumerable Members
136.
137.
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
138.
{
139.
return
_exceptMessages.GetEnumerator();
140.
}
141.
142.
#endregion
143.
}
144.
</keyvaluepair<
string
,></keyvaluepair<
string
,
string
></
string
,></
string
,></
string
,></
string
,></keyvaluepair<
string
,
string
></
string
></
string
></
string
,
string
></
string
,></
string
,></
string
,>
01.
[AcceptVerbs(HttpVerbs.Post)]
02.
public
ActionResult New(Product productValue)
03.
{
04.
try
05.
{
06.
productValue.CheckValidation();
07.
}
08.
catch
(ProductException ex)
09.
{
10.
foreach
(
string
key
in
ex.Keys)
11.
{
12.
ModelState.AddModelError(key, ex[key]);
13.
}
14.
return
View();
15.
}
16.
17.
return
RedirectToAction(
"NewResult"
,
18.
new
{ product = productValue });
19.
}