<?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>&#60; web:Blog x:Name=&#34;Brian Lagunas&#34; &#47;&#62; &#187; CSLA</title>
	<atom:link href="http://www.brianlagunas.com/index.php/category/csla/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.brianlagunas.com</link>
	<description></description>
	<lastBuildDate>Fri, 03 Sep 2010 01:55:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>CSLA: Make GUIDs required with a custom validation rule</title>
		<link>http://www.brianlagunas.com/index.php/2009/05/11/csla-make-guids-required-with-a-custom-validation-rule/</link>
		<comments>http://www.brianlagunas.com/index.php/2009/05/11/csla-make-guids-required-with-a-custom-validation-rule/#comments</comments>
		<pubDate>Mon, 11 May 2009 21:44:01 +0000</pubDate>
		<dc:creator>Brian Lagunas</dc:creator>
				<category><![CDATA[CSLA]]></category>
		<category><![CDATA[custom validation rule]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2009/05/11/csla-make-guids-required-with-a-custom-validation-rule-2/</guid>
		<description><![CDATA[If you use CSLA and use GUIDs as your unique identifiers you may have need to make them required for a particular object’s property. It isn’t as difficult to do as you might think and I will show you how to accomplish this by writing a custom validation rule.
I have an object that has two [...]]]></description>
			<content:encoded><![CDATA[<p>If you use CSLA and use GUIDs as your unique identifiers you may have need to make them required for a particular object’s property. It isn’t as difficult to do as you might think and I will show you how to accomplish this by writing a custom validation rule.</p>
<p>I have an object that has two properties, a LineNumber and a ControlAccount.&#160; The ControlAccount property is represented by as ComboBox using the Id (GUID) as the SelectedValuePath and the Description as the DisplayMemberPath as displayed by my WPF UI. I don’t want to user to be able to add a record unless they select a Control Account.</p>
<p><a href="http://elegantcode.com/wp-content/uploads/2009/05/guid-not-validated.jpg" target="_blank"><img title="guid_not_validated" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="62" alt="guid_not_validated" src="http://elegantcode.com/wp-content/uploads/2009/05/guid-not-validated-thumb.jpg" width="354" border="0" /></a> </p>
<p>As you can see the LineNumber is required and I am using the built-in CommonRules.StringRequired rule to add validation to the LineNumber property.</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; height: 30px; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">ValidationRules.AddRule(CommonRules.StringRequired, LineItemValueProperty);</pre>
</div>
<p>So now my problem is that CSLA doesn’t have a GuidRequired rule in its CommonRules class for me to use.&#160; So I will have to write my own.</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; height: 251px; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #0000ff">namespace</span> BusinessLibrary.Validation
{
   <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">class</span> CustomRules
   {
      <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">bool</span> GuidRequired(<span style="color: #0000ff">object</span> target, RuleArgs e)
      {
         Guid <span style="color: #0000ff">value</span> = (Guid)Utilities.CallByName(target, e.PropertyName, CallType.Get);

         <span style="color: #0000ff">if</span> (<span style="color: #0000ff">value</span> == Guid.Empty)
         {
            e.Description = String.Format(<span style="color: #006080">&quot;{0} required&quot;</span>, RuleArgs.GetPropertyName(e));
            <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;
         }

         <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>;
      }
   }
}</pre>
</div>
<p>Now I can use this custom rule to add a required GUID validation rule to my object on my AccountIdProperty.</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, &#39;Courier New&#39;, courier, monospace; height: 34px; background-color: #f4f4f4">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, &#39;Courier New&#39;, courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">ValidationRules.AddRule(CustomRules.GuidRequired, AccountIdProperty);</pre>
</div>
<p>Now that my rule has been added to my object, my WPF UI will now pickup the new validation rule.</p>
<p><a href="http://elegantcode.com/wp-content/uploads/2009/05/guid-validated1.jpg" target="_blank"><img title="guid_validated" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="62" alt="guid_validated" src="http://elegantcode.com/wp-content/uploads/2009/05/guid-validated-thumb1.jpg" width="354" border="0" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.brianlagunas.com/index.php/2009/05/11/csla-make-guids-required-with-a-custom-validation-rule/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
