<?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; .NET</title>
	<atom:link href="http://schotime.net/blog/index.php/category/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>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Dynamic Dot Less Css With Asp.net MVC 2</title>
		<link>http://schotime.net/blog/index.php/2010/07/02/dynamic-dot-less-css-with-asp-net-mvc-2/</link>
		<comments>http://schotime.net/blog/index.php/2010/07/02/dynamic-dot-less-css-with-asp-net-mvc-2/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 14:33:27 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[dotLess]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2010/07/02/dynamic-dot-less-css-with-asp-net-mvc-2/</guid>
		<description><![CDATA[I have been having a look at the best way to theme a asp.net mvc website in the last few weeks. I heard about dot less css late last year but hadn’t had time to integrate it into a project until now. 
Integrating dot less css is pretty easy by default, as shown on its [...]]]></description>
			<content:encoded><![CDATA[<p>I have been having a look at the best way to theme a asp.net mvc website in the last few weeks. I heard about <a href="http://dotlesscss.com" target="_blank" onclick="pageTracker._trackPageview('/outgoing/dotlesscss.com?referer=');">dot less css</a> late last year but hadn’t had time to integrate it into a project until now. </p>
<p>Integrating dot less css is pretty easy by default, as shown on its home page however I wanted something that could be configured by application users. We want to be able to specify the link html tag like the following.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400"> 
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">link </span><span style="color: red">href</span><span style="color: blue">=&quot;/public/css/sites.less&quot; </span><span style="color: red">rel</span><span style="color: blue">=&quot;stylesheet&quot; </span><span style="color: red">type</span><span style="color: blue">=&quot;text/css&quot; /&gt;</span></pre>
</td>
</tr>
</tbody>
</table>
<p>There are few steps involved in the solution I have come up with so bare with me as I go through them. Firstly we’ll start with the web.config:</p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
        </p>
<pre class="code"><span style="background: black; color: white">        &lt;</span><span style="background: black; color: #cc7832">compilation</span><span style="background: black; color: white">&gt;
            &lt;</span><span style="background: black; color: #cc7832">buildProviders</span><span style="background: black; color: white">&gt;
                &lt;</span><span style="background: black; color: #cc7832">add </span><span style="background: black; color: white">extension=&quot;</span><span style="background: black; color: #a5c25c">.lessx</span><span style="background: black; color: white">&quot; type=&quot;</span><span style="background: black; color: #a5c25c">System.Web.Compilation.PageBuildProvider</span><span style="background: black; color: white">&quot; /&gt;
            &lt;/</span><span style="background: black; color: #cc7832">buildProviders</span><span style="background: black; color: white">&gt;
        &lt;/</span><span style="background: black; color: #cc7832">compilation</span><span style="background: black; color: white">&gt;</span></pre>
</td>
</tr>
</tbody>
</table>
<p>Insert the builderProviders block inside the compilation section. The compilation section will be under the system.web section. This will enable us to use code blocks in our lessx files.</p>
<p>Secondly, is the routing.</p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
        </p>
<pre class="code"><span style="background: black; color: white">        </span><span style="background: black; color: gray">//Special Route for css so that images are relative to it
        </span><span style="background: black; color: white">routes.MapRoute(</span><span style="background: black; color: #a5c25c">&quot;css&quot;</span><span style="background: black; color: white">,
                           </span><span style="background: black; color: #a5c25c">&quot;public/css/{filename}.less&quot;</span><span style="background: black; color: white">,
                           </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: white">{ controller = </span><span style="background: black; color: #a5c25c">&quot;Css&quot;</span><span style="background: black; color: white">, action = </span><span style="background: black; color: #a5c25c">&quot;Index” </span><span style="background: black; color: white">},
                           </span><span style="background: black; color: #cc7832">new</span><span style="background: black; color: white">[] { </span><span style="background: black; color: #a5c25c">&quot;eLearning.Controllers&quot; </span><span style="background: black; color: white">});</span></pre>
</td>
</tr>
</tbody>
</table>
<p>This specifies that any url matching /public/css/{filename}.less should be routed to the CssController passing the filename as the parameter to the Index method.</p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
        </p>
<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">CssController </span><span style="background: black; color: white">: </span><span style="background: black; color: #ffc66d">BaseController
    </span><span style="background: black; color: white">{
        [</span><span style="background: black; color: #ffc66d">OutputCache</span><span style="background: black; color: white">(Duration = </span><span style="background: black; color: #6897bb">10</span><span style="background: black; color: white">, VaryByParam = </span><span style="background: black; color: #a5c25c">&quot;&quot;</span><span style="background: black; color: white">)]
        </span><span style="background: black; color: #cc7832">public </span><span style="background: black; color: #ffc66d">ActionResult </span><span style="background: black; color: white">Index(</span><span style="background: black; color: #cc7832">string </span><span style="background: black; color: white">filename)
        {
            </span><span style="background: black; color: #cc7832">if </span><span style="background: black; color: white">(</span><span style="background: black; color: #cc7832">string</span><span style="background: black; color: white">.IsNullOrEmpty(filename))
                </span><span style="background: black; color: #cc7832">throw new </span><span style="background: black; color: #ffc66d">ArgumentException</span><span style="background: black; color: white">(</span><span style="background: black; color: #a5c25c">&quot;A filename must be supplied&quot;</span><span style="background: black; color: white">);

            ViewData[</span><span style="background: black; color: #a5c25c">&quot;baseColor&quot;</span><span style="background: black; color: white">] = </span><span style="background: black; color: #a5c25c">&quot;#aabbcc&quot;</span><span style="background: black; color: white">;

            </span><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">less = RenderViewToString(filename + </span><span style="background: black; color: #a5c25c">&quot;.lessx&quot;</span><span style="background: black; color: white">, </span><span style="background: black; color: #cc7832">null</span><span style="background: black; color: white">);
            </span><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">css = </span><span style="background: black; color: #ffc66d">LessCss</span><span style="background: black; color: white">.Generate(less, </span><span style="background: black; color: #cc7832">true</span><span style="background: black; color: white">);
            </span><span style="background: black; color: #cc7832">return </span><span style="background: black; color: white">Content(css, </span><span style="background: black; color: #a5c25c">&quot;text/css&quot;</span><span style="background: black; color: white">);
        }
    }

    </span><span style="background: black; color: #cc7832">public static class </span><span style="background: black; color: #ffc66d">LessCss
    </span><span style="background: black; color: white">{
        </span><span style="background: black; color: #cc7832">public static string </span><span style="background: black; color: white">Generate(</span><span style="background: black; color: #cc7832">string </span><span style="background: black; color: white">less, </span><span style="background: black; color: #cc7832">bool </span><span style="background: black; color: white">minify)
        {
            </span><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">lessEngine = </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: white">EngineFactory();
            lessEngine.Configuration.MinifyOutput = minify;
            </span><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">output = lessEngine.GetEngine().TransformToCss(less, </span><span style="background: black; color: #cc7832">null</span><span style="background: black; color: white">);
            </span><span style="background: black; color: #cc7832">return </span><span style="background: black; color: white">output;
        }
    }</span></pre>
</td>
</tr>
</tbody>
</table>
<p>This is the css controller class. It receives the filename as the input, finds the corresponding .lessx file and returns the file with appropriate view data replaced. Is then takes the less formatted string and runs it through the dot less parser to return a css string. This is then returned to the browser with the appropriate content-type. One method I have not shown is the RenderViewToString() and how the .lessx file is located. This method is located on the base controller class that the css controller inherits from. I have also turned Caching on so that it will cache the result for 10seconds.</p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
        </p>
<pre class="code"><span style="background: black; color: white">        </span><span style="background: black; color: #cc7832">protected string </span><span style="background: black; color: white">RenderViewToString(</span><span style="background: black; color: #cc7832">string </span><span style="background: black; color: white">viewName, </span><span style="background: black; color: #cc7832">object </span><span style="background: black; color: white">model)
        {
            </span><span style="background: black; color: #cc7832">if </span><span style="background: black; color: white">(</span><span style="background: black; color: #cc7832">string</span><span style="background: black; color: white">.IsNullOrEmpty(viewName))
                viewName = ControllerContext.RouteData.GetRequiredString(</span><span style="background: black; color: #a5c25c">&quot;action&quot;</span><span style="background: black; color: white">);

            ViewData.Model = model;

            </span><span style="background: black; color: #cc7832">using </span><span style="background: black; color: white">(</span><span style="background: black; color: #ffc66d">StringWriter </span><span style="background: black; color: white">sw = </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">StringWriter</span><span style="background: black; color: white">())
            {
                </span><span style="background: black; color: #ffc66d">ViewEngineResult </span><span style="background: black; color: white">viewResult = </span><span style="background: black; color: #ffc66d">ViewEngines</span><span style="background: black; color: white">.Engines.FindView(ControllerContext, viewName, </span><span style="background: black; color: #cc7832">null</span><span style="background: black; color: white">);
                </span><span style="background: black; color: #ffc66d">ViewContext </span><span style="background: black; color: white">viewContext = </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">ViewContext</span><span style="background: black; color: white">(ControllerContext, viewResult.View, ViewData, TempData, sw);
                viewResult.View.Render(viewContext, sw);

                </span><span style="background: black; color: #cc7832">return </span><span style="background: black; color: white">sw.GetStringBuilder().ToString();
            }
        }</span></pre>
</td>
</tr>
</tbody>
</table>
<p>
  <br />This method finds the .lessx file and returns the result as a string. Now usually the view engine will take controller name and method name and by convention look in the /Views/css/index.aspx file. It doesn’t quite make sense to put it in the views folder so I have overridden the default web forms view engine so that they can be placed in the /Public/Css folder. This can be configured to your liking (convention).</p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
        </p>
<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">CustomViewEngine </span><span style="background: black; color: white">: </span><span style="background: black; color: #ffc66d">WebFormViewEngine
    </span><span style="background: black; color: white">{
        </span><span style="background: black; color: #cc7832">public </span><span style="background: black; color: white">CustomViewEngine()
        {
            </span><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">locs = </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">List</span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #cc7832">string</span><span style="background: black; color: white">&gt;(</span><span style="background: black; color: #cc7832">base</span><span style="background: black; color: white">.ViewLocationFormats);
            locs.Add(</span><span style="background: black; color: #a5c25c">&quot;~/Public/{1}/{0}&quot;</span><span style="background: black; color: white">); </span><span style="background: black; color: gray">//My personal choice
            </span><span style="background: black; color: white">locs.Add(</span><span style="background: black; color: #a5c25c">&quot;~/Views/{1}/{0}&quot;</span><span style="background: black; color: white">);  </span><span style="background: black; color: gray">//An alternative choice
            </span><span style="background: black; color: #cc7832">base</span><span style="background: black; color: white">.ViewLocationFormats = locs.ToArray();
        }
    }</span></pre>
</td>
</tr>
</tbody>
</table>
<p>Once your Custom View Engine has been overridden, you will need to add it to the ViewEngines.Engines collection in the application start method in the global.asax.cs.</p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
        </p>
<pre class="code"><span style="background: black; color: white">        </span><span style="background: black; color: #ffc66d">ViewEngines</span><span style="background: black; color: white">.Engines.Clear();
        </span><span style="background: black; color: #ffc66d">ViewEngines</span><span style="background: black; color: white">.Engines.Add(</span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">CustomViewEngine</span><span style="background: black; color: white">());</span></pre>
</td>
</tr>
</tbody>
</table>
<p>This will then allow us to write a *.lessx file like the following; you can also pass a strongly typed model to the .lessx file if you so wish in the same way you would to a regular aspx view.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
        </p>
<pre class="code">&lt;%@ Page Language=&quot;C#&quot; Inherits=&quot;System.Web.Mvc.ViewPage&quot; %&gt;

@basecolor: &lt;%= ViewData[&quot;baseColor&quot;] %&gt;;

body {
    background: @basecolor;
}</pre>
</td>
</tr>
</tbody>
</table>
<p>Which will be rendered to the browser as:</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
        </p>
<pre class="code">body {
    background: #aabbcc;
}</pre>
</td>
</tr>
</tbody>
</table>
<p>And that’s it. I’m still working on the finer details but I am liking how it works at the moment. If you have any thoughts or suggestions, please leave a comment.</p>
<p>Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2010/07/02/dynamic-dot-less-css-with-asp-net-mvc-2/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>NoRM, MongoDb and Complex Queries</title>
		<link>http://schotime.net/blog/index.php/2010/05/14/norm-mongodb-and-complex-queries/</link>
		<comments>http://schotime.net/blog/index.php/2010/05/14/norm-mongodb-and-complex-queries/#comments</comments>
		<pubDate>Fri, 14 May 2010 08:47:18 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[NoRM]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2010/05/14/norm-mongodb-and-complex-queries/</guid>
		<description><![CDATA[In my last post I told you about how you can use Regular Expressions to do complex queries in mongodb. In this post I will show you some more features of the Linq Provider.
Updated: 16th May 2010 after some feedback
Take these two classes for example:



 
    public class User
    [...]]]></description>
			<content:encoded><![CDATA[<p>In my last <a href="http://schotime.net/blog/index.php/2010/04/29/nosql-norm-mongodb-and-regular-expressions/" target="_blank">post</a> I told you about how you can use Regular Expressions to do complex queries in mongodb. In this post I will show you some more features of the Linq Provider.</p>
<p><strong>Updated: 16th May 2010 after some feedback</strong></p>
<p>Take these two classes for example:</p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td 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">User
    </span><span style="background: black; color: white">{
        </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">Id { </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: #cc7832">public string </span><span style="background: black; color: white">Name { </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: #cc7832">public </span><span style="background: black; color: #6897bb">IList</span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #ffc66d">Role</span><span style="background: black; color: white">&gt; Roles { </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: #cc7832">public class </span><span style="background: black; color: #ffc66d">Role
    </span><span style="background: black; color: white">{
        </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">Name { </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: #cc7832">public int </span><span style="background: black; color: white">AccessLevel { </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>and given the following data:</p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
        </p>
<pre class="code"><span style="background: black; color: white">            </span><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">standardRole = </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">Role </span><span style="background: black; color: white">{Name = </span><span style="background: black; color: #a5c25c">&quot;Standard&quot;</span><span style="background: black; color: white">, AccessLevel = </span><span style="background: black; color: #6897bb">2</span><span style="background: black; color: white">};
            </span><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">adminRole = </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">Role </span><span style="background: black; color: white">{ Name = </span><span style="background: black; color: #a5c25c">&quot;Standard&quot;</span><span style="background: black; color: white">, AccessLevel = </span><span style="background: black; color: #6897bb">5 </span><span style="background: black; color: white">};
            </span><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">user1 = </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">User</span><span style="background: black; color: white">()
            {
                Id = </span><span style="background: black; color: #a5c25c">&quot;jbloggs&quot;</span><span style="background: black; color: white">,
                Name = </span><span style="background: black; color: #a5c25c">&quot;Joe Bloggs&quot;</span><span style="background: black; color: white">,
                Roles = </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">List</span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #ffc66d">Role</span><span style="background: black; color: white">&gt;
                            {
                                standardRole,
                                adminRole
                            }
            };

            </span><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">user2 = </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">User</span><span style="background: black; color: white">()
            {
                Id = </span><span style="background: black; color: #a5c25c">&quot;tsmith&quot;</span><span style="background: black; color: white">,
                Name = </span><span style="background: black; color: #a5c25c">&quot;Tony Smith&quot;</span><span style="background: black; color: white">,
                Roles = </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">List</span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #ffc66d">Role</span><span style="background: black; color: white">&gt;
                            {
                                standardRole,
                            }
            };</span></pre>
</td>
</tr>
</tbody>
</table>
<p>Now say I want to find all users with access level greater than or equal to 5. In Linq-2-sql or plain sql your might write this query like the following. </p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
          </p>
<pre class="code"><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">query = list.Where(x =&gt; x.Roles.Where(y =&gt; y.AccessLevel &gt;= </span><span style="background: black; color: #6897bb">5</span><span style="background: black; color: white">).Count() &gt; </span><span style="background: black; color: #6897bb">0</span><span style="background: black; color: white">).ToList();</span></pre>
</td>
</tr>
</tbody>
</table>
<p>This query will not work in the NoRM linq provider, however there is an alternative.</p>
<table style="background: black" cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
        </p>
<pre><span style="background: black; color: #cc7832">1. var </span><span style="background: black; color: white">queryforMongo = list.Where(x =&gt; x.Roles[0].AccessLevel &gt;= </span><span style="background: black; color: #6897bb">5</span><span style="background: black; color: white">).ToList();or</span></pre>
<pre class="code"><span style="background: black; color: #cc7832">2. var </span><span style="background: black; color: white">queryforMongo = list.Where(x =&gt; x.Roles.Any(y=&gt;y.AccessLevel &gt;= </span><span style="background: black; color: #6897bb">5)</span><span style="background: black; color: white">).ToList();</span></pre>
<pre><span style="background: black; color: white"></span><span style="background: black; color: white"></span></pre>
</td>
</tr>
</tbody>
</table>
<p>Query 1 here will only return Users where the first role has an access level greater than or equal to 5.<br />
  <br />Query 2 will return Users where 1 of the Roles has an access level greater than or equal to 5.</p>
<p><strong>One caveat though.</strong> If you try and do a complex query ( one involving an “or” condition or a “and” using the same property ) then you will get an exception. This is because complex queries get converted to a javascript function that cannot be used in the same ways. </p>
<p>Hope this comes in handy for folks out there. </p>
<p>Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2010/05/14/norm-mongodb-and-complex-queries/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>NOSQL, NoRM (mongoDB) and Regular Expressions</title>
		<link>http://schotime.net/blog/index.php/2010/04/29/nosql-norm-mongodb-and-regular-expressions/</link>
		<comments>http://schotime.net/blog/index.php/2010/04/29/nosql-norm-mongodb-and-regular-expressions/#comments</comments>
		<pubDate>Thu, 29 Apr 2010 07:57:29 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[NoRM]]></category>
		<category><![CDATA[Regex]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2010/04/29/nosql-norm-mongodb-and-regular-expressions/</guid>
		<description><![CDATA[Over the past few weeks I have got quite involved in the development of the Open Source NoRM project started by Andrew Theken which is a MongoDB driver for C#. In particular I have been refactoring and adding new functionality to the LINQ provider.
It currently supports a lot of functionality including deep queries, regex, datetime [...]]]></description>
			<content:encoded><![CDATA[<p>Over the past few weeks I have got quite involved in the development of the Open Source <a href="http://github.com/atheken/NoRM" onclick="pageTracker._trackPageview('/outgoing/github.com/atheken/NoRM?referer=');">NoRM</a> project started by <a href="http://andrewtheken.com/" onclick="pageTracker._trackPageview('/outgoing/andrewtheken.com/?referer=');">Andrew Theken</a> which is a <a href="http://www.mongodb.org/" onclick="pageTracker._trackPageview('/outgoing/www.mongodb.org/?referer=');">MongoDB</a> driver for C#. In particular I have been refactoring and adding new functionality to the LINQ provider.</p>
<p>It currently supports a lot of functionality including deep queries, regex, datetime which is really exciting. In this post though I am going to concentrate on regular expressions.</p>
<p>This is the newest part of the Linq provider however it is probably the most powerful, especially for complex queries. The reason for this is that MongoDB will use the indexes created when a regular expression is used (where the query is not a complex query). A complex query is one that filters on the same property twice, uses a string function (replace/substring/toLower etc) or does some other fancy stuff. For example using the toUpper() method.</p>
<table style="background: black;" border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td width="400" valign="top">
<pre class="code"><span style="background: black; color: #cc7832;">var </span><span style="background: black; color: white;">products = session.Products.Where(x =&gt; x.Name.ToUpper() == </span><span style="background: black; color: #a5c25c;">"TEST3"</span><span style="background: black; color: white;">).ToList();</span></pre>
</td>
</tr>
</tbody>
</table>
<p>Anyways….i digress. So, here is an example of using a Regex in a Linq Query. Pretty simple.</p>
<table style="background: black;" border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td width="400" valign="top">
<pre class="code"><span style="background: black; color: #cc7832;">var </span><span style="background: black; color: white;">products = session.Products.Where(p =&gt; </span><span style="background: black; color: #ffc66d;">Regex</span><span style="background: black; color: white;">.IsMatch(p.Name, </span><span style="background: black; color: #a5c25c;">"^te"</span><span style="background: black; color: white;">)).ToList();</span></pre>
</td>
</tr>
</tbody>
</table>
<p>Using the static Regex.IsMatch is the only way to invoke a regex call using the Linq Provider. This will however run blazingly fast. I tested this query on 1,000,000 Products and it only took 1.5sec, which was approximately 10x faster than when a complex query is invoked. There are however 3 string functions that have been optimized using this regex functionality. They are StartsWith(), EndsWith() and Contains() which is why the following query only takes 2secs to return over 48,000 rows, however when using Skip() and Take() you can get 50 results back in just milliseconds.</p>
<table style="background: black;" border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td width="400" valign="top">
<pre class="code"><span style="background: black; color: #cc7832;">var </span><span style="background: black; color: white;">products = session.Products.Where(x =&gt; x.Name.StartsWith(</span><span style="background: black; color: #a5c25c;">"X"</span><span style="background: black; color: white;">)).ToList();</span></pre>
</td>
</tr>
</tbody>
</table>
<p>The Linq provider also supports 3 of the RegexOptions. They are RegexOptions.IgnoreCase (but please note this will not use the index so will be slower), RegexOptions.Multiline and RegexOptions.None. Regex’s can also be used in conjunction with other filters. eg.</p>
<table style="background: black;" border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td width="400" valign="top"><span style="background: black; color: #cc7832;"></p>
<pre class="code">var <span style="background: black; color: white;">products = session.Products.Where(p =&gt; </span><span style="background: black; color: #ffc66d;">Regex</span><span style="background: black; color: white;">.IsMatch(p.Name, </span><span style="background: black; color: #a5c25c;">"^te"</span><span style="background: black; color: white;">) &amp;&amp; p.Price == </span><span style="background: black; color: #6897bb;">10</span><span style="background: black; color: white;">).ToList();</span></pre>
<p></span></td>
</tr>
</tbody>
</table>
<p>This query is not considered a complex query because two different properties are used and an “and”(&amp;&amp;) operator is used.</p>
<p><strong><em>Please note:</em></strong> Any time a “or” (||) operator is used, it will be considered a complex query.</p>
<p><strong>Summary:</strong></p>
<p><strong> </strong></p>
<p>If you have a filter than can be written as a regex, chances are it will be as fast or faster than without using a regex.</p>
<p>So please go and try out NoRM and enjoy the freedom. I will try and post some more cool stuff in the LINQ provider over the next few weeks. Stay tuned.</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2010/04/29/nosql-norm-mongodb-and-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>FluentValidation Xval Integration</title>
		<link>http://schotime.net/blog/index.php/2009/06/13/fluentvalidation-xval-integration/</link>
		<comments>http://schotime.net/blog/index.php/2009/06/13/fluentvalidation-xval-integration/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 14:00:00 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Fluent Validation]]></category>
		<category><![CDATA[Validation]]></category>
		<category><![CDATA[xVal]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2009/06/13/fluentvalidation-xval-integration/</guid>
		<description><![CDATA[After a few months of using FluentValidation I asked its author Jeremy Skinner if it were possible to integrate this with xVal. At that time it was not possible because there were no easy way to access the properties needed by xVal. After submitting a few patches, we now have a solution which enables xVal [...]]]></description>
			<content:encoded><![CDATA[<p>After a few months of using <a href="http://fluentvalidation.codeplex.com" target="_blank" onclick="pageTracker._trackPageview('/outgoing/fluentvalidation.codeplex.com?referer=');">FluentValidation</a> I asked its author Jeremy Skinner if it were possible to integrate this with <a href="http://xval.codeplex.com" target="_blank" onclick="pageTracker._trackPageview('/outgoing/xval.codeplex.com?referer=');">xVal</a>. At that time it was not possible because there were no easy way to access the properties needed by xVal. After submitting a few patches, we now have a solution which enables xVal integration with most of the FV validators.</p>
<p>It currently supports the following FV validatiors:</p>
<ul>
<li>NullValidator </li>
<li>NotEmptyValidator </li>
<li>LengthValidator </li>
<li>RegularExpressionValidator </li>
<li>ComparisonValidator including:
<ul>
<li>Equal </li>
<li>Not Equal </li>
<li>Greater Than or Equal </li>
<li>Less Than or Equal </li>
</ul>
</li>
</ul>
<p>To configure the integration we need to tell xVal to use the FV rules provider rather than the default one. This is done in the global.asax.cs in Application_Start().</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">xVal.</span><span style="background: black; color: #ffc66d">ActiveRuleProviders</span><span style="background: black; color: white">.Providers.Clear();
xVal.</span><span style="background: black; color: #ffc66d">ActiveRuleProviders</span><span style="background: black; color: white">.Providers.Add(
    </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: white">FluentValidation.xValIntegration.
            </span><span style="background: black; color: #ffc66d">FluentValidationRulesProvider</span><span style="background: black; color: white">(</span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">AttributedValidatorFactory</span><span style="background: black; color: white">()));</span></pre>
</td>
</tr>
</tbody>
</table>
<p>The rules provider here is instructed to use the AttributedValidatorFactory to instruct the provider to use the attribute attached to the model class to find the validation class for that model.</p>
<p>Note: This is still new and xVal is still in beta so there may be some issues. If you find any please let me know so we can fix them as soon as possible.</p>
<p>Hopefully once its ready it can be checked in with the other providers at the xVal codeplex site.</p>
<p>This is currently in the development source code which can be downloaded and tried now.</p>
<p>Cheers,</p>
<p>Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2009/06/13/fluentvalidation-xval-integration/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Fluent Validation Model Binder &#8211; Asp.net MVC</title>
		<link>http://schotime.net/blog/index.php/2009/05/11/fluent-validation-model-binder-aspnet-mvc/</link>
		<comments>http://schotime.net/blog/index.php/2009/05/11/fluent-validation-model-binder-aspnet-mvc/#comments</comments>
		<pubDate>Mon, 11 May 2009 13:59:00 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Fluent Validation]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2009/05/11/fluent-validation-model-binder-aspnet-mvc/</guid>
		<description><![CDATA[A few weeks ago I found the Fluent Validation framework by Jeremy Skinner. I needed to conditionally validate a model depending on an application setting. eg. Description field mandatory / not mandatory depending on the clients business requirements. I loved the simplicity of the framework and the separation from the model it provided.
Since then I [...]]]></description>
			<content:encoded><![CDATA[<p>A few weeks ago I found the <a href="http://fluentvalidation.codeplex.com/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/fluentvalidation.codeplex.com/?referer=');">Fluent Validation</a> framework by <a href="http://www.jeremyskinner.co.uk/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.jeremyskinner.co.uk/?referer=');">Jeremy Skinner</a>. I needed to conditionally validate a model depending on an application setting. eg. Description field mandatory / not mandatory depending on the clients business requirements. I loved the simplicity of the framework and the separation from the model it provided.</p>
<p>Since then I have submitted a few patches for the framework, one of which is the Fluent Validation Model binder. Inspired by the Data Annotations Model binder, it works in much the same way. Once you have it set to your default model binder, it will validate any model which contains the specific attribute. This will become clear in the examples below.</p>
<p>Firstly lets take our model.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td style="background: black" valign="top" width="400" code?="code?"><span style="background: black; color: white">           <br /> 
<pre class="code"><span style="background: black; color: white">    [</span><span style="background: black; color: #ffc66d">Validator</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">LineItemValidator</span><span style="background: black; color: white">))]
    </span><span style="background: black; color: #cc7832">public class </span><span style="background: black; color: #ffc66d">LineItem
    </span><span style="background: black; color: white">{
        </span><span style="background: black; color: #cc7832">public int </span><span style="background: black; color: white">LineNumber { </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: #cc7832">public </span><span style="background: black; color: #6897bb">DateTime </span><span style="background: black; color: white">Date { </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: #cc7832">public string </span><span style="background: black; color: white">Description { </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: #cc7832">public decimal </span><span style="background: black; color: white">Net { </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: #cc7832">public decimal </span><span style="background: black; color: white">Tax { </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: #cc7832">public decimal </span><span style="background: black; color: white">Gross { </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>
<p>          <a href="http://11011.net/software/vspaste" onclick="pageTracker._trackPageview('/outgoing/11011.net/software/vspaste?referer=');"></a></p>
<p></span></td>
</tr>
</tbody>
</table>
<p>Attached to this simple LineItem class is a Validator attribute. This attribute is used by the Model Binder to locate the Class used for validation. Below I will define my LineItemValidator class which will hold the rules for the validation.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td style="background: black" valign="top" width="400" code?="code?">
<pre><span style="background: black; color: white">    </span><span style="background: black; color: #cc7832">public class </span><span style="background: black; color: #ffc66d">LineItemValidator </span><span style="background: black; color: white">: </span><span style="background: black; color: #ffc66d">AbstractValidator</span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #ffc66d">LineItem</span><span style="background: black; color: white">&gt;
    {
        </span><span style="background: black; color: #cc7832">public </span><span style="background: black; color: white">LineItemValidator()
        {
            RuleFor(x =&gt; x.Description)
                .NotEmpty().When(x =&gt; </span><span style="background: black; color: #ffc66d">Settings</span><span style="background: black; color: white">.DescriptionRequired)
                .And
                .Length(</span><span style="background: black; color: #6897bb">0</span><span style="background: black; color: white">, </span><span style="background: black; color: #6897bb">30</span><span style="background: black; color: white">);

            RuleFor(x =&gt; x.Date)
                .GreaterThanOrEqualTo(</span><span style="background: black; color: #6897bb">DateTime</span><span style="background: black; color: white">.Today);

            RuleFor(x =&gt; x.Net)
                .GreaterThan(</span><span style="background: black; color: #6897bb">0</span><span style="background: black; color: white">);

            RuleFor(x =&gt; x.Gross)
                .GreaterThan(</span><span style="background: black; color: #6897bb">0</span><span style="background: black; color: white">)
                .And
                .Equal(y =&gt; (y.Net + y.Tax))
                .WithName(</span><span style="background: black; color: #a5c25c">&quot;Total Amount&quot;</span><span style="background: black; color: white">);
        }
    }</span></pre>
<p>        <a href="http://11011.net/software/vspaste" onclick="pageTracker._trackPageview('/outgoing/11011.net/software/vspaste?referer=');"></a></td>
</tr>
</tbody>
</table>
<p>As you can see the class must inherit from AbstractValidator&lt;T&gt; where T is the model you want to define the rules for. The first rule uses the conditional When clause where it will only validate that the field is NotEmpty when the Settings.DescriptionRequired boolean is true. Also, another thing is the complex validation taking place on the Gross field. Not only does it validate that it is greater than 0, but that the value is equal to the net + tax amount. This is very elegant indeed. I have also specified the ‘WithName’ clause which has also been integrated into the model binder so that if an error occurs with the gross field, the WithName value will be displayed when an error happens. This is extremely handy for language localization or when the name of the field on the Model is insufficient. </p>
<p>Wiring this Model binder up in the Application_start event in the global.asax.cs is as easy as this.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td style="background: black" valign="top" width="400" code?="code?">
<pre><span style="background: black; color: white">     </span><span style="background: black; color: #ffc66d">ModelBinders</span><span style="background: black; color: white">.Binders.DefaultBinder =
                </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">FluentValidationModelBinder</span><span style="background: black; color: white">(</span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">AttributedValidatorFactory</span><span style="background: black; color: white">());</span></pre>
</td>
</tr>
</tbody>
</table>
<p>Note that we have to pass an instance of the AttributedValidatoryFactory into the Model Binder. This means if you have an alternate way of locating the Validator class other than via the attribute you can inherit from IValidatorFactory and create your own.</p>
<p>Now when a parameter of LineItem gets passed into a controller it will be validated against the model and all errors placed into the ModelState. This can then check the isValid property to determine if there are any errors and proceed accordingly.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td style="background: black" valign="top" width="400"><span style="background: black; color: white"><br />
          </p>
<pre class="code"><span style="background: black; color: white">        [</span><span style="background: black; color: #ffc66d">AcceptVerbs</span><span style="background: black; color: white">(</span><span style="background: black; color: #6897bb">HttpVerbs</span><span style="background: black; color: white">.Post)]
        </span><span style="background: black; color: #cc7832">public </span><span style="background: black; color: #ffc66d">ActionResult </span><span style="background: black; color: white">Edit(</span><span style="background: black; color: #ffc66d">List</span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #ffc66d">LineItem</span><span style="background: black; color: white">&gt; lineItems)
        {
            </span><span style="background: black; color: #cc7832">if </span><span style="background: black; color: white">(ModelState.IsValid)
                </span><span style="background: black; color: #cc7832">return </span><span style="background: black; color: white">RedirectToAction(</span><span style="background: black; color: #a5c25c">&quot;Edit&quot;</span><span style="background: black; color: white">);

            </span><span style="background: black; color: #cc7832">return </span><span style="background: black; color: white">View();
        }</span></pre>
<p>        </span></td>
</tr>
</tbody>
</table>
<p>And that’s it. I hope you enjoy using the Fluent Validation framework as much as I have and happy coding. For all you guys waiting for xVal integration, I will try and post a solution in the coming week. </p>
</p>
<p>Adam
  </p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2009/05/11/fluent-validation-model-binder-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<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. 
After [...]]]></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>
		<item>
		<title>Custom Validation Attributes</title>
		<link>http://schotime.net/blog/index.php/2009/03/11/custom-validation-attributes/</link>
		<comments>http://schotime.net/blog/index.php/2009/03/11/custom-validation-attributes/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 13:57:00 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[c# mvc validation asp.net]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2009/03/11/custom-validation-attributes/</guid>
		<description><![CDATA[In my last post I showed you how to use Validation attributes on your model for validation. Today I will show how you can create your own attributes that can provide custom validation for you.    
To accomplish this we need to inherit from ValidationAttribute. We&#8217;ll create an attribute today that will only [...]]]></description>
			<content:encoded><![CDATA[<p>In my last <a href="http://schotime.net/blog/index.php/2009/03/05/validation-with-aspnet-mvc-xval-idataerrorinfo/">post</a> I showed you how to use Validation attributes on your model for validation. Today I will show how you can create your own attributes that can provide custom validation for you.    </p>
<p>To accomplish this we need to inherit from ValidationAttribute. We&#8217;ll create an attribute today that will only allow a &quot;Yes&quot; or &quot;No&quot; value to be entered. A pretty bad example however it will still suffice.</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: #cc7832">public class </span><span style="background: black; color: #ffc66d">TwoValueAttribute </span><span style="background: black; color: white">: </span><span style="background: black; color: #ffc66d">ValidationAttribute
</span><span style="background: black; color: white">{
    </span><span style="background: black; color: #cc7832">private string </span><span style="background: black; color: white">str1 { </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: #cc7832">private string </span><span style="background: black; color: white">str2 { </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: #cc7832">public </span><span style="background: black; color: white">TwoValueAttribute(</span><span style="background: black; color: #cc7832">string </span><span style="background: black; color: white">str1, </span><span style="background: black; color: #cc7832">string </span><span style="background: black; color: white">str2)
    {
        </span><span style="background: black; color: #cc7832">this</span><span style="background: black; color: white">.str1 = str1;
        </span><span style="background: black; color: #cc7832">this</span><span style="background: black; color: white">.str2 = str2;
    }

    </span><span style="background: black; color: #cc7832">public override bool </span><span style="background: black; color: white">IsValid(</span><span style="background: black; color: #cc7832">object </span><span style="background: black; color: white">value)
    {
        </span><span style="background: black; color: #cc7832">if </span><span style="background: black; color: white">(value.ToString() == str1 || value.ToString() == str2)
            </span><span style="background: black; color: #cc7832">return true</span><span style="background: black; color: white">;
        </span><span style="background: black; color: #cc7832">else
            return false</span><span style="background: black; color: white">;
    }

    </span><span style="background: black; color: #cc7832">public override string </span><span style="background: black; color: white">FormatErrorMessage(</span><span style="background: black; color: #cc7832">string </span><span style="background: black; color: white">name)
    {
        </span><span style="background: black; color: #cc7832">return string</span><span style="background: black; color: white">.Format(</span><span style="background: black; color: #ffc66d">CultureInfo</span><span style="background: black; color: white">.CurrentCulture,
                             </span><span style="background: black; color: #cc7832">base</span><span style="background: black; color: white">.ErrorMessageString,
                             </span><span style="background: black; color: white">name, </span><span style="background: black; color: #cc7832">this</span><span style="background: black; color: white">.str1, </span><span style="background: black; color: #cc7832">this</span><span style="background: black; color: white">.str2);
    }
}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>All the validation logic takes place in the IsValid function. You can override the FormatErrorMessage function so that you can supply a custom error message for each instance that you want to validate if you so wish.</p>
<p>I&#8217;ll show you what I mean.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td style="background: black" valign="top" width="400">
        </p>
<pre class="code"><span style="background: black; color: #cc7832">public class </span><span style="background: black; color: #ffc66d">MyOrder </span><span style="background: black; color: white">: </span><span style="background: black; color: #ffc66d">CustomValidation
</span><span style="background: black; color: white">{
    [</span><span style="background: black; color: #ffc66d">TwoValue</span><span style="background: black; color: white">(</span><span style="background: black; color: #a5c25c">&quot;Yes&quot;</span><span style="background: black; color: white">,</span><span style="background: black; color: #a5c25c">&quot;No&quot;</span><span style="background: black; color: white">,
        ErrorMessage=</span><span style="background: black; color: #a5c25c">&quot;Only the values '{1}' and '{1}' are possible&quot;
                </span><span style="background: black; color: white">+ </span><span style="background: black; color: #a5c25c">&quot; for the {0} field&quot;</span><span style="background: black; color: white">)]
    </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">Value1 { </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">TwoValue</span><span style="background: black; color: white">(</span><span style="background: black; color: #a5c25c">&quot;Yes&quot;</span><span style="background: black; color: white">, </span><span style="background: black; color: #a5c25c">&quot;No&quot;</span><span style="background: black; color: white">)]
    </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">Value2 { </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>
  <br />Value2 in the above case will give you the default error message if the value is not &quot;Yes&quot; or &quot;No&quot;. However if Value1 is not &quot;Yes&quot; or &quot;No&quot; then the error message supplied will be used and the tokens replaced by the value entered, and the two values used for validation.</p>
<p>And that&#8217;s it. Pretty simple really.<br />
  <br />Cheers,</p>
<p>Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2009/03/11/custom-validation-attributes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Validation with Asp.net MVC, xVal &amp; IDataerrorInfo</title>
		<link>http://schotime.net/blog/index.php/2009/03/05/validation-with-aspnet-mvc-xval-idataerrorinfo/</link>
		<comments>http://schotime.net/blog/index.php/2009/03/05/validation-with-aspnet-mvc-xval-idataerrorinfo/#comments</comments>
		<pubDate>Thu, 05 Mar 2009 11:46:07 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C# MVC Asp.net Validation]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2009/03/05/validation-with-aspnet-mvc-xval-idataerrorinfo/</guid>
		<description><![CDATA[I&#8217;ve been playing around with lots of different Validation concepts recently and I think I have come up with something that will be very useful. Hopefully you will also find it useful.
Firstly I have been looking at Steve Sanderson&#8217;s new open source project xVal and decided that it was a good place to start. I [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been playing around with lots of different Validation concepts recently and I think I have come up with something that will be very useful. Hopefully you will also find it useful.</p>
<p>Firstly I have been looking at <a href="http://blog.codeville.net/2009/02/27/xval-08-beta-now-released/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/blog.codeville.net/2009/02/27/xval-08-beta-now-released/?referer=');">Steve Sanderson</a>&#8217;s new open source project <a href="http://www.codeplex.com/xval" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.codeplex.com/xval?referer=');">xVal</a> and decided that it was a good place to start. I was mainly looking at server side validation but <a href="http://www.codeplex.com/xval" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.codeplex.com/xval?referer=');">xVal</a> can also generate script to enable client side validation.</p>
<p>Firstly I&#8217;ll show you how I integrated the DataAnnotations validation pack that Dynamic data uses and is available to us in 3.5sp1 as far as I know.</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: #cc7832">public class </span><span style="background: black; color: #ffc66d">CustomValidation </span><span style="background: black; color: white">: </span><span style="background: black; color: #6897bb">IDataErrorInfo
</span><span style="background: black; color: white">{
    </span><span style="background: black; color: #cc7832">string </span><span style="background: black; color: #6897bb">IDataErrorInfo</span><span style="background: black; color: white">.Error
    {
        </span><span style="background: black; color: #cc7832">get
        </span><span style="background: black; color: white">{
            </span><span style="background: black; color: #cc7832">return string</span><span style="background: black; color: white">.Empty;
        }
    }

    </span><span style="background: black; color: #cc7832">string </span><span style="background: black; color: #6897bb">IDataErrorInfo</span><span style="background: black; color: white">.</span><span style="background: black; color: #cc7832">this</span><span style="background: black; color: white">[</span><span style="background: black; color: #cc7832">string </span><span style="background: black; color: white">columnName]
    {
        </span><span style="background: black; color: #cc7832">get
        </span><span style="background: black; color: white">{
            </span><span style="background: black; color: #ffc66d">List</span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #ffc66d">ErrorInfo</span><span style="background: black; color: white">&gt; errors =
                </span><span style="background: black; color: #ffc66d">DataAnnotations</span><span style="background: black; color: white">.GetErrors(</span><span style="background: black; color: #cc7832">this</span><span style="background: black; color: white">, columnName).ToList();
            </span><span style="background: black; color: #cc7832">return </span><span style="background: black; color: white">errors.Count &gt; </span><span style="background: black; color: #6897bb">0 </span><span style="background: black; color: white">? errors[</span><span style="background: black; color: #6897bb">0</span><span style="background: black; color: white">].ErrorMessage : </span><span style="background: black; color: #cc7832">null</span><span style="background: black; color: white">;
        }
    }
}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>So I first created a class called CustomValidation that implements the IDataErrorInfo interface. From here only two Properties need to be set, the second one being the most important. Here I use the get property accessor to check the errors for each column and if there are errors I return the error message for the first error.</p>
<p>From here we need our Model that we want validated to inherit from this class as follows.</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">MyProduct </span><span style="background: black; color: white">: </span><span style="background: black; color: #ffc66d">CustomValidation
    </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">100</span><span style="background: black; color: white">, ErrorMessage=</span><span style="background: black; color: #a5c25c">"{0} must be set between {1} and {2}"</span><span style="background: black; color: white">)]
        </span><span style="background: black; color: #cc7832">public double</span><span style="background: black; color: white">? UnitPrice { </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">StringLength</span><span style="background: black; color: white">(</span><span style="background: black; color: #6897bb">30</span><span style="background: black; color: white">)]
        </span><span style="background: black; color: #cc7832">public string </span><span style="background: black; color: white">ProductName { </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: #cc7832">public </span><span style="background: black; color: #ffc66d">List</span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #ffc66d">MyOrder</span><span style="background: black; color: white">&gt; Orders { </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: #cc7832">public class </span><span style="background: black; color: #ffc66d">MyOrder </span><span style="background: black; color: white">: </span><span style="background: black; color: #ffc66d">CustomValidation
    </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">Name { </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>We can then attach Validation attributes. <strong>note:</strong> you will need to add a reference to the System.ComponentModel.DataAnnotations assembly for these attributes to show up.</p>
<p>Once we have these two things setup we can start using it. Actually before I start I will show you the DataAnnotations.GetErrors methods.</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: #cc7832">public static class </span><span style="background: black; color: #ffc66d">DataAnnotations
</span><span style="background: black; color: white">{
    </span><span style="background: black; color: #cc7832">public static </span><span style="background: black; color: #6897bb">IEnumerable</span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #ffc66d">ErrorInfo</span><span style="background: black; color: white">&gt; GetErrors(</span><span style="background: black; color: #cc7832">object </span><span style="background: black; color: white">instance)
    {
        </span><span style="background: black; color: #cc7832">return </span><span style="background: black; color: white">GetErrors(instance, </span><span style="background: black; color: #cc7832">null</span><span style="background: black; color: white">);
    }

    </span><span style="background: black; color: #cc7832">public static </span><span style="background: black; color: #6897bb">IEnumerable</span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #ffc66d">ErrorInfo</span><span style="background: black; color: white">&gt; GetErrors(</span><span style="background: black; color: #cc7832">object </span><span style="background: black; color: white">instance, </span><span style="background: black; color: #cc7832">string </span><span style="background: black; color: white">name)
    {
        </span><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">metadataAttrib = instance.GetType()
                .GetCustomAttributes(</span><span style="background: black; color: #cc7832">typeof</span><span style="background: black; color: white">(</span><span style="background: black; color: #ffc66d">MetadataTypeAttribute</span><span style="background: black; color: white">), </span><span style="background: black; color: #cc7832">true</span><span style="background: black; color: white">)
                .OfType&lt;</span><span style="background: black; color: #ffc66d">MetadataTypeAttribute</span><span style="background: black; color: white">&gt;().FirstOrDefault();
        </span><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">buddyClassOrModelClass = metadataAttrib != </span><span style="background: black; color: #cc7832">null
                </span><span style="background: black; color: white">? metadataAttrib.MetadataClassType
                : instance.GetType();
        </span><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">buddyClassProperties = </span><span style="background: black; color: #ffc66d">TypeDescriptor</span><span style="background: black; color: white">.GetProperties(buddyClassOrModelClass)
            .Cast&lt;</span><span style="background: black; color: #ffc66d">PropertyDescriptor</span><span style="background: black; color: white">&gt;();
        </span><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">modelClassProperties = </span><span style="background: black; color: #ffc66d">TypeDescriptor</span><span style="background: black; color: white">.GetProperties(instance.GetType())
            .Cast&lt;</span><span style="background: black; color: #ffc66d">PropertyDescriptor</span><span style="background: black; color: white">&gt;();

        </span><span style="background: black; color: #cc7832">var </span><span style="background: black; color: white">list = </span><span style="background: black; color: #cc7832">from </span><span style="background: black; color: white">buddyProp </span><span style="background: black; color: #cc7832">in </span><span style="background: black; color: white">buddyClassProperties
                   </span><span style="background: black; color: #cc7832">join </span><span style="background: black; color: white">modelProp </span><span style="background: black; color: #cc7832">in </span><span style="background: black; color: white">modelClassProperties </span><span style="background: black; color: #cc7832">on
                            </span><span style="background: black; color: white">buddyProp.Name </span><span style="background: black; color: #cc7832">equals </span><span style="background: black; color: white">modelProp.Name
                   </span><span style="background: black; color: #cc7832">from </span><span style="background: black; color: white">attribute </span><span style="background: black; color: #cc7832">in </span><span style="background: black; color: white">buddyProp.Attributes.OfType&lt;</span><span style="background: black; color: #ffc66d">ValidationAttribute</span><span style="background: black; color: white">&gt;()
                   </span><span style="background: black; color: #cc7832">where </span><span style="background: black; color: white">!attribute.IsValid(modelProp.GetValue(instance))
                   </span><span style="background: black; color: #cc7832">select new </span><span style="background: black; color: #ffc66d">ErrorInfo</span><span style="background: black; color: white">(
                       buddyProp.Name,
                       attribute.FormatErrorMessage(modelProp.Name),
                       instance);

        </span><span style="background: black; color: #cc7832">if </span><span style="background: black; color: white">(name != </span><span style="background: black; color: #cc7832">null</span><span style="background: black; color: white">)
            list = list.Where(x =&gt; x.PropertyName == name);

        </span><span style="background: black; color: #cc7832">return </span><span style="background: black; color: white">list;
    }
}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>note: you will need the using statement and a reference to the xVal.dll<br /><span style="background: black; color: #cc7832">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using </span><span style="background: black; color: white">xVal.ServerSide;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></p>
<p><a href="http://11011.net/software/vspaste" onclick="pageTracker._trackPageview('/outgoing/11011.net/software/vspaste?referer=');"></a></p>
<p>Now we can start.<br />Here is are sample controller actions. Creating a MyProduct and sending it to the view. Then accepting a MyProduct when the form gets posted back.</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: #cc7832">public </span><span style="background: black; color: #ffc66d">ActionResult </span><span style="background: black; color: white">ValidationTest()
{
    </span><span style="background: black; color: #ffc66d">MyProduct </span><span style="background: black; color: white">p = </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">MyProduct</span><span style="background: black; color: white">()
    {
        ProductName = </span><span style="background: black; color: #a5c25c">"Magazine"</span><span style="background: black; color: white">,
        UnitPrice = </span><span style="background: black; color: #6897bb">23
    </span><span style="background: black; color: white">};

    p.Orders = </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">List</span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #ffc66d">MyOrder</span><span style="background: black; color: white">&gt;();
    p.Orders.Add(</span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">MyOrder </span><span style="background: black; color: white">{ Name = </span><span style="background: black; color: #a5c25c">"Robert King" </span><span style="background: black; color: white">});

    </span><span style="background: black; color: #cc7832">return </span><span style="background: black; color: white">View(p);
}</span></pre>
<pre class="code"><span style="background: black; color: white">[</span><span style="background: black; color: #ffc66d">AcceptVerbs</span><span style="background: black; color: white">(</span><span style="background: black; color: #6897bb">HttpVerbs</span><span style="background: black; color: white">.Post)]
</span><span style="background: black; color: #cc7832">public </span><span style="background: black; color: #ffc66d">ActionResult </span><span style="background: black; color: white">ValidationTest(</span><span style="background: black; color: #ffc66d">MyProduct </span><span style="background: black; color: white">product)
{
    </span><span style="background: black; color: #cc7832">if </span><span style="background: black; color: white">(ModelState.IsValid)
    {
        </span><span style="background: black; color: #cc7832">return </span><span style="background: black; color: white">RedirectToAction(</span><span style="background: black; color: #a5c25c">"ValidationTest"</span><span style="background: black; color: white">);
    }

    </span><span style="background: black; color: #cc7832">return </span><span style="background: black; color: white">View(product);
}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>In the latter action method the product argument has already been validated by the time it arrives at the if statement. All that needs to be done is to check if the Modelstate is valid and if it is redirect back to the page else display the posted data back to the user with the errors in Modelstate.</p>
<p>Here is what the view looks like.</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">&lt;</span><span style="background: black; color: #e8bc64">h2</span><span style="background: black; color: white">&gt;Custom Validation&lt;/</span><span style="background: black; color: #e8bc64">h2</span><span style="background: black; color: white">&gt;

</span><span style="background: black; color: #6897bb">&lt;%</span><span style="background: black; color: white">= Html.ValidationSummary() </span><span style="background: black; color: #6897bb">%&gt;

&lt;% </span><span style="background: black; color: #cc7832">using </span><span style="background: black; color: white">(Html.BeginForm()) { </span><span style="background: black; color: #6897bb">%&gt;

    </span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #e8bc64">div </span><span style="background: black; color: white">style=</span><span style="background: black; color: #a5c25c">"</span><span style="background: black; color: #fcf4be">padding-left</span><span style="background: black; color: white">: 15px</span><span style="background: black; color: #a5c25c">"</span><span style="background: black; color: white">&gt;
        &lt;</span><span style="background: black; color: #e8bc64">div</span><span style="background: black; color: white">&gt;
            Unit Price: </span><span style="background: black; color: #6897bb">&lt;%</span><span style="background: black; color: white">= Html.TextBox(</span><span style="background: black; color: #a5c25c">"product.UnitPrice"</span><span style="background: black; color: white">, Model.UnitPrice)</span><span style="background: black; color: #6897bb">%&gt;
            &lt;%</span><span style="background: black; color: white">= Html.ValidationMessage(</span><span style="background: black; color: #a5c25c">"product.UnitPrice"</span><span style="background: black; color: white">)</span><span style="background: black; color: #6897bb">%&gt;
        </span><span style="background: black; color: white">&lt;/</span><span style="background: black; color: #e8bc64">div</span><span style="background: black; color: white">&gt;
        &lt;</span><span style="background: black; color: #e8bc64">div</span><span style="background: black; color: white">&gt;
            Product Name: </span><span style="background: black; color: #6897bb">&lt;%</span><span style="background: black; color: white">= Html.TextBox(</span><span style="background: black; color: #a5c25c">"product.ProductName"</span><span style="background: black; color: white">, Model.ProductName)</span><span style="background: black; color: #6897bb">%&gt;
             &lt;%</span><span style="background: black; color: white">= Html.ValidationMessage(</span><span style="background: black; color: #a5c25c">"product.ProductName"</span><span style="background: black; color: white">)</span><span style="background: black; color: #6897bb">%&gt;
        </span><span style="background: black; color: white">&lt;/</span><span style="background: black; color: #e8bc64">div</span><span style="background: black; color: white">&gt;
        &lt;</span><span style="background: black; color: #e8bc64">div</span><span style="background: black; color: white">&gt;
            Orders:
            </span><span style="background: black; color: #6897bb">&lt;% </span><span style="background: black; color: #cc7832">int </span><span style="background: black; color: white">j = </span><span style="background: black; color: #6897bb">0</span><span style="background: black; color: white">;
               </span><span style="background: black; color: #cc7832">foreach </span><span style="background: black; color: white">(</span><span style="background: black; color: #ffc66d">MyOrder </span><span style="background: black; color: white">o </span><span style="background: black; color: #cc7832">in </span><span style="background: black; color: white">Model.Orders) { </span><span style="background: black; color: #6897bb">%&gt;
               </span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #e8bc64">div  </span><span style="background: black; color: white">style=</span><span style="background: black; color: #a5c25c">"</span><span style="background: black; color: #fcf4be">padding-left</span><span style="background: black; color: white">: 15px</span><span style="background: black; color: #a5c25c">"</span><span style="background: black; color: white">&gt;
                    </span><span style="background: black; color: #6897bb">&lt;%</span><span style="background: black; color: white">= j + </span><span style="background: black; color: #6897bb">1 %&gt;</span><span style="background: black; color: white">. </span><span style="background: black; color: #6897bb">&lt;%</span><span style="background: black; color: white">= Html.TextBox(</span><span style="background: black; color: #a5c25c">"product.Orders[" </span><span style="background: black; color: white">+ j + </span><span style="background: black; color: #a5c25c">"].Name"</span><span style="background: black; color: white">, o.Name)</span><span style="background: black; color: #6897bb">%&gt;
                    &lt;%</span><span style="background: black; color: white">= Html.ValidationMessage(</span><span style="background: black; color: #a5c25c">"product.Orders[" </span><span style="background: black; color: white">+ j + </span><span style="background: black; color: #a5c25c">"].Name"</span><span style="background: black; color: white">)</span><span style="background: black; color: #6897bb">%&gt;</span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #e8bc64">br </span><span style="background: black; color: white">/&gt;
                    </span><span style="background: black; color: #6897bb">&lt;% </span><span style="background: black; color: white">j++; </span><span style="background: black; color: #6897bb">%&gt;
               </span><span style="background: black; color: white">&lt;/</span><span style="background: black; color: #e8bc64">div</span><span style="background: black; color: white">&gt;
            </span><span style="background: black; color: #6897bb">&lt;% </span><span style="background: black; color: white">} </span><span style="background: black; color: #6897bb">%&gt;
        </span><span style="background: black; color: white">&lt;/</span><span style="background: black; color: #e8bc64">div</span><span style="background: black; color: white">&gt;
    &lt;/</span><span style="background: black; color: #e8bc64">div</span><span style="background: black; color: white">&gt;

    &lt;</span><span style="background: black; color: #e8bc64">br </span><span style="background: black; color: white">/&gt;
    &lt;</span><span style="background: black; color: #e8bc64">input </span><span style="background: black; color: white">type=</span><span style="background: black; color: #a5c25c">"submit" </span><span style="background: black; color: white">/&gt;

</span><span style="background: black; color: #6897bb">&lt;% </span><span style="background: black; color: white">} </span><span style="background: black; color: #6897bb">%&gt;</span></pre>
<p><a href="http://11011.net/software/vspaste" onclick="pageTracker._trackPageview('/outgoing/11011.net/software/vspaste?referer=');"></a></td>
</tr>
</tbody>
</table>
<p>Not that this even works when complex binding lists to that have been posted. I&#8217;ll try and get a full working example for download here as well if I get enough interest.</p>
<p>And that&#8217;s it. In upcoming posts I&#8217;ll show you how:</p>
<ul>
<li>To do the validation without using the IDataErrorInfo interface and its automatic binding.
<li>Use the methods that come with <a href="http://www.codeplex.com/xval" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.codeplex.com/xval?referer=');">xVal</a> to implement some client side validation.
<li>How to create your own validation attributes.
<li>Plugging in Castle Validator or other supported validators into <a href="http://www.codeplex.com/xval" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.codeplex.com/xval?referer=');">xVal</a>.</li>
</ul>
<p>Hope this helps you get started with Validation in Asp.net MVC.</p>
<p>Cheers,<br />Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2009/03/05/validation-with-aspnet-mvc-xval-idataerrorinfo/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Custom Authorization With Asp.net MVC</title>
		<link>http://schotime.net/blog/index.php/2009/02/17/custom-authorization-with-aspnet-mvc/</link>
		<comments>http://schotime.net/blog/index.php/2009/02/17/custom-authorization-with-aspnet-mvc/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 11:17:29 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C# MVC .net]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2009/02/17/custom-authorization-with-aspnet-mvc/</guid>
		<description><![CDATA[The whole advantage with MVC over webforms is extensibility at every point. Extensibility, Extensibility, Extensibility.
Authorization is a very important and every web project has there own needs and requirements. Full customisation is paramount.
Here I will show you a simple way to customise your authorization.
In MVC attributes are used to protect a controller method, so we [...]]]></description>
			<content:encoded><![CDATA[<p>The whole advantage with MVC over webforms is extensibility at every point. Extensibility, Extensibility, Extensibility.</p>
<p>Authorization is a very important and every web project has there own needs and requirements. Full customisation is paramount.</p>
<p>Here I will show you a simple way to customise your authorization.</p>
<p>In MVC attributes are used to protect a controller method, so we to get started all we need to do is inherit from the AuthorizeAttribute class.</p>
<table border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td style="background: black" width="400" valign="top">
<pre class="code"><span style="background: black; color: #cc7832">    public class </span><span style="background: black; color: #ffc66d">CustomAuthorizeAttribute </span><span style="background: black; color: white">: </span><span style="background: black; color: #ffc66d">AuthorizeAttribute
    </span><span style="background: black; color: white">{
        </span><span style="background: black; color: #cc7832">protected override bool </span><span style="background: black; color: white">AuthorizeCore(</span><span style="background: black; color: #ffc66d">HttpContextBase </span><span style="background: black; color: white">httpContext)
        {
            </span><span style="background: black; color: #cc7832">string</span><span style="background: black; color: white">[] users = Users.Split(</span><span style="background: black; color: #a5c25c">','</span><span style="background: black; color: white">);

            </span><span style="background: black; color: #cc7832">if </span><span style="background: black; color: white">(!httpContext.User.Identity.IsAuthenticated)
                </span><span style="background: black; color: #cc7832">return false</span><span style="background: black; color: white">;

            </span><span style="background: black; color: #cc7832">if </span><span style="background: black; color: white">(users.Length &gt; </span><span style="background: black; color: #6897bb">0 </span><span style="background: black; color: white">&amp;&amp;
                !users.Contains(httpContext.User.Identity.Name,
                    </span><span style="background: black; color: #ffc66d">StringComparer</span><span style="background: black; color: white">.OrdinalIgnoreCase))
                </span><span style="background: black; color: #cc7832">return false</span><span style="background: black; color: white">;

            </span><span style="background: black; color: #cc7832">return true</span><span style="background: black; color: white">;
        }
    }</span></pre>
</td>
</tr>
</tbody>
</table>
<p>This is the basics. We can put any logic we like in here and all we have to do is return false if for whatever reason the user should not be authorized. Then all you need to do is decorate the controller method with the new attribute as below.</p>
<table border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td style="background: black" width="400" valign="top">
<pre class="code"><span style="background: black; color: white">    [</span><span style="background: black; color: #ffc66d">CustomAuthorize</span><span style="background: black; color: white">]
    </span><span style="background: black; color: #cc7832">public </span><span style="background: black; color: #ffc66d">ActionResult </span><span style="background: black; color: white">Index()
    {
        </span><span style="background: black; color: #cc7832">return </span><span style="background: black; color: white">View();
    }</span></pre>
</td>
</tr>
</tbody>
</table>
<p>From this simple example we can expand it with custom Roles.</p>
<table border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td style="background: black" width="400" valign="top">
<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">CustomAuthorizeAttribute </span><span style="background: black; color: white">: </span><span style="background: black; color: #ffc66d">AuthorizeAttribute
    </span><span style="background: black; color: white">{
        </span><span style="background: black; color: gray">// the "new" must be used here because we are overriding
        // the Roles property on the underlying class
        </span><span style="background: black; color: #cc7832">public new </span><span style="background: black; color: #6897bb">SiteRoles </span><span style="background: black; color: white">Roles;

        </span><span style="background: black; color: #cc7832">protected override bool </span><span style="background: black; color: white">AuthorizeCore(</span><span style="background: black; color: #ffc66d">HttpContextBase </span><span style="background: black; color: white">httpContext)
        {
            </span><span style="background: black; color: #cc7832">if </span><span style="background: black; color: white">(httpContext == </span><span style="background: black; color: #cc7832">null</span><span style="background: black; color: white">)
                </span><span style="background: black; color: #cc7832">throw new </span><span style="background: black; color: #ffc66d">ArgumentNullException</span><span style="background: black; color: white">(</span><span style="background: black; color: #a5c25c">"httpContext"</span><span style="background: black; color: white">);

            </span><span style="background: black; color: #cc7832">string</span><span style="background: black; color: white">[] users = Users.Split(',');

</span><span style="background: black; color: white">            </span><span style="background: black; color: #cc7832">if </span><span style="background: black; color: white">(!httpContext.User.Identity.IsAuthenticated)
                </span><span style="background: black; color: #cc7832">return false</span><span style="background: black; color: white">;

            </span><span style="background: black; color: #6897bb">SiteRoles </span><span style="background: black; color: white">role = (</span><span style="background: black; color: #6897bb">SiteRoles</span><span style="background: black; color: white">)httpContext.Session[</span><span style="background: black; color: #a5c25c">"role"</span><span style="background: black; color: white">];

            </span><span style="background: black; color: #cc7832">if </span><span style="background: black; color: white">(</span><span style="background: black; color: white">Roles != 0 &amp;&amp; ((Roles</span><span style="background: black; color: white"> &amp; </span><span style="background: black; color: white">role) != role))</span><span style="background: black; color: white">
                </span><span style="background: black; color: #cc7832">return false</span><span style="background: black; color: white">;

            </span><span style="background: black; color: #cc7832">return true</span><span style="background: black; color: white">;
        }
    }</span></pre>
</td>
</tr>
</tbody>
</table>
<p>Where the SiteRoles class is defined as below.</p>
<table border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td style="background: black" width="400" valign="top">
<pre class="code"><span style="background: black; color: white">    [</span><span style="background: black; color: #ffc66d">Serializable</span><span style="background: black; color: white">]
    [</span><span style="background: black; color: #ffc66d">Flags</span><span style="background: black; color: white">]
    </span><span style="background: black; color: #cc7832">public enum </span><span style="background: black; color: #6897bb">SiteRoles
    </span><span style="background: black; color: white">{
        User = </span><span style="background: black; color: #6897bb">1 </span><span style="background: black; color: white">&lt;&lt; </span><span style="background: black; color: #6897bb">0</span><span style="background: black; color: white">,
        Admin = </span><span style="background: black; color: #6897bb">1 </span><span style="background: black; color: white">&lt;&lt; </span><span style="background: black; color: #6897bb">1</span><span style="background: black; color: white">,
        Helpdesk = </span><span style="background: black; color: #6897bb">1 </span><span style="background: black; color: white">&lt;&lt; </span><span style="background: black; color: #6897bb">2
    </span><span style="background: black; color: white">}</span></pre>
</td>
</tr>
</tbody>
</table>
<p>This can then be used be used as follows.</p>
<table border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td style="background: black" width="400" valign="top">
<pre class="code"><span style="background: black; color: white">    [</span><span style="background: black; color: #ffc66d">CustomAuthorize</span><span style="background: black; color: white">(Roles=</span><span style="background: black; color: #6897bb">SiteRoles</span><span style="background: black; color: white">.Admin|</span><span style="background: black; color: #6897bb">SiteRoles</span><span style="background: black; color: white">.HelpDesk)]
    </span><span style="background: black; color: #cc7832">public </span><span style="background: black; color: #ffc66d">ActionResult </span><span style="background: black; color: white">Index()
    {
        </span><span style="background: black; color: #cc7832">return </span><span style="background: black; color: white">View();
    }</span></pre>
</td>
</tr>
</tbody>
</table>
<p>This will only allow the Admin and the Helpdesk Role access to the Index controller. If you don&#8217;t belong to one of these roles then you will be sent to the Login page.</p>
<p>The possibilities are really endless.<br />
Happy coding.</p>
<p>Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2009/02/17/custom-authorization-with-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Retrieving Data Without the Dataset With Custom SQL</title>
		<link>http://schotime.net/blog/index.php/2009/02/12/retrieving-data-without-the-dataset-with-custom-sql/</link>
		<comments>http://schotime.net/blog/index.php/2009/02/12/retrieving-data-without-the-dataset-with-custom-sql/#comments</comments>
		<pubDate>Thu, 12 Feb 2009 10:38:16 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Linq C# MySql]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2009/02/12/retrieving-data-without-the-dataset-with-custom-sql/</guid>
		<description><![CDATA[In my previous post I used custom sql query and transformed the results into a nested class. However you may have overlooked way I got the data into a class. It quite easy and will work with all database connectors that implement IDbConnection.
Here is how its done. We use the DataContext that came with and [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous post I used custom sql query and transformed the results into a nested class. However you may have overlooked way I got the data into a class. It quite easy and will work with all database connectors that implement IDbConnection.</p>
<p>Here is how its done. We use the DataContext that came with and used by Linq2Sql.</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">RawData
        </span><span style="background: black; color: white">{
            </span><span style="background: black; color: #cc7832">public int </span><span style="background: black; color: white">MyId { </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: #cc7832">public string </span><span style="background: black; color: white">Name { </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></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: white"></span></pre>
<pre class="code"><span style="background: black; color: white">        </span><span style="background: black; color: #ffc66d">DataContext </span><span style="background: black; color: white">dc = </span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: #ffc66d">DataContext</span><span style="background: black; color: white">(</span><span style="background: black; color: #cc7832">new </span><span style="background: black; color: white">MySqlConnection(connString));
        </span><span style="background: black; color: #6897bb">IEnumerable</span><span style="background: black; color: white">&lt;</span><span style="background: black; color: #ffc66d">RawData</span><span style="background: black; color: white">&gt; rd =
            dc.ExecuteQuery&lt;</span><span style="background: black; color: #ffc66d">RawData</span><span style="background: black; color: white">&gt;(</span><span style="background: black; color: #a5c25c">"select myid, name from mytable where myid &gt; ?"</span><span style="background: black; color: white">,5);</span></pre>
<p><a href="http://11011.net/software/vspaste" onclick="pageTracker._trackPageview('/outgoing/11011.net/software/vspaste?referer=');"></a></td>
</tr>
</tbody>
</table>
<p>And thats it. All the data from the mytable database table will get pushed into the IEnumerable&lt;RawData&gt; variable. As long as the names of the columns are the same as the class then this will work. This ExecuteQuery method also takes a parameters object argument which will replace the question marks with the variables specified.</p>
<p>It makes it very easy, especially since there isn&#8217;t a very good linq 2 mysql option out there yet and Entity Framework still doesn&#8217;t support it.</p>
<p>Cheers,<br />Adam</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2009/02/12/retrieving-data-without-the-dataset-with-custom-sql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
