<?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>Web Dev Bros &#187; .net</title>
	<atom:link href="http://www.webdevbros.net/category/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webdevbros.net</link>
	<description>hot talk about web development</description>
	<lastBuildDate>Thu, 20 Jan 2011 19:55:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>ASP.Net MVC, Fluent Validation and testing</title>
		<link>http://www.webdevbros.net/2010/12/03/asp-net-mvc-fluent-validation-and-tesing/</link>
		<comments>http://www.webdevbros.net/2010/12/03/asp-net-mvc-fluent-validation-and-tesing/#comments</comments>
		<pubDate>Fri, 03 Dec 2010 15:40:58 +0000</pubDate>
		<dc:creator>Dai Bok</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[ASP.net MVC]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.webdevbros.net/?p=932</guid>
		<description><![CDATA[In this post, I will show you how I set up fluent validation, to work smoothly with my MVC 2 project. I create a custom model binder to validate view models and show how to validate a registration using fluent validation. Finally I show how to unit test the validation rules I needed for registration.]]></description>
			<content:encoded><![CDATA[<p>I am finally coming to the end of a project, and I thought it would be good to write a little post on how we have managed to set up our fluent validation for our MVC project. In the start we did some research into how to go about performing validation, and found a number of recommendations. But all involved using data annotations. In this post, I will show you how I set up fluent validation, to work smoothly with my MVC 2 project. I create a custom model binder to validate view models and show how to validate a registration using fluent validation. Finally I show how to unit test the validation rules I needed for registration.
</p>
<p>To start with, there are some problems I have with data annotations
<p>1 –  Too many annotations  make your model s look ugly. <a href="http://www.asp.net/mvc/tutorials/validation-with-the-data-annotation-validators-cs ">Take this example</a> . </p>
<p>
   <span style='color:blue'>public</span> <span style='color:blue'>class</span> <span style='color:#2B91AF'>Product</span> { </br><br />
     &nbsp;  &nbsp; &nbsp; <span style='color:blue'>public</span> <span style='color:blue'>int</span> Id { <span style='color:blue'> get </span> ; <span style='color:blue'> set </span> ; }<br />
</br><br />
 &nbsp;  &nbsp; &nbsp; [<span style='color:#2B91AF'>Required</span>]<br />
 &nbsp;  &nbsp; &nbsp; [<span style='color:#2B91AF'>StringLength</span>(10)]<br />
 &nbsp;  &nbsp; &nbsp; <span style='color:blue'>public</span> <span style='color:blue'>string</span> Name { <span style='color:blue'>get</span>; <span style='color:blue'>set</span>; } </p>
<p> &nbsp;  &nbsp; &nbsp; [<span style='color:#2B91AF'>Required</span>]<br />
 &nbsp;  &nbsp; &nbsp; <span style='color:blue'>public</span> <span style='color:blue'>string</span> Description { <span style='color:blue'>get</span>; <span style='color:blue'>set</span>; }</p>
<p> &nbsp;  &nbsp; &nbsp; [<span style='color:#2B91AF'>DisplayName</span>(<span style='color:#A31515'>&quot;Price&quot;</span>)]<br />
 &nbsp;  &nbsp; &nbsp; [<span style='color:#2B91AF'>Required</span>]<br />
 &nbsp;  &nbsp; &nbsp; [<span style='color:#2B91AF'>RegularExpression</span>(<span style='color:#A31515'>@&quot;^\$?\d+(\.(\d{2}))?$&quot;</span>)]<br />
 &nbsp;  &nbsp; &nbsp; <span style='color:blue'>public</span> <span style='color:blue'>decimal</span> UnitPrice { <span style='color:blue'>get</span>; <span style='color:blue'>set</span>; }<br />
 &nbsp;  &nbsp; &nbsp;}</p>
<p>Now wouldn’t it be nice to have your model/entity just look like this. </p>
<p><span style='color:blue'>public</span> <span style='color:blue'>class</span> <span style='color:#2B91AF'>Product</span> <br />
{<br />
 &nbsp;  &nbsp; &nbsp; <span style='color:blue'>public</span> <span style='color:blue'>int</span> Id { <span style='color:blue'>get</span>; <span style='color:blue'>set</span>; }<br />
 &nbsp;  &nbsp; &nbsp; <span style='color:blue'>public</span> <span style='color:blue'>string</span> Name { <span style='color:blue'>get</span>; <span style='color:blue'>set</span>; }<br />
 &nbsp;  &nbsp; &nbsp; <span style='color:blue'>public</span> <span style='color:blue'>string</span> Description { <span style='color:blue'>get</span>; <span style='color:blue'>set</span>; }<br />
 &nbsp;  &nbsp; &nbsp; <span style='color:blue'>public</span> <span style='color:blue'>decimal</span> UnitPrice { <span style='color:blue'>get</span>;<span style='color:blue'>set</span>; }<br />
}</p>
<p>2 – Complex data validation with attributes makes your code get even more ugly.<a href=" http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx "> Take a lot at this example showing how</a> to achieve slightly more complexity with data annotations. Now trying to reuse and share attributes seem to make things more and more complex&#8230;.</p>
<p> 3- I think there may be a performance issue, as we need to extract the validation attributes using reflection. Now, while these are simple models, with simple validation rules, we may not notice the performance degrading, but I am sure that with numerous complex attributes, things might run a little slow. (I need to prove this though – maybe when I get time, I will write some tests – I could be wrong here, things might change in MVC 3) </p>
<p> So after looking at some examples, such as<a href="http://xval.codeplex.com/"> xVal –dead now</a>, <a href="http://www.lostechies.com/blogs/jimmy_bogard/archive/2007/10/24/entity-validation-with-visitors-and-extension-methods.aspx"> Entity validation with visitors and extension methods </a>these all did the job, but I would need to write lots of helpers, what I needed was some kind of framework for validation. <a href="http://fluentvalidation.codeplex.com/">Then I found fluent validation.</a>
</p>
<h3>Linking MVC with Fluent Validation </h3>
<p></p>
<p>So let us look at how we set our MVC project . Firstly, I want to automate the validation, so that any errors are automically added to the models state.  With some <a href="http://fluentvalidation.codeplex.com/Thread/View.aspx?ThreadId=76865">help from Jeremy</a>  I set up an customised BindAndValidate attribute.  Here is a simplified attribute we started with. </p>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">BindAndValidateAttribute</span> : <span style="color: #2b91af;">CustomModelBinderAttribute</span>,&nbsp; <span style="color: #2b91af;">IModelBinder</span> {</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">AttributedValidatorFactory</span> validatorFactory = <span style="color: blue;">new</span> <span style="color: #2b91af;">AttributedValidatorFactory</span>();</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: #2b91af;">IModelBinder</span> GetBinder() {</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">this</span>;</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;8</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">object</span> BindModel(<span style="color: #2b91af;">ControllerContext</span> controllerContext, <span style="color: #2b91af;">ModelBindingContext</span> bindingContext) {</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;9</span>&nbsp;</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;10</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> innerBinder = <span style="color: blue;">new</span> <span style="color: #2b91af;">DefaultModelBinder</span>();</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;11</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> boundInstance = innerBinder.BindModel(controllerContext, bindingContext);</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;12</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (boundInstance != <span style="color: blue;">null</span>)</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;13</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ValidateInstance(boundInstance, bindingContext);</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;14</span>&nbsp;</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;15</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> boundInstance;</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;16</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;17</span>&nbsp;</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;18</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">void</span> ValidateInstance(<span style="color: blue;">object</span> instance, <span style="color: #2b91af;">ModelBindingContext</span> context) {</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;19</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> validator = validatorFactory.GetValidator(context.ModelType);</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;20</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (validator != <span style="color: blue;">null</span>)</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;21</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;22</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">var</span> result = validator.Validate(instance);</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;23</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; result.AddToModelState(context.ModelState, <span style="color: #a31515;">&quot;&quot;</span>);</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;24</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;25</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;26</span>&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></p>
<p>Now all I need to do is hook this up with my controller method. In the Sign in method, I add the attribute [BindAndValidatiate] and all I need to do is check that the model state is valid. If so, I perform the log in.</p>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">AccountController</span> : <span style="color: #2b91af;">BaseController</span> {</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">ActionResult</span> Signin() {</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> View();</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; [<span style="color: #2b91af;">HttpPost</span>]</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: #2b91af;">ActionResult</span> Signin([<span style="color: #2b91af;">BindAndValidate</span>] <span style="color: #2b91af;">ChangeEmailModel</span> model) {</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;8</span>&nbsp;</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;9</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (ModelState.IsValid)</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;10</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; PerformSignIn();</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;11</span>&nbsp;</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;12</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> View();</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;13</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;14</span>&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></p>
<h3>Linking MVC View Models with Fluent Validation</h3>
<p></p>
<p>Lets look at how all this works. What we need to do is create out model, then create our validator. We then hook our validator to our model by adding the validator atrribute to our model. I am using a simple Register View Model here as an example.</p>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color: #2b91af;">Validator</span>(<span style="color: blue;">typeof</span>(<span style="color: #2b91af;">RegisterViewModelValidator</span>))]</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">RegisterViewModel</span></p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;&nbsp;&nbsp;&nbsp; {</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Email { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> Password { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">string</span> ConfirmPassword { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></p>
<p>Now for the model validator. Here we have some simple rules. The password must not be empty, and it must also be a good password. The password confirmation must be the same as the password, and finally the Email must be a valid email address, and also not already exisit in our site. </p>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">RegisterViewModelValidator</span> : <span style="color: #2b91af;">AbstractValidator</span>&lt;<span style="color: #2b91af;">RegisterViewModel</span>&gt; {</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> RegisterViewModelValidator() {</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; RuleFor(reg =&gt; reg.Password)</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .NotEmpty()</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .WithMessage(<span style="color: #a31515;">&quot;Please provide a password&quot;</span>)</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .Must(BeGoodPassword)</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .WithMessage(<span style="color: #a31515;">&quot;Password must be at least 8 characters long, and contain numbers and letters&quot;</span>);</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;8</span>&nbsp;</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;9</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; RuleFor(reg =&gt; reg.ConfirmPassword)</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;10</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .Equal(reg =&gt; reg.Password)</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;11</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; .WithMessage(<span style="color: #a31515;">&quot;Passwords do not match&quot;</span>);</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;12</span>&nbsp;</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;13</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; RuleFor(reg =&gt; reg.Email)</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;14</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; .EmailAddress()</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;15</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; .WithMessage(<span style="color: #a31515;">&quot;Invalid Email&quot;</span>)</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;16</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; .Must(BeUniqueEmail)</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;17</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; .WithMessage(<span style="color: #a31515;">&quot;Account already Registered&quot;</span>);</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;18</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;19</span>&nbsp;</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;20</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">bool</span> BeGoodPassword(<span style="color: blue;">string</span> password) {</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;21</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af;">Regex</span> regex = <span style="color: blue;">new</span> <span style="color: #2b91af;">Regex</span>(<span style="color: #a31515;">@&quot;^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*$&quot;</span>);</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;22</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> regex.IsMatch(password);</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;23</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;24</span>&nbsp;</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;25</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">bool</span> BeUniqueEmail(<span style="color: blue;">string</span> email) {</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;26</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">int</span> count = <span style="color: #2b91af;">Repository</span>.GetInstance().CountOccurrencesOfEmail(email);</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;27</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> (count == 0);</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;28</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;29</span>&nbsp;&nbsp;&nbsp;&nbsp; }</p>
</div>
<p></p>
<h3>Test your validation</h3>
<p>Now, finally for the testing, whcih is really useful, when I want to make sure that things work 100%! To keep it simepl, I am just going to test the password rule, because testing the email requires a lot more of an explination. So working from some <a href="http://fluentvalidation.codeplex.com/wikipage?title=Testing">simple examples Here: </a>I have written three tests. Thfirst is to make sure that the password can not be null. The second is to catch a week password. The third makes sure that a strong password does not cause an error. </p>
<div style="font-family: Courier New; font-size: 10pt; color: black; background: white;">
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; [<span style="color: #2b91af;">Test</span>]</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> Should_have_error_when_Password_is_null() {</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; validator.ShouldHaveValidationErrorFor(reg =&gt; reg.Password, <span style="color: blue;">null</span> <span style="color: blue;">as</span> <span style="color: blue;">string</span>);</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; [<span style="color: #2b91af;">Test</span>]</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> Should_have_error_when_Password_is_weak() {</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;8</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; validator.ShouldHaveValidationErrorFor(reg =&gt; reg.Password, <span style="color: #a31515;">&quot;weakpass&quot;</span>);</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;&nbsp;9</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;10</span>&nbsp;</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;11</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; [<span style="color: #2b91af;">Test</span>]</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;12</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">void</span> Should_not_have_error_when_Password_is_strong() {</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;13</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Validator.ShouldNotHaveValidationErrorFor(reg =&gt; reg.Password, <span style="color: #a31515;">&quot;SecretPassword123&quot;</span>);</p>
<p style="margin: 0px;"><span style="color: #2b91af;">&nbsp;&nbsp;&nbsp;14</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
</div>
<p></p>
<p>And that is it! There is quite a lot you can achieve with fluent validation, such as <a href="http://fluentvalidation.codeplex.com/wikipage?title=CreatingAValidator&#038;referringTitle=Documentation&#038;ANCHOR#ReusingValidators">reusing validators</a>  on complex properties and also some useful conditions like<a href="http://fluentvalidation.codeplex.com/wikipage?title=Customising&#038;referringTitle=Documentation&#038;ANCHOR#WhenUnless"> when or unless</a>! The reasons I like this are that it uses generics to help build clean code. There is now no need to attributes on every property I have. Also, Jeremy was also very quick to help with any questions I had. <a href="http://www.jeremyskinner.co.uk/">Thanks for the help Jeremy.</a> </p>
<div><table> <td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://www.webdevbros.net/2010/12/03/asp-net-mvc-fluent-validation-and-tesing/&amp;t=ASP.Net+MVC%2C+Fluent+Validation+and+testing&amp;s=normal' height='80' width='52' frameborder='0' scrolling='no'></iframe></td> <td><iframe src='http://www.reddit.com/button_content?newwindow=1&amp;url=http://www.webdevbros.net/2010/12/03/asp-net-mvc-fluent-validation-and-tesing/&amp;title=ASP.Net+MVC%2C+Fluent+Validation+and+testing&amp;t=2 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http://www.webdevbros.net/2010/12/03/asp-net-mvc-fluent-validation-and-tesing/&amp;title=ASP.Net+MVC%2C+Fluent+Validation+and+testing&amp;t=1 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><script type="text/javascript"><!--yahooBuzzArticleHeadline=ASP.Net+MVC%2C+Fluent+Validation+and+testing;//--></script><script type="text/javascript" src="http://d.yimg.com/ds/badge2.js" badgetype=square></script></td> <td><script type="text/javascript">tweetmeme_url='http://www.webdevbros.net/2010/12/03/asp-net-mvc-fluent-validation-and-tesing/'; tweetmeme_style = 'normal';; </script><script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js" ></script></td></table></div><!-- This is a HTML comment, it will not display in any page. Feel free to remove this comment if it cause any inconvenient to you.
	Thanks for using digg digg, please visit http://www.mkyong.com/blog/digg-digg-wordpress-plugin for any comments and ideas, 
	
    Author : Yong Mook Kim
    Website : http://www.mkyong.com
	-->]]></content:encoded>
			<wfw:commentRss>http://www.webdevbros.net/2010/12/03/asp-net-mvc-fluent-validation-and-tesing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Nhibernate returns duplicate results on paged data sets &#8211; work around</title>
		<link>http://www.webdevbros.net/2010/11/11/nhibernate-returns-duplicate-results-on-paged-data-sets-work-around/</link>
		<comments>http://www.webdevbros.net/2010/11/11/nhibernate-returns-duplicate-results-on-paged-data-sets-work-around/#comments</comments>
		<pubDate>Thu, 11 Nov 2010 12:02:13 +0000</pubDate>
		<dc:creator>Dai Bok</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[nhibernate]]></category>

		<guid isPermaLink="false">http://www.webdevbros.net/?p=909</guid>
		<description><![CDATA[There is a problem with Nhibernate, when paging record sets, using SQLServer 2008. This offers a work around, to fix this problem.]]></description>
			<content:encoded><![CDATA[<p>Recently, while implementing a page-able data grid with <a href="http://nhforge.org/">nHibernate </a>and  <a href="http://mvccontrib.codeplex.com/">MVC Contrib Grid</a>, I came across a strange problem. My result set had duplicates, and the strange thing was that it would only happen when paging my record set. Anyway, I thought I would write a little post about how I solved the problem, just in case someone else comes across it. </p>
<p>Firstly, lets look at simplified description of the problem. Those using MySQL have the luxury of limit, which makes paging data sets a breeze, but in SQL Server(and Oracle) things get a bit more “tricky”. The trick is to count the rows on the result set, using RowCount over something, and mix that in with a sub query, but there is a problem with nHibernate T-SQL 2005 Dialect. The RowCount was being used on the sub-query, and not the parent query. Now that I had discovered the problem.</p>
<blockquote><p><strong> &#8220;ROW_NUMBER() OVER(ORDER BY CURRENT_TIMESTAMP) as __hibernate_sort_row&#8221;</strong> is in the wrong place!</p></blockquote>
<p>After some research, I found that other people were having this problem too.<a href="http://www.daczkowski.net/2010/09/07/rows-duplication-for-certain-nhibernate-queries-%E2%80%93-workaround/"> Marcin Daczkowski has an excellent work-around, that he blogged about</a>, he also describes the problem with some NH generated SQL examples, I won’t repeat myself here, so have a look at his post if you are not sure and check his <a href="http://216.121.112.228/browse/NH-2214">bug report here</a>. </p>
<p><a href="http://julianjelfs.wordpress.com/2009/04/03/nhibernate-removing-duplicates-combined-with-paging/">I found a another solution here too- not sure this one works though</a>, ultimately I had to come up with a solution that suited my project.</p>
<p>There are some reasons why I can’t use Marcin Daczkowski solution.<br />
<br />
<strong> &#8211; Firstly</strong>, it does not look like the guys at nHibernate be able to release the patched version of nHibernate any time soon,I guess they are very busy working hard on version 3.0! Can&#8217;t wait for that realease!  <a href="http://216.121.112.228/browse/NH-2214?focusedCommentId=20075&amp;page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#action_20075">see the comments here.</a><br />
<br />
<strong> &#8211; Secondly</strong>, if I build my own version of nHibernate, I will need to also rebuild all my dependencies, linking them with Marcins patched version. That means <strong>FluentNHibernate</strong> needs to be rebuilt, <strong>NHibernate.Caches.SysCache</strong> needs to be rebuilt, <strong>NHibernate.ByteCode.Castle</strong> needs to be rebuilt, you get the picture?</p>
<p>So after some thought and source code investigation, I came up with the idea of making a customised Dialect, and just use Darcins patched files. Now, I don’t need to build a patched version of my all my open source dependencies, as I have my own SQL dialect set up in the fluent configuration.</p>
<p><code><br />
        _sessionFactory = Fluently.<span style="color:#3399CC">Configure</span>()<br />
 &nbsp; &nbsp;  &nbsp; .Database(<span style="color:#3399CC">MsSqlConfiguration</span><br />
 &nbsp; &nbsp;  &nbsp; .MsSql2008<br />
 &nbsp; &nbsp;  &nbsp; .ConnectionString(connectionString)<br />
 &nbsp; &nbsp;  &nbsp; .CurrentSessionContext(<span style="color:#CC3300">"web"</span>)<br />
 &nbsp; &nbsp;  &nbsp; .Dialect(<span style="color:#3333CC">CustomSQL2008Dialect</span>)<br />
                )<br />
</code></p>
<p> My Dialect classes are set up like this:</p>
<p><code><br />
<span style="color:#3333CC"> public class </span> <span style="color:#3399CC"> CustomSQL2008Dialect: CustomSQL2005Dialect </span> {<br />
 &nbsp; &nbsp;  &nbsp; <span style="color:#3333CC">public </span> <span style="color:#3399CC">  GWMsSql2008Dialect</span> () {<br />
 &nbsp; &nbsp;  &nbsp;  &nbsp; &nbsp;  &nbsp;  <span style="color:#347C17"> // Duplicate of the contents of MsSsql2008Dialect constructor  goes here </span><br />
&nbsp; &nbsp;  &nbsp;   }<br />
 }</p>
<p><span style="color:#3333CC"> public class </span> <span style="color:#3399CC"> CustomSQL2005Dialect: MsSql2000Dialect </span> {<br />
 &nbsp; &nbsp;  &nbsp; <span style="color:#347C17"> // the contents Darcins MsSql2005Dialect file goes here.<br />
 &nbsp; &nbsp;  &nbsp; // <a href="http://216.121.112.228/browse/NH-2214"> MsSql2005Dialect at NHibernate JIRA</a></span><br />
 }<br />
</code></p>
<p>Luckily, I have unit tests set up for all my repository methods, and after seeing the green bar in NUnit, I was more than satisfied with the custom dialect. I hope the guys at NHibernate manage to get things working in their next release, keep up the good work guys! And a special thanks to<br />
to Darcin, for writing the patch. </p>
<p>
Here is a copy <a href='http://www.webdevbros.net/wp-content/uploads/2011/01/CustomSql2008Dialect.cs_.zip'>the Custom Sql 2008 Dialect</a>. </p>
<div><table> <td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://www.webdevbros.net/2010/11/11/nhibernate-returns-duplicate-results-on-paged-data-sets-work-around/&amp;t=Nhibernate+returns+duplicate+results+on+paged+data+sets+-+work+around&amp;s=normal' height='80' width='52' frameborder='0' scrolling='no'></iframe></td> <td><iframe src='http://www.reddit.com/button_content?newwindow=1&amp;url=http://www.webdevbros.net/2010/11/11/nhibernate-returns-duplicate-results-on-paged-data-sets-work-around/&amp;title=Nhibernate+returns+duplicate+results+on+paged+data+sets+-+work+around&amp;t=2 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http://www.webdevbros.net/2010/11/11/nhibernate-returns-duplicate-results-on-paged-data-sets-work-around/&amp;title=Nhibernate+returns+duplicate+results+on+paged+data+sets+-+work+around&amp;t=1 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><script type="text/javascript"><!--yahooBuzzArticleHeadline=Nhibernate+returns+duplicate+results+on+paged+data+sets+-+work+around;//--></script><script type="text/javascript" src="http://d.yimg.com/ds/badge2.js" badgetype=square></script></td> <td><script type="text/javascript">tweetmeme_url='http://www.webdevbros.net/2010/11/11/nhibernate-returns-duplicate-results-on-paged-data-sets-work-around/'; tweetmeme_style = 'normal';; </script><script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js" ></script></td></table></div><!-- This is a HTML comment, it will not display in any page. Feel free to remove this comment if it cause any inconvenient to you.
	Thanks for using digg digg, please visit http://www.mkyong.com/blog/digg-digg-wordpress-plugin for any comments and ideas, 
	
    Author : Yong Mook Kim
    Website : http://www.mkyong.com
	-->]]></content:encoded>
			<wfw:commentRss>http://www.webdevbros.net/2010/11/11/nhibernate-returns-duplicate-results-on-paged-data-sets-work-around/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Role Based Access Control in ASP.Net MVC</title>
		<link>http://www.webdevbros.net/2009/12/16/role-based-access-control-in-asp-net-mvc/</link>
		<comments>http://www.webdevbros.net/2009/12/16/role-based-access-control-in-asp-net-mvc/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 12:59:24 +0000</pubDate>
		<dc:creator>Dai Bok</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[general stuff]]></category>
		<category><![CDATA[Access Control]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Bitwise Operations]]></category>

		<guid isPermaLink="false">http://www.webdevbros.net/?p=874</guid>
		<description><![CDATA[Currently I am looking at access control systems, and how best to integrate them with ASP.Net MVC framework. While this framework already provides support for role based access control (RBAC), using the membership classes. I need to implement this on a legacy database, and some how integrate the old system with asp.net forms authentication. This [...]]]></description>
			<content:encoded><![CDATA[<p>Currently I am looking at access control systems, and how best to integrate them with ASP.Net MVC framework. While this framework already provides support for role based access control (RBAC), <a href="http://msdn.microsoft.com/en-us/library/system.web.security.membership.aspx">using the membership classes</a>. I need to implement this on a legacy database, and some how integrate the old system with asp.net forms authentication. This post is about how I realised this, and acts a potential solution. If you can think of a better way, of find any devastating flaws, let me know. ;-)</p>
<p>The scenario is simple, we have four roles defined for the system. They are Students, Graduates, Staff and Administrators. Some staff can be graduates, (or even Students). Administrators are, of course staff! So how you model this? We already know of one bitwise <a href="http://www.webdevbros.net/2006/12/02/toggle-states-like-active-deleted-etc-bitwise-negation-in-sql/">trick from Michal’s post</a>, so let us see how we can use bitwise operations to make this a reality!</p>
<p>First let us revise the results of the bitwise AND operations. You can check<a href="http://en.wikipedia.org/wiki/Bitwise_operation"> Wikipedia for full details</a>.</p>
<p align="center">
<table style="border:1px solid black" cellspacing="0">
<tr>
<td>1
<td>&amp;
<td>0
<td>=
<td>0 </tr>
<tr>
<td>0
<td>&amp;
<td>1
<td>=
<td>0 </tr>
<tr>
<td>0
<td>&amp;
<td>0
<td>=
<td>0 </tr>
<tr>
<td>1
<td>&amp;
<td>1
<td>=
<td>1 </tr>
</table>
<p>
</p>
<p>Converting these back to decimal 1001 is 9 and 0101 is 5. So 9 &amp; 5 = 8. If we convert each of these bits to represent a role in our system, we can come up with a table like this. </p>
<p align="center">
<table style="border:1px solid black" cellspacing="0">
<tr>
<td>Bit 1
<td>0 (false)
<td>Student</tr>
<tr>
<td>Bit 2
<td>0 (false)
<td>Graduate</tr>
<tr>
<td>Bit 3
<td>0 (false)
<td>Staff</tr>
<tr>
<td>Bit 4
<td>1 (true)
<td>Admin</tr>
</table>
<p>
</p>
<p>So a user of the system with a role number of 8 is an Admin, but in our case, an Admin is also a member of staff, and in fact, a member of staff could also be a student or a graduate. This is where using bitwise operations can really help model such a situation.  To get it working, a staff member who is a student will have bits 1 and 3 set to true, while a graduate who is also a staff member will have bits 2 and 3 set to true. We can represent these roles in decimal as User(Staff &amp; Graduate) = 6, while User (Staff &amp; Student) = 5. Get the picture?</p>
<p>Let’s look at a simple real world example. First we have a User class, with a Role property of the type int. The reason we use an integer, is that is can be easily stored in the database.</p>
<div style="font-family: Courier New;font-size: 10pt;color: black;background: white">
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">class</span> <span style="color: #2b91af">User</span> {</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">string</span> Name { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">int</span> Role { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">bool</span> IsInRole(<span style="color: #2b91af">Role</span> role) {</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: green">//todo</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> <span style="color: blue">false</span>;</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;8</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;9</span>&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p>
</div>
<p>We also need to create an enumeration, with a Flags attribute. The flags attribute tells the compiler that this enumeration can be treated as a bit field. We then define a value for each role. The reason for using exponents of 2 should become clearer later. </p>
<div style="font-family: Courier New;font-size: 10pt;color: black;background: white">
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;&nbsp;&nbsp;&nbsp; [<span style="color: #2b91af">Flags</span>]</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">enum</span> <span style="color: #2b91af">Role</span> {</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Student = 1,&nbsp;&nbsp;&nbsp; <span style="color: green">// 0001</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Employer = 2,&nbsp;&nbsp; <span style="color: green">// 0010</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Staff = 4,&nbsp;&nbsp;&nbsp; &nbsp; <span style="color: green">// 0100</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Admin = 8&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <span style="color: green">// 1000</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p>
</div>
<p>The menu of our website needs to be generated depending on the user role.  The menu selection code below should generate the correct menu depending on the user role.</p>
<div style="font-family: Courier New;font-size: 10pt;color: black;background: white">
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">&lt;</span><span style="color: #a31515">div</span> <span style="color: red">class</span><span style="color: blue">=&quot;LeftMenu&quot;&gt;</span>&nbsp; </p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="background: #ffee62">&lt;%</span> <span style="color: blue">if</span> (user.IsInRole(<span style="color: #2b91af">Role</span>.Student)) <span style="background: #ffee62">%&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="background: #ffee62">&lt;%</span> Html.RenderPartial(<span style="color: #a31515">&quot;StudentMenu&quot;</span>); <span style="background: #ffee62">%&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="background: #ffee62">&lt;%</span> <span style="color: blue">if</span> (user.IsInRole(<span style="color: #2b91af">Role</span>.Graduate)) <span style="background: #ffee62">%&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="background: #ffee62">&lt;%</span> Html.RenderPartial(<span style="color: #a31515">&quot;GraduateMenu&quot;</span>); <span style="background: #ffee62">%&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;8</span>&nbsp;</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;9</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="background: #ffee62">&lt;%</span> <span style="color: blue">if</span> (user.IsInRole(<span style="color: #2b91af">Role</span>.Staff)) <span style="background: #ffee62">%&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;10</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="background: #ffee62">&lt;%</span> Html.RenderPartial(<span style="color: #a31515">&quot;StaffMenu&quot;</span>); <span style="background: #ffee62">%&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;11</span>&nbsp;</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;12</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="background: #ffee62">&lt;%</span> <span style="color: blue">if</span> (user.IsInRole(<span style="color: #2b91af">Role</span>.Admin)) <span style="background: #ffee62">%&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;13</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="background: #ffee62">&lt;%</span> Html.RenderPartial(<span style="color: #a31515">&quot;AdminMenu&quot;</span>); <span style="background: #ffee62">%&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;14</span>&nbsp;</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;15</span>&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: blue">&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;</span></p>
<p>
</div>
<p>Ok, so let see where the magic happens!  If we <a href="http://msdn.microsoft.com/en-us/library/sbf85k1c.aspx">AND (&amp;)</a> the user assigned role, with the role required, and we compare this result to the role required, we can determine if a user is in the role.  Summarised, the end result of the AND operation needs to equal that of the role required. In user class we have the method:</p>
<div style="font-family: Courier New;font-size: 10pt;color: black;background: white">
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">public</span> <span style="color: blue">bool</span> IsInRole(<span style="color: #2b91af">Role</span> role) {</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">Role</span> userRole = (<span style="color: #2b91af">Role</span>)<span style="color: blue">this</span>.Role;</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue">return</span> ((userRole &amp; role) == role);</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</p>
<p>
</div>
<p>Looking at some binary examples, we can see how it works. In the first example, an admin user wants accesses a graduate item. </p>
<p align="center">
<table style="border:1px solid black" cellspacing="0">
<tr>
<td>Role Required
<td style="color:red">Staff(4)
<td>0 1 0 0</tr>
<tr>
<td>User Role
<td style="color:green">Admin (8)
<td>1 0 0 0</tr>
<tr>
<td>Result of &amp;
<td style="color:red"> Access Denied (0)
<td>0 0 0 0</tr>
</table>
<p></br>
</p>
<p>It is clear that we have a problem here, because we said that admin could be both staff, and staff may also be graduates.  What we need to do is add up the roles, so that this user will access both admin and staff content. Assigning the user the role of Admin and Staff is easy. All we do is: </p>
<div style="font-family: Courier New;font-size: 10pt;color: black;background: white">
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;1</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: #2b91af">User</span> user = <span style="color: blue">new</span> <span style="color: #2b91af">User</span>();</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; user.Role = (<span style="color: blue">int</span>)<span style="color: #2b91af">Role</span>.Staff;</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; user.Role += (<span style="color: blue">int</span>) <span style="color: #2b91af">Role</span>.Admin;</p>
<p>
</div>
<p>And the resulting table is: </p>
<table style="border:1px solid black" cellspacing="0">
<tr>
<td>Role Required
<td style="color:red">Staff(4)
<td>0 1 0 0</tr>
<tr>
<td>User Role
<td style="color:green">Admin  + Staff (12)
<td>1 1 0 0</tr>
<tr>
<td>Result of &amp;
<td style="color:green">Access Granted (4)
<td>0 1 0 0</tr>
</table>
<p>
</p>
<p>Now we can easily draw our menu depending on the roles assigned to a user. Adding or removing roles for a user is also easy, just add it or subtract it. I wrote a little project to go with this so you can test it our your self.  Thanks to Michi for introducing  this, and Dan for helping work it out! </p>
<p>Download the <a href='http://www.webdevbros.net/wp-content/uploads/2009/12/Roles.zip'>Roles sample project </a>  You’ll need to use <a href="http://www.nunit.org/">nUnit to test it</a>. </p>
<div><table> <td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://www.webdevbros.net/2009/12/16/role-based-access-control-in-asp-net-mvc/&amp;t=Role+Based+Access+Control+in+ASP.Net+MVC&amp;s=normal' height='80' width='52' frameborder='0' scrolling='no'></iframe></td> <td><iframe src='http://www.reddit.com/button_content?newwindow=1&amp;url=http://www.webdevbros.net/2009/12/16/role-based-access-control-in-asp-net-mvc/&amp;title=Role+Based+Access+Control+in+ASP.Net+MVC&amp;t=2 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http://www.webdevbros.net/2009/12/16/role-based-access-control-in-asp-net-mvc/&amp;title=Role+Based+Access+Control+in+ASP.Net+MVC&amp;t=1 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><script type="text/javascript"><!--yahooBuzzArticleHeadline=Role+Based+Access+Control+in+ASP.Net+MVC;//--></script><script type="text/javascript" src="http://d.yimg.com/ds/badge2.js" badgetype=square></script></td> <td><script type="text/javascript">tweetmeme_url='http://www.webdevbros.net/2009/12/16/role-based-access-control-in-asp-net-mvc/'; tweetmeme_style = 'normal';; </script><script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js" ></script></td></table></div><!-- This is a HTML comment, it will not display in any page. Feel free to remove this comment if it cause any inconvenient to you.
	Thanks for using digg digg, please visit http://www.mkyong.com/blog/digg-digg-wordpress-plugin for any comments and ideas, 
	
    Author : Yong Mook Kim
    Website : http://www.mkyong.com
	-->]]></content:encoded>
			<wfw:commentRss>http://www.webdevbros.net/2009/12/16/role-based-access-control-in-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>picnik comes with a free-to-use image editing API for your website</title>
		<link>http://www.webdevbros.net/2008/01/23/picnik-comes-with-a-free-to-use-image-editing-api-for-your-website/</link>
		<comments>http://www.webdevbros.net/2008/01/23/picnik-comes-with-a-free-to-use-image-editing-api-for-your-website/#comments</comments>
		<pubDate>Wed, 23 Jan 2008 03:32:11 +0000</pubDate>
		<dc:creator>Michal</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[classic ASP (VBScript)]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://www.webdevbros.net/2008/01/23/picnik-comes-with-a-free-to-use-image-editing-api-for-your-website/</guid>
		<description><![CDATA[Ever heard from Picnik? It allows you editing your images direclty within your browser. Forget Adobe Photoshop :) Picnik has been around for a while now but recently it started to offer its service for the public. for free! I played around a bit and must say that it is amazing .. Use it within [...]]]></description>
			<content:encoded><![CDATA[<p><img src='http://www.webdevbros.net/wp-content/uploads/2008/01/picnic.gif' alt='picnic.gif' style="float:left"/> Ever heard from <a href="http://www.picnik.com">Picnik</a>? It allows you editing your images direclty within your browser. Forget Adobe Photoshop :) Picnik has been around for a while now but recently it started to offer its service for the public. for free! I played around a bit and must say that it is amazing .. Use it within PHP, classic ASP, .net, Ruby on Rails  or whatever... especially i like the idea of the whole service which works like this...</p>
<ol>
<li>Send your images to their service (existing image via URL or encoded as multi-part)</li>
<li>Then the user modifies the picture (size, colors, rotation, special effects...)</li>
<li>Picnik sends you the picture back to your server (either an URL where you can download it or directly as multi-part image)</li>
</ol>
<p>Here comes an example I have built for demonstration...<span id="more-149"></span>Click on the picture below to edit it.</p>
<p><a href="http://www.picnik.com/service/?_apikey=178ef7667a0c301dd8eb89e76caba626&#038;_import=http%3A//www.webdevbros.net/wp-content/uploads/2008/01/googleapple.jpg&#038;_export=http%3A//www.webdevbros.net/2008/01/23/picnik-comes-with-a-free-to-use-image-editing-api-for-your-website&#038;_export_title=back%20to%20webdevbros"><img src='http://www.webdevbros.net/wp-content/uploads/2008/01/googleapple.jpg' alt='googleapple.jpg' /></a></p>
<p>Everything realized by using a link to this URL:</p>
<div class="igBar"><span id="lhtml-2"><a href="#" onclick="javascript:showCodeTxt('html-2'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">HTML:</span>
<div id="html-2">
<div class="html">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">http://www.picnik.com/service/</div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; ?_apikey=178ef7667a0c301dd8eb89e76caba626</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; <span style="color: #ddbb00;">&amp;_import=http%3A//www.webdevbros.net/wp-content/uploads/2008/01/googleapple.jpg</div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &amp;_export=http%3A//www.webdevbros.net/2008/01/23/picnik-comes-with-a-free-to-use-image-editing-api-for-your-website</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &amp;_export_title=back%20to%20webdevbros </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Pretty cool isn't it? Thats the basic example but you can customize a lot more (see their <a href="http://www.picnik.com/info/api/reference">API</a>). Also I didn't progress the picture back on my server.</p>
<p>If you want to get started i recommend to start with their <a href="http://www.picnik.com/info/api/tutorials">tutorials</a>. They are nicely written and done in 10 minutes.</p>
<div><table> <td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://www.webdevbros.net/2008/01/23/picnik-comes-with-a-free-to-use-image-editing-api-for-your-website/&amp;t=picnik+comes+with+a+free-to-use+image+editing+API+for+your+website&amp;s=normal' height='80' width='52' frameborder='0' scrolling='no'></iframe></td> <td><iframe src='http://www.reddit.com/button_content?newwindow=1&amp;url=http://www.webdevbros.net/2008/01/23/picnik-comes-with-a-free-to-use-image-editing-api-for-your-website/&amp;title=picnik+comes+with+a+free-to-use+image+editing+API+for+your+website&amp;t=2 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http://www.webdevbros.net/2008/01/23/picnik-comes-with-a-free-to-use-image-editing-api-for-your-website/&amp;title=picnik+comes+with+a+free-to-use+image+editing+API+for+your+website&amp;t=1 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><script type="text/javascript"><!--yahooBuzzArticleHeadline=picnik+comes+with+a+free-to-use+image+editing+API+for+your+website;//--></script><script type="text/javascript" src="http://d.yimg.com/ds/badge2.js" badgetype=square></script></td> <td><script type="text/javascript">tweetmeme_url='http://www.webdevbros.net/2008/01/23/picnik-comes-with-a-free-to-use-image-editing-api-for-your-website/'; tweetmeme_style = 'normal';; </script><script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js" ></script></td></table></div><!-- This is a HTML comment, it will not display in any page. Feel free to remove this comment if it cause any inconvenient to you.
	Thanks for using digg digg, please visit http://www.mkyong.com/blog/digg-digg-wordpress-plugin for any comments and ideas, 
	
    Author : Yong Mook Kim
    Website : http://www.mkyong.com
	-->]]></content:encoded>
			<wfw:commentRss>http://www.webdevbros.net/2008/01/23/picnik-comes-with-a-free-to-use-image-editing-api-for-your-website/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C# tips, tricks and things you should know</title>
		<link>http://www.webdevbros.net/2007/11/14/c-tips-tricks-and-things-you-should-know/</link>
		<comments>http://www.webdevbros.net/2007/11/14/c-tips-tricks-and-things-you-should-know/#comments</comments>
		<pubDate>Wed, 14 Nov 2007 11:33:25 +0000</pubDate>
		<dc:creator>Michal</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[programming style]]></category>

		<guid isPermaLink="false">http://www.webdevbros.net/2007/11/14/c-tips-tricks-and-things-you-should-know/</guid>
		<description><![CDATA[During my previous holidays (i have to admit 1.5 months surfing in portugal) I had some literature with me which also included the book Programming .net components released with O'Reilly. It talks about the aspects of components in general and provides good examples how to achieve those in .net. Although these pages are a very [...]]]></description>
			<content:encoded><![CDATA[<p><img src='http://www.webdevbros.net/wp-content/uploads/2007/11/dotnetcomponents.jpg' alt='.net components' align='left' /> During my previous holidays (i have to admit 1.5 months surfing in portugal) I had some literature with me which also included the book <a href="http://www.amazon.com/Programming-NET-Components-Juval-Lowy/dp/0596102070/ref=pd_bbs_sr_1?ie=UTF8&#038;s=books&#038;qid=1195026384&#038;sr=8-1">Programming .net components</a> released with O'Reilly. It talks about the aspects of components in general and provides good examples how to achieve those in .net. Although these pages are a very good read (rather for experienced devs) for ppl interested in component development, I found it more interesting because of its general C# content. There are concepts &#038; approaches, small tips &#038; tricks and intersting things about C# which can be used not only in component based architectures and are therefore good to know anyway. </p>
<p>As I always make notes while reading "geek" books I thought: "so why not summarize them all up in a nice list and blog'em". Here we go...<span id="more-115"></span></p>
<p><strong>1. implicit vs explicit interface</strong><br />
implicit interface implementation allows to use the interface member through an object or through the interface whereas the explicit interface implementation allows only access through the interface.</p>
<div class="igBar"><span id="lcsharp-15"><a href="#" onclick="javascript:showCodeTxt('csharp-15'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-15">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">//public interface IInterface {</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">void</span> Foo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">//implicit implementation</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> AClass : IInterface <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Foo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">//explicit implementation</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> BClass : IInterface <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">void</span> IInterface.<span style="color: #0000FF;">Foo</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">//usage implicit</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">AClass a = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> AClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">a.<span style="color: #0000FF;">Foo</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">IInterface b = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> AClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">b.<span style="color: #0000FF;">Foo</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">//usage explicit</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">IInterface c = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> BClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">c.<span style="color: #0000FF;">Foo</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">BClass d = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> BClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">d.<span style="color: #0000FF;">Foo</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">//Fails because of explicit implementation!!! </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Visual studio just offers the choice of the implementation but most people don't know the difference. With explicit implementation the server (implementing the interface) can prevent clients from using it directly on object which implements it. It forces that the members of the interfaces are only accessible via the interface itself</p>
<p><strong>2. implicit vs explicit cast</strong><br />
this is just to get used to the terminology because a lot of guys are using it but don't know the terminology ;)</p>
<div class="igBar"><span id="lcsharp-16"><a href="#" onclick="javascript:showCodeTxt('csharp-16'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-16">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">//implicit cast</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">T x = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> T</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">//explicit cast</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">T x = <span style="color: #000000;">&#40;</span>T<span style="color: #000000;">&#41;</span>y</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">//explicit cast (defensive)</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">T x = y <span style="color: #0600FF;">as</span> T </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Here you see that implicit casting is assigning a class instance directly to a variable. Explicit means casting from one type to another. </p>
<p><strong>3. Destructors</strong><br />
A destructor of a class is defined with ~ClassName. In comparison to Dispose() the C# destructor is always called. Dispose() needs to be called by the client whereas the destructor is called when the garbage collector comes around. Best way is to implement both and channel the implementation to the method which does the cleanup. e.g. cleanup. Example of implementation [COMP05, 98]:</p>
<div class="igBar"><span id="lcsharp-17"><a href="#" onclick="javascript:showCodeTxt('csharp-17'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-17">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MyBase : IDisposable <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">bool</span> _disposed = <span style="color: #0600FF;">false</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">protected</span> <span style="color: #FF0000;">bool</span> Disposed <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; get <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">lock</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> _disposed;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Dispose<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">lock</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>!_disposed<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CleanUp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _disposed = <span style="color: #0600FF;">true</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GC.<span style="color: #0000FF;">SuppressFinalize</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">//the method for the subclasses if they need to release resources</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">virtual</span> <span style="color: #0600FF;">void</span> CleanUp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;~MyBase<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; CleanUp<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Classes deriving from this baseclass can now implement their destruction by overriding the cleanup method and can now decide to do the clean up manually (calling Dispose) or leave it till the GC comes around. Important here is that if Dispose() was called we tell the garbage collector to exclude this instance from being garbaged later (with GC.SuppressFinalize()).</p>
<p><strong>4. use "Using" defensive</strong></p>
<div class="igBar"><span id="lcsharp-18"><a href="#" onclick="javascript:showCodeTxt('csharp-18'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-18">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>obj <span style="color: #0600FF;">as</span> IDisposable<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
This approach will only call Dispose() if the object implements IDisposeable. Good if you dont really know if the object needs to release resources or not and if you want to be ready for future changes. e.g. the server decides to implement IDisposeable.</p>
<p><strong>5. Delegate inference</strong></p>
<div class="igBar"><span id="lcsharp-19"><a href="#" onclick="javascript:showCodeTxt('csharp-19'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-19">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">//instead</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">publisher.<span style="color: #0000FF;">someDelegate</span> += <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #FF0000;">Delegate</span><span style="color: #000000;">&#40;</span>someMethod<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">//u can use</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">publisher.<span style="color: #0000FF;">someDelegate</span> += someMethod </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This is much nicer to read but only supported with .net 2.0.</p>
<p><strong>6. Custom event arguments</strong><br />
Custom event arguments (own class derrived from EventArgs) should have only readonly members. This prevents the client from changing the values. Readonly members can only be assigned in the constructor so the publisher of the event is able to set the values when creating the arguments.</p>
<p><strong>7. Always fire events in a try catch block</strong><br />
You never know what the event handler method of the client does, so its good to put the firing into a try catch block. You could write your own EventsHelper class [COMP05, 144] for the purpose of firing event within your classes.</p>
<div class="igBar"><span id="lcsharp-20"><a href="#" onclick="javascript:showCodeTxt('csharp-20'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-20">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> EventsHelper <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Fire<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">Delegate</span> d, <span style="color: #0600FF;">params</span> <span style="color: #FF0000;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>d == <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span> <span style="color: #0600FF;">return</span>;</div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #FF0000;">Delegate</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> dels = d.<span style="color: #0000FF;">GetInvocationList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">Delegate</span> sind <span style="color: #0600FF;">in</span> dels<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">try</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sink.<span style="color: #0000FF;">DynamicInvoke</span><span style="color: #000000;">&#40;</span>args<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong>8. use "property-like" approach for event accessors</strong><br />
As you do with common member variables (expressing them with properties) you also should do this with events. This alows you to control the adding and removing of event handlers for your event.</p>
<div class="igBar"><span id="lcsharp-21"><a href="#" onclick="javascript:showCodeTxt('csharp-21'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-21">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MyClass <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;MyHandler _myEvent;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">event</span> MyHandler MyEvent <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; add <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_myEvent += value;</div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; remove <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_myEvent -= value;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong>9. Static objects as keys</strong></p>
<div class="igBar"><span id="lcsharp-22"><a href="#" onclick="javascript:showCodeTxt('csharp-22'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-22">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">static</span> name = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #FF0000;">Object</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">myCollection<span style="color: #000000;">&#91;</span>name<span style="color: #000000;">&#93;</span> = <span style="color: #808080;">"a name"</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">//instead of (old school)</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">myCollection<span style="color: #000000;">&#91;</span><span style="color: #808080;">"name"</span><span style="color: #000000;">&#93;</span> = <span style="color: #808080;">"a name"</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
a static object is created to act as a key for e.g. collections (because every instance is unique - has its own identity!). The advantage in comparison to the "old school" version (key is a string) is that the key is already available in memory wheras the string needs to be created everytime. If you use this key in a lot of places it could save some time.</p>
<p><strong>10. Combine enumeration values</strong></p>
<div class="igBar"><span id="lcsharp-23"><a href="#" onclick="javascript:showCodeTxt('csharp-23'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-23">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">enum</span> color <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;red,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;green,</div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;all = red | green</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
This example illustrates that it is possible to have enumeration values which accept more than one existing values of the current enumeration.</p>
<p><strong>11. Singelton Application</strong><br />
A singelton application is one which only allows running one instance per machine. e.g. Microsoft Outlook is a singelton application. Everytime the user starts the application it will check if an instance is already running. If its not running it will launch it otherwise it will focus to the current one. The source for an application which runs only once is available in another post "<a href="http://www.webdevbros.net/2007/11/14/singelton-application-with-c/">Singelton Application with C#</a>"</p>
<p><strong>12. Containment</strong><br />
If it is not possible to derive from a class we use a class to wrap around the class we would like to inherit from. This procedure is also known as "Containment" (I mention it here because the this term is more unknown - usually developers use wrapper)</p>
<p><strong>13. Use Debug.Assert()</strong><br />
The use of Debug.Assert should be used as often as possible. During the development the developer should "assert" things which always should be true. This helps finding bugs easier and its easier for other developers to understand the code.</p>
<div class="igBar"><span id="lcsharp-24"><a href="#" onclick="javascript:showCodeTxt('csharp-24'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-24">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF0000;">int</span> a = <span style="color: #FF0000;color:#800000;">10</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF0000;">int</span> b = <span style="color: #FF0000;color:#800000;">20</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF0000;">int</span> c = <span style="color: #FF0000;color:#800000;">20</span> - <span style="color: #FF0000;color:#800000;">10</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Debug.<span style="color: #0000FF;">Assert</span><span style="color: #000000;">&#40;</span>c&gt; <span style="color: #FF0000;color:#800000;">0</span>, <span style="color: #808080;">"i need it bigger than 0. check might be done in the future"</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;color:#800000;">10</span> / c<span style="color: #000000;">&#41;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>This short example demonstrates that obviously the developer expects the variable "c" to be greater than 0. Why? Because in the next line he/she uses it for a dvision which would break with c = 0. If this is the case you would get an information seen in line 4. This concept is nice because it makes comments "active".</p>
<p><strong>14. user string instead of String</strong><br />
String is just an alias for string and therefore its nicer to use the original type straight away. The same with int and Int32, etc.</p>
<p><strong>15. Rethrowing exceptions</strong></p>
<div class="igBar"><span id="lcsharp-25"><a href="#" onclick="javascript:showCodeTxt('csharp-25'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-25">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">try</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> <span style="color: #0600FF;">catch</span><span style="color: #000000;">&#40;</span>Exception e<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">//some logging,etc...</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">throw</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
This piece of code rethrows the exception. The interesting thing here is that you dont need to specify the Exception explicitly. Just the keyword "throw" is enough to rethrow the exception. </p>
<p><strong>15. Code exclusion with attributes and not with #if .. #endif</strong></p>
<div class="igBar"><span id="lcsharp-26"><a href="#" onclick="javascript:showCodeTxt('csharp-26'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-26">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#91;</span>Conditional<span style="color: #000000;">&#40;</span><span style="color: #808080;">"DEBUG"</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">void</span> Foo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p>
The advantage here is that the code is not reachable anymore if the condition is not met. This means that it will result in compile errors if you reference this method. #if ... #endif does not enforce this constraint. Its also nicer than the hash signs ;) Please check the <a href="http://msdn2.microsoft.com/en-us/library/aa664622(VS.71).aspx">MSDN Reference</a> for more details about the conditional attribute.</p>
<p>Thats was it :) Hope there was something interesting and maybe new for everyone. I dont want to promote the book here but I really recommend it because it contains good content about programming with c# (and especially for component development) and should be read by every C# developer who wants to develop for enterprise.</p>
<p>Maybe you have some interesting tips &#038; tricks which might be useful for others. Please be so nice and comment them... I am interested.</p>
<p>Resources:<br />
[COMP05] .net Components, Juval Löwy, O'Reilly 2005 United States</p>
<div><table> <td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://www.webdevbros.net/2007/11/14/c-tips-tricks-and-things-you-should-know/&amp;t=C%23+tips%2C+tricks+and+things+you+should+know&amp;s=normal' height='80' width='52' frameborder='0' scrolling='no'></iframe></td> <td><iframe src='http://www.reddit.com/button_content?newwindow=1&amp;url=http://www.webdevbros.net/2007/11/14/c-tips-tricks-and-things-you-should-know/&amp;title=C%23+tips%2C+tricks+and+things+you+should+know&amp;t=2 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http://www.webdevbros.net/2007/11/14/c-tips-tricks-and-things-you-should-know/&amp;title=C%23+tips%2C+tricks+and+things+you+should+know&amp;t=1 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><script type="text/javascript"><!--yahooBuzzArticleHeadline=C%23+tips%2C+tricks+and+things+you+should+know;//--></script><script type="text/javascript" src="http://d.yimg.com/ds/badge2.js" badgetype=square></script></td> <td><script type="text/javascript">tweetmeme_url='http://www.webdevbros.net/2007/11/14/c-tips-tricks-and-things-you-should-know/'; tweetmeme_style = 'normal';; </script><script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js" ></script></td></table></div><!-- This is a HTML comment, it will not display in any page. Feel free to remove this comment if it cause any inconvenient to you.
	Thanks for using digg digg, please visit http://www.mkyong.com/blog/digg-digg-wordpress-plugin for any comments and ideas, 
	
    Author : Yong Mook Kim
    Website : http://www.mkyong.com
	-->]]></content:encoded>
			<wfw:commentRss>http://www.webdevbros.net/2007/11/14/c-tips-tricks-and-things-you-should-know/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>8 Reasons to Stick with ASP 3.0 in 2006 (and 2007)</title>
		<link>http://www.webdevbros.net/2007/03/30/8-reasons-to-stick-with-asp-30-in-2006-and-2007/</link>
		<comments>http://www.webdevbros.net/2007/03/30/8-reasons-to-stick-with-asp-30-in-2006-and-2007/#comments</comments>
		<pubDate>Fri, 30 Mar 2007 08:56:55 +0000</pubDate>
		<dc:creator>Michal</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[classic ASP (VBScript)]]></category>

		<guid isPermaLink="false">http://fabiankoehler.de/wdb/?p=27</guid>
		<description><![CDATA[Today I was browsing through the microsoft.­public.­inetserver.­asp.­general newsgroup and found a nice article linked within a topic where people are talking about classic ASP and its brother .net. The article outlines 8 reasons to stick with the good old classic ASP ... Unfortunately I cannot really figure out when the article was written but i [...]]]></description>
			<content:encoded><![CDATA[<p>Today I was browsing through the microsoft.­public.­inetserver.­asp.­general newsgroup and found a nice article linked within a topic where people are talking about classic ASP and its brother .net. <a target="_blank" href="http://www.packtpub.com/article/Classic-ASP">The article </a>outlines 8 reasons to stick with the good old classic ASP ... Unfortunately I cannot really figure out when the article was written but i think it must be quite recent because they bring up VS2005 and web 2.0.</p>
<p>As I have done already some projects with .net I love this statement (refers to web-projects):</p>
<blockquote><p><span lang="EN-GB">Sluggish is a word to describe the whole of the .NET development environment. Starting a new project? Go make a cup of coffee. Switching between tabs? 'Go large' with your coffee. Pressing F1 for help? Make your coffee a take-out and go and have a stroll in the park.</span></p></blockquote>
<p><span lang="EN-GB"><a target="_blank" href="http://www.packtpub.com/article/Classic-ASP">http://www.packtpub.com/article/Classic-ASP</a></span></p>
<div><table> <td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://www.webdevbros.net/2007/03/30/8-reasons-to-stick-with-asp-30-in-2006-and-2007/&amp;t=8+Reasons+to+Stick+with+ASP+3.0+in+2006+%28and+2007%29+&amp;s=normal' height='80' width='52' frameborder='0' scrolling='no'></iframe></td> <td><iframe src='http://www.reddit.com/button_content?newwindow=1&amp;url=http://www.webdevbros.net/2007/03/30/8-reasons-to-stick-with-asp-30-in-2006-and-2007/&amp;title=8+Reasons+to+Stick+with+ASP+3.0+in+2006+%28and+2007%29+&amp;t=2 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http://www.webdevbros.net/2007/03/30/8-reasons-to-stick-with-asp-30-in-2006-and-2007/&amp;title=8+Reasons+to+Stick+with+ASP+3.0+in+2006+%28and+2007%29+&amp;t=1 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><script type="text/javascript"><!--yahooBuzzArticleHeadline=8+Reasons+to+Stick+with+ASP+3.0+in+2006+%28and+2007%29+;//--></script><script type="text/javascript" src="http://d.yimg.com/ds/badge2.js" badgetype=square></script></td> <td><script type="text/javascript">tweetmeme_url='http://www.webdevbros.net/2007/03/30/8-reasons-to-stick-with-asp-30-in-2006-and-2007/'; tweetmeme_style = 'normal';; </script><script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js" ></script></td></table></div><!-- This is a HTML comment, it will not display in any page. Feel free to remove this comment if it cause any inconvenient to you.
	Thanks for using digg digg, please visit http://www.mkyong.com/blog/digg-digg-wordpress-plugin for any comments and ideas, 
	
    Author : Yong Mook Kim
    Website : http://www.mkyong.com
	-->]]></content:encoded>
			<wfw:commentRss>http://www.webdevbros.net/2007/03/30/8-reasons-to-stick-with-asp-30-in-2006-and-2007/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

