<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Schotime.net &#187; ASP.NET</title>
	<atom:link href="http://schotime.net/blog/index.php/tag/asp-net/feed/" rel="self" type="application/rss+xml" />
	<link>http://schotime.net/blog</link>
	<description>All Things .Net and Me</description>
	<lastBuildDate>Thu, 01 Jul 2010 14:42:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Integrating xVal Validation with Linq-to-Sql</title>
		<link>http://schotime.net/blog/index.php/2009/03/31/integrating-xval-validation-with-linq-to-sql/</link>
		<comments>http://schotime.net/blog/index.php/2009/03/31/integrating-xval-validation-with-linq-to-sql/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 13:58:00 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Validation]]></category>
		<category><![CDATA[xVal]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2009/03/31/integrating-xval-validation-with-linq-to-sql/</guid>
		<description><![CDATA[In a previous post I showed you how you can use xVal and the IDataErrorInfo class to add validation to your Asp.net MVC website. In this post I will extend that to Linq-to-Sql and the classes it generates. The northwind database has a suppliers table. The info contained below is using that table with linq-to-sql. [...]]]></description>
			<content:encoded><![CDATA[<p>In a previous <a href="http://schotime.net/blog/index.php/2009/03/05/validation-with-aspnet-mvc-xval-idataerrorinfo/">post</a> I showed you how you can use xVal and the IDataErrorInfo class to add validation to your Asp.net MVC website. In this post I will extend that to Linq-to-Sql and the classes it generates.</p>
<p>The northwind database has a suppliers table. The info contained below is using that table with linq-to-sql. </p>
<p>After adding the table to the designer, a Supplier class gets constructed in the background. This class is a partial class which means we can add to it without changing the code auto-generated in the designer.cs file.</p>
<p>We can then create a partial class called Supplier inheriting from Custom Validation and add a MetadataType attribute to it. This attribute specifies the class for which use for validating the Supplier class.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td style="background: black" valign="top" width="400">
<pre class="code"><span style="background: black; color: white">    [</span><span style="background: black; color: #ffc66d">MetadataType</span><span style="background: black; color: white">(</span><span style="background: black; color: #cc7832">typeof</span><span style="background: black; color: white">(</span><span style="background: black; color: #ffc66d">SupplierValidation</span><span style="background: black; color: white">))]
</span><span style="background: black; color: #cc7832">    public partial class </span><span style="background: black; color: #ffc66d">Supplier</span><span style="background: black; color: white"> : </span><span style="background: black; color: #ffc66d">CustomValidation</span><span style="background: black; color: #ffc66d">
</span><span style="background: black; color: white">    {
    }</span></pre>
</td>
</tr>
</tbody>
</table>
<p>We can then create the SupplierValidation class specifying the properties of the Supplier class we would like to be validated. For instance here I only want to validate the ContactName and the ContactTitle of the Supplier.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td style="background: black" valign="top" width="400">
<pre class="code"><span style="background: black; color: white">    </span><span style="background: black; color: #cc7832">    public class </span><span style="background: black; color: #ffc66d">SupplierValidation
    </span><span style="background: black; color: white">{
        [</span><span style="background: black; color: #ffc66d">Required</span><span style="background: black; color: white">]
        </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">ContactName { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }

        [</span><span style="background: black; color: #ffc66d">Required</span><span style="background: black; color: white">, </span><span style="background: black; color: #ffc66d">Range</span><span style="background: black; color: white">(</span><span style="background: black; color: #6897bb">0</span><span style="background: black; color: white">, </span><span style="background: black; color: #6897bb">10</span><span style="background: black; color: white">)]
        </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">ContactTitle { </span><span style="background: black; color: #cc7832">get</span><span style="background: black; color: white">; </span><span style="background: black; color: #cc7832">set</span><span style="background: black; color: white">; }
    }</span></pre>
</td>
</tr>
</tbody>
</table>
<p>This specifies that both fields are required and that the ContactTitle cannot be more than 10 characters in length. </p>
<p>The other benefit of using the buddy class here is that if you need to regenerate a table in the linq-to-sql designer, you won’t lose your changes because they’re contained in a separate file.</p>
<p>From here when a Supplier gets passed to in as a parameter on a Controller action it will be validated using the rules in the Supplier Validation class. </p>
<p>Cheers,<br />
  <br />Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2009/03/31/integrating-xval-validation-with-linq-to-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
