<?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; c#</title>
	<atom:link href="http://www.webdevbros.net/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.webdevbros.net</link>
	<description>hot talk about web development</description>
	<lastBuildDate>Wed, 16 Dec 2009 12:59:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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>0</slash:comments>
		</item>
		<item>
		<title>FluentNhibernate and Stored Procedures</title>
		<link>http://www.webdevbros.net/2009/11/25/fluentnhibernate-and-stored-procedures/</link>
		<comments>http://www.webdevbros.net/2009/11/25/fluentnhibernate-and-stored-procedures/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 10:59:27 +0000</pubDate>
		<dc:creator>Dai Bok</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[general stuff]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[Fluent NHibernate]]></category>

		<guid isPermaLink="false">http://www.webdevbros.net/?p=808</guid>
		<description><![CDATA[I am evaluating FluentNHibernate  (FNH), to see if it is suitable for a project I am working on. Disappointingly, FNH does not support Store procedures of the box. Of course, FNH is under the BSD licence, so I am sure those who are confident enough can implement this for the rest of us! This [...]]]></description>
			<content:encoded><![CDATA[<p>I am evaluating <a href="http://fluentnhibernate.org/">FluentNHibernate </a> (FNH), to see if it is suitable for a project I am working on. Disappointingly, FNH does not support Store procedures of the box. Of course, FNH is under the BSD licence, so I am sure those who are confident enough can implement this for the rest of us! This post will show how I got FNH to work with stored procedures, and can hopefully be followed as a working example.</p>
<p>FNH extends NHibernate, and automagically generates XML mapping files for your objects. Unfortunately, to get stored procedures to work, you need to take a step backwards, and create good old fashioned hbm.xml files, doing the mappings manually.</p>
<p>Firstly , let us look at the results  of the stored procedure that we want to map.</p>
<table border="0" align="center">
<tbody>
<tr>
<td>ID</td>
<td>enDescription</td>
<td>cyDescription</td>
<td>IsActive</td>
</tr>
<tr>
<td>1</td>
<td>Swansea</td>
<td>Abertawe</td>
<td>True</td>
</tr>
<tr>
<td>2</td>
<td>Cardiff</td>
<td>Caerdydd</td>
<td>True</td>
</tr>
<tr>
<td>3</td>
<td>Newport</td>
<td>Cas Newydd</td>
<td>False</td>
</tr>
</tbody>
</table>
<p>The class that will use this data is called lookup.</p>
<p>The code for this class 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;<span style="color: blue">namespace</span> Entities {</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">Lookup</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">virtual</span> <span style="color: blue">int</span> Id { <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">virtual</span> <span style="color: blue">string</span> EnDescription { <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">virtual</span> <span style="color: blue">string</span> CyDescription { <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">virtual</span> <span style="color: blue">bool</span> IsActive { <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>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;8</span>&nbsp;}</p>
</div>
<p>&nbsp; <br />
This object will be used to populate a simple drop down list, so that a user can select their county.</p>
<p>When I started using FluentHNibernate, I wanted to totally avoid using XML mappings, so I skipped<a href="http://www.amazon.co.uk/gp/product/193239415X?ie=UTF8&amp;tag=daisramblin-21&amp;linkCode=as2&amp;camp=1634&amp;creative=19450&amp;creativeASIN=193239415X"> chapters 3 and 6 of Hibernate in Action.</a><img class=" luqmsfcfqseksqyogrfs luqmsfcfqseksqyogrfs luqmsfcfqseksqyogrfs luqmsfcfqseksqyogrfs luqmsfcfqseksqyogrfs luqmsfcfqseksqyogrfs luqmsfcfqseksqyogrfs luqmsfcfqseksqyogrfs luqmsfcfqseksqyogrfs luqmsfcfqseksqyogrfs luqmsfcfqseksqyogrfs luqmsfcfqseksqyogrfs pcshjncfdcdawbvrvmch pcshjncfdcdawbvrvmch" style="border:none !important;margin:0px !important" src="http://www.assoc-amazon.co.uk/e/ir?t=daisramblin-21&amp;l=as2&amp;o=2&amp;a=193239415X" border="0" alt="" width="1" height="1" /> My first mistake! So for those attempting this, it may be worth your while understanding Hibernate mappings before you proceed. (You may also ask why I have the Java Book and my code is in C#, that is because I am quite used to working in different programming languages, so those who prefer examples in .Net examples check <a href="http://www.amazon.co.uk/gp/product/1932394923?ie=UTF8&amp;tag=daisramblin-21&amp;linkCode=as2&amp;camp=1634&amp;creative=19450&amp;creativeASIN=1932394923">NHibernate in Action</a><img class=" luqmsfcfqseksqyogrfs" style="border:none !important;margin:0px !important" src="http://www.assoc-amazon.co.uk/e/ir?t=daisramblin-21&amp;l=as2&amp;o=2&amp;a=1932394923" border="0" alt="" width="1" height="1" />.)</p>
<p>Let’s move on to creating the mapping file.</p>
<p><em>IMPORTANT:</em> When you add the mapping file to your project, make sure you set the Build Action to Embedded Resource!</p>
<p>I have created a Lookup.hbm.xml file, and the source is below:</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;<span style="color: blue">&lt;?</span><span style="color: #a31515">xml</span><span style="color: blue"> </span><span style="color: red">version</span><span style="color: blue">=</span>&quot;<span style="color: blue">1.0</span>&quot;<span style="color: blue"> </span><span style="color: red">encoding</span><span style="color: blue">=</span>&quot;<span style="color: blue">utf-8</span>&quot;<span style="color: blue"> ?&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;<span style="color: blue">&lt;</span><span style="color: #a31515">hibernate-mapping</span><span style="color: blue"> </span><span style="color: red">xmlns</span><span style="color: blue">=</span>&quot;<span style="color: blue">urn:nhibernate-mapping-2.2</span>&quot;</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; </span><span style="color: red">namespace</span><span style="color: blue">=</span>&quot;<span style="color: blue">Entities</span>&quot;<span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;4</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">class</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">Lookup</span>&quot;<span style="color: blue"> </span><span style="color: red">table</span><span style="color: blue">=</span>&quot;<span style="color: blue">dbo.sp_GetLookups</span>&quot;<span style="color: blue"> &gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;5</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">id</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">Id</span>&quot;<span style="color: blue"> </span><span style="color: red">column</span><span style="color: blue">=</span>&quot;<span style="color: blue">Id</span>&quot;<span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;6</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">generator</span><span style="color: blue"> </span><span style="color: red">class</span><span style="color: blue">=</span>&quot;<span style="color: blue">native</span>&quot;<span style="color: blue"> /&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;7</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/</span><span style="color: #a31515">id</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;8</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">property</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">EnDescription</span>&quot;<span style="color: blue"> </span><span style="color: red">column</span><span style="color: blue">=</span>&quot;<span style="color: blue">enDescription</span>&quot;<span style="color: blue"> /&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;9</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">property</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">CyDescription</span>&quot;<span style="color: blue"> </span><span style="color: red">column</span><span style="color: blue">=</span>&quot;<span style="color: blue">cyDescription</span>&quot;<span style="color: blue"> /&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;10</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">property</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">IsActive</span>&quot;<span style="color: blue"> </span><span style="color: red">column</span><span style="color: blue">=</span>&quot;<span style="color: blue">IsActive</span>&quot;<span style="color: blue"> /&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;11</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">loader</span><span style="color: blue"> </span><span style="color: red">query-ref</span><span style="color: blue">=</span>&quot;<span style="color: blue">dbo.sp_GetLookups</span>&quot;<span style="color: blue">/&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;12</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &lt;/</span><span style="color: #a31515">class</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;13</span>&nbsp;</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;14</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">sql-query</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">dbo.sp_GetLookups</span>&quot;<span style="color: blue"> &gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;15</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">return</span><span style="color: blue"> </span><span style="color: red">alias</span><span style="color: blue">=</span>&quot;<span style="color: blue">dbo.sp_GetLookups</span>&quot;<span style="color: blue"> </span><span style="color: red">class</span><span style="color: blue">=</span>&quot;<span style="color: blue">Lookup</span>&quot;<span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;16</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">return-property</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">Id</span>&quot;<span style="color: blue"> </span><span style="color: red">column</span><span style="color: blue">=</span>&quot;<span style="color: blue">Id</span>&quot;<span style="color: blue">/&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;17</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">return-property</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">EnDescription</span>&quot;<span style="color: blue"> </span><span style="color: red">column</span><span style="color: blue">=</span>&quot;<span style="color: blue">enDescription</span>&quot;<span style="color: blue">/&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;18</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">return-property</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">CyDescription</span>&quot;<span style="color: blue"> </span><span style="color: red">column</span><span style="color: blue">=</span>&quot;<span style="color: blue">cyDescription</span>&quot;<span style="color: blue">/&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;19</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;</span><span style="color: #a31515">return-property</span><span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&quot;<span style="color: blue">IsActive</span>&quot;<span style="color: blue"> </span><span style="color: red">column</span><span style="color: blue">=</span>&quot;<span style="color: blue">IsActive</span>&quot;<span style="color: blue">/&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;20</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &lt;/</span><span style="color: #a31515">return</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;21</span>&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; exec dbo.sp_GetLookups</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;22</span>&nbsp;<span style="color: blue">&nbsp;&nbsp;&nbsp; &lt;/</span><span style="color: #a31515">sql-query</span><span style="color: blue">&gt;</span></p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;23</span>&nbsp;<span style="color: blue">&lt;/</span><span style="color: #a31515">hibernate-mapping</span><span style="color: blue">&gt; </span></p>
</div>
<p>&nbsp; </p>
<p>To put it quite simply, lines 5 to 13 map my Lookup class to the columns in the stored procedure, while lines 16 to 20 map the results from the stored procedure my lookup class. Line 22 names the stored procedure. I am not sure if this is the best way to achieve the mappings, so any feedback would be appreciated.</p>
<p>Once your object is nicely  mapped, you then need to update your fluent configuration. All you need to do is tell FNH to load hbmMappings from the current assembly. See the snippet below:</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;1</span>&nbsp;&nbsp;&nbsp; .Mappings(m =&gt; {</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;2</span>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; m.HbmMappings.AddFromAssembly(<span style="color: #2b91af">Assembly</span>.GetExecutingAssembly());</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;3</span>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;  m.FluentMappings.AddFromAssembly(<span style="color: #2b91af">Assembly</span>.GetExecutingAssembly());</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;4</span>&nbsp;&nbsp;&nbsp;&nbsp; })</p>
</div>
<p> &nbsp; </p>
<p>To retrieve the list of lookups, I do the following, which populates my results variable with a list of all my lookups.</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; <span style="color: blue">var</span> sessionfactory = CreateSessionFactory();</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;2</span>&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> session = sessionfactory.OpenSession();</p>
<p style="margin: 0px"><span style="color: #2b91af">&nbsp;&nbsp;&nbsp;&nbsp;3</span>&nbsp;&nbsp;&nbsp; <span style="color: blue">var</span> results = session.GetNamedQuery(<span style="color: #a31515">&quot;dbo.sp_GetLookups&quot;</span>).List();</p>
</div>
<p> &nbsp; </p>
<p>And that is it, the results variable now contains the list of lookups that I can use to populate my list control.</p>
<div><table> <td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://www.webdevbros.net/2009/11/25/fluentnhibernate-and-stored-procedures/&amp;t=FluentNhibernate+and+Stored+Procedures&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/11/25/fluentnhibernate-and-stored-procedures/&amp;title=FluentNhibernate+and+Stored+Procedures&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/11/25/fluentnhibernate-and-stored-procedures/&amp;title=FluentNhibernate+and+Stored+Procedures&amp;t=1 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><script type="text/javascript"><!--yahooBuzzArticleHeadline=FluentNhibernate+and+Stored+Procedures;//--></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/11/25/fluentnhibernate-and-stored-procedures/'; 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/11/25/fluentnhibernate-and-stored-procedures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Top three C# questions on stackoverflow you should read</title>
		<link>http://www.webdevbros.net/2009/07/07/top-three-c-questions-on-stackoverflow-you-should-read/</link>
		<comments>http://www.webdevbros.net/2009/07/07/top-three-c-questions-on-stackoverflow-you-should-read/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 15:57:04 +0000</pubDate>
		<dc:creator>Michal</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[favourites]]></category>
		<category><![CDATA[hidden features]]></category>
		<category><![CDATA[rank]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[toplist]]></category>
		<category><![CDATA[tricks]]></category>
		<category><![CDATA[visual studio]]></category>
		<category><![CDATA[vs2003]]></category>
		<category><![CDATA[vs2005]]></category>
		<category><![CDATA[vs2008]]></category>

		<guid isPermaLink="false">http://www.webdevbros.net/?p=365</guid>
		<description><![CDATA[Here is a list of questions which I (!!) consider as TOP 3 C# (Visual Studio) questions (although the answers are probably the most interesting) on stackoverflow. Yes, I do love STO :)


Hidden Features of C#
Visual Studio Optimizations
Which C#/.Net blogs do you read?

If you need more see the most voted C# questions. Would be nice [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a list of questions which I (!!) consider as TOP 3 C# (Visual Studio) questions (although the answers are probably the most interesting) on <a href="http://www.stackoverflow.com">stackoverflow</a>. Yes, I do love STO :)</p>
<p><img src="http://www.webdevbros.net/wp-content/uploads/2009/07/stackoverflowtop5csharp1.png" alt="stackoverflowtop5csharp" title="stackoverflowtop5csharp" width="150" height="37" class="alignright size-full wp-image-370" /></p>
<ol>
<li><a href="http://stackoverflow.com/questions/9033/hidden-features-of-c">Hidden Features of C#</a></li>
<li><a href="http://stackoverflow.com/questions/8440/visual-studio-optimizations">Visual Studio Optimizations</a></li>
<li><a href="http://stackoverflow.com/questions/551315/which-c-net-blogs-do-you-read">Which C#/.Net blogs do you read?</a></li>
</ol>
<p>If you need more see the <a href="http://stackoverflow.com/questions/tagged/c%23">most voted C# questions</a>. Would be nice to see some more lists like that (Julien, what about Flex?, Fab PHP?).</p>
<div><table> <td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://www.webdevbros.net/2009/07/07/top-three-c-questions-on-stackoverflow-you-should-read/&amp;t=Top+three+C%23+questions+on+stackoverflow+you+should+read&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/07/07/top-three-c-questions-on-stackoverflow-you-should-read/&amp;title=Top+three+C%23+questions+on+stackoverflow+you+should+read&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/07/07/top-three-c-questions-on-stackoverflow-you-should-read/&amp;title=Top+three+C%23+questions+on+stackoverflow+you+should+read&amp;t=1 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><script type="text/javascript"><!--yahooBuzzArticleHeadline=Top+three+C%23+questions+on+stackoverflow+you+should+read;//--></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/07/07/top-three-c-questions-on-stackoverflow-you-should-read/'; 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/07/07/top-three-c-questions-on-stackoverflow-you-should-read/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Nhibernate and the Null object pattern</title>
		<link>http://www.webdevbros.net/2009/07/06/nhibernate-and-the-null-object-pattern/</link>
		<comments>http://www.webdevbros.net/2009/07/06/nhibernate-and-the-null-object-pattern/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 04:41:29 +0000</pubDate>
		<dc:creator>Michal</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[default value]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[if null]]></category>
		<category><![CDATA[null]]></category>
		<category><![CDATA[null reference]]></category>
		<category><![CDATA[pattern]]></category>

		<guid isPermaLink="false">http://www.webdevbros.net/?p=349</guid>
		<description><![CDATA[Is this dialog (exception) familiar to you?

Okay, maybe you haven&#8217;t seen this screen in particular but apart from the troubleshooting tips you should have seen it.

Few days ago I have discovered the Null object pattern (NOP) and its usefulness. Kick me, punch me or even stone me to death cause I haven&#8217;t known it so [...]]]></description>
			<content:encoded><![CDATA[<p>Is this dialog (exception) familiar to you?</p>
<p><img src="http://www.webdevbros.net/wp-content/uploads/2009/07/null-object-pattern.png" alt="null-object-pattern" title="null-object-pattern" width="402" height="233" style="border:0px" class="size-full wp-image-350" /></p>
<p>Okay, maybe you haven&#8217;t seen this screen in particular but apart from the troubleshooting tips you should have seen it.<br />
<span id="more-349"></span><br />
Few days ago I have discovered the <a href="http://en.wikipedia.org/wiki/Null_Object_pattern">Null object pattern (NOP)</a> and its usefulness. Kick me, punch me or even stone me to death cause I haven&#8217;t known it so far :) Still I would like to write a quick note about it as not everyone might be aware of it.</p>
<blockquote><p>
If it&#8217;s possible for a variable to be null, you have to remember to surround it with null test code so you&#8217;ll do the right thing if a null is present. Often the right thing is same in many contexts, so you end up writing similar code in lots of places &#8211; committing the sin of code duplication. &#8211; <a href="http://martinfowler.com/eaaCatalog/specialCase.html">Martin Fowler</a>
</p></blockquote>
<p>What does that exactly mean? It means the following:</p>
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<pre style="margin: 0px;"><span style="color: blue;">if</span> (Product.Creator == <span style="color: blue;">null</span>) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; Debug.WriteLine(<span style="color: maroon;">"unknown"</span>);</pre>
<pre style="margin: 0px;">} <span style="color: blue;">else</span> {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; Debug.WriteLine(Product.Creator);</pre>
<pre style="margin: 0px;">}</pre>
</div>
<p>If we have a <code>Creator</code> property of a <code>Product</code> and it may be null (to indicate an unknown creator) we need to do a null check everywhere we wanna display the creator. I would call it boring code duplication. And that&#8217;s where the NOP joins our game.</p>
<p>Let&#8217;s create a user instance which represents the actual &#8220;unknown&#8221; user so we can get rid of the odd null check. Best to achieve that is to sub class our <code>User</code> into an <code>Unknown</code> user. The following diagram illustrates this simple approach. btw see Customers equal to users :))</p>
<p><img src="http://www.webdevbros.net/wp-content/uploads/2009/07/nullobjectpatternclassdiagramm.gif" alt="nullobjectpatternclassdiagramm" title="nullobjectpatternclassdiagramm" width="244" height="189" class="aligncenter size-full wp-image-361" /><br />
<a href="http://martinfowler.com/eaaCatalog/specialCase.html">Figure 1 &#8211; Patterns of Enterprise Application Architecture, Martin Fowler</a></p>
<p>As you can see we are even able to provide different types of &#8220;null&#8221; users such as &#8220;Missing&#8221; and &#8220;Unknown&#8221;. That allows us to assign a human readable name to those users (e.g. assign in the sub class&#8217; constructor) and we are still able to check if a given user is unknown (if necessary):</p>
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<pre style="margin: 0px;"><span style="color: blue;">if</span> (Product.Creator <span style="color: blue;">is</span> UnknownUser)</pre>
</div>
<p>Okay, enough general theory about the pattern itself. You might ask how does the whole thing fit into NHibernate? Persistence is the magic word. If our users are persisted then the &#8220;unknown&#8221; users gets persisted as well. That&#8217;s probably not what we want. We wanna keep our data storage clean containing only &#8220;real&#8221; users. Michael Cromwell points that out in one of his articles:</p>
<blockquote><p>This then has its own problems because if it’s a user editable object you don’t really want them to be able to change/delete this data so it becomes a special case that will need to be locked for editing/deleting. &#8211; <a href="http://journalofasoftwaredev.wordpress.com/2009/01/17/nhibernate-null-object-pattern-the-options/">NHibernate &#038; Null Object Pattern: The Options, Michael Cromwell</a></p></blockquote>
<p>Correct! We don&#8217;t want to exclude this user in all our queries and take care of these &#8220;special case users&#8221;. What he suggests is to handle that case directly in the getter and setter of the property. In case a null is assigned he returns an &#8220;NotAssigned User&#8221;. In our particular example the solution would look the following:</p>
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<pre style="margin: 0px;"><span style="color: blue;">protected</span> <span style="color: teal;">User</span> _creator;</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: teal;">User</span> Creator {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> _creator ?? <span style="color: blue;">new</span> UnknownUser();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">set</span> {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (<span style="color: blue;">value</span> <span style="color: blue;">is</span> UnknownUser)</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _creator = <span style="color: blue;">null</span>;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">else</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _creator = <span style="color: blue;">value</span>;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">}</pre>
</div>
<p>I am happy! This looks like a nice OO solution to me. The &#8220;unknown user&#8221; won&#8217;t be persisted, a null reference will be stored instead. <strong>Important:</strong> This works fine for one &#8220;unknown user type&#8221;. However, keep in mind that if we have more than one &#8220;unknown user type&#8221; (see Figure 1) than it might be necessary to persist those users in order to be able to differentiate between them.</p>
<p>Haven&#8217;t used it so far but will in my next projects. Anyone who is interested into more practical details might read <a href="http://stackoverflow.com/questions/271526/how-to-avoid-null-statements-in-java">How to avoid “!= null” statements in Java?</a>.</p>
<div><table> <td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://www.webdevbros.net/2009/07/06/nhibernate-and-the-null-object-pattern/&amp;t=Nhibernate+and+the+Null+object+pattern&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/07/06/nhibernate-and-the-null-object-pattern/&amp;title=Nhibernate+and+the+Null+object+pattern&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/07/06/nhibernate-and-the-null-object-pattern/&amp;title=Nhibernate+and+the+Null+object+pattern&amp;t=1 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><script type="text/javascript"><!--yahooBuzzArticleHeadline=Nhibernate+and+the+Null+object+pattern;//--></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/07/06/nhibernate-and-the-null-object-pattern/'; 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/07/06/nhibernate-and-the-null-object-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Create a multi languaged domain model with NHibernate and C#</title>
		<link>http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/</link>
		<comments>http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/#comments</comments>
		<pubDate>Wed, 24 Jun 2009 16:40:47 +0000</pubDate>
		<dc:creator>Michal</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[culture]]></category>
		<category><![CDATA[DAO]]></category>
		<category><![CDATA[globalization]]></category>
		<category><![CDATA[i18n]]></category>
		<category><![CDATA[internationalization]]></category>
		<category><![CDATA[localisation]]></category>
		<category><![CDATA[localization]]></category>
		<category><![CDATA[mapping]]></category>

		<guid isPermaLink="false">http://www.webdevbros.net/?p=290</guid>
		<description><![CDATA[Recently I&#8217;ve been developing an enterprise asp.net application with NHibernate which was required to fully support multi language (localization) on UI side as well as on data side. Whereas the former is easily implemented with .net resources the latter is not that straightforward as it seems. That articles talks about it and the solution I [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I&#8217;ve been developing an enterprise asp.net application with NHibernate which was required to fully support multi language (localization) on UI side as well as on data side. Whereas the former is easily implemented with .net resources the latter is not that straightforward as it seems. That articles talks about it and the solution I have chosen as I have never done it with NHibernate before. <span id="more-290"></span></p>
<p>Let&#8217;s start with a brief description of the problem before digging through the possible solutions. The applications architecture is based on the <a href="http://www.codeproject.com/KB/architecture/NHibernateBestPractices.aspx">NHibernate Best Practices with ASP.NET, 1.2nd Ed.</a> by Billy McCafferty. I guess it&#8217;s a must read for all who deal with Nhibernate and asp.net. So please read it before moving on, as it helps you to understand the approach I am talking about.</p>
<p><strong>Problem:</strong></p>
<ol>
<li>Many domain objects have different properties which are in need of translations. E.g. A <code>Product</code> may contain a <code>Name</code> and a  <code>Category</code> property which needs to be translated into the applications supported languages.</li>
<li>It should be easily possible to update the translations of an object before persisting it.</li>
<li>After getting a persistent entity it should be possible to get a given property in the applications current language.</li>
<li>Querying entities according to the translation must not be a big problem. E.g. Getting a list of products ordered by its name in the current selected language.</li>
</ol>
<p><strong>1st Solution:</strong></p>
<p>The first solution I tried was the approach from Ayende (<a href="http://ayende.com/Blog/archive/2006/12/26/LocalizingNHibernateContextualParameters.aspx">Localizing NHibernate: Contextual Parameters</a>) &#8211; whose Blog I strongly recommend for all NHibernaters. The approach he describes is having a simple string property on your domain object and mapping it using a filter with the current selected language (culture).</p>
<p>Pros:<br />
- quickly to implement<br />
- Domain objects property stays unchanged (still a string)</p>
<p>Cons:<br />
- Each property would result in an own table<br />
- &#8220;ugly&#8221; sql queries in your mapping files<br />
- Can&#8217;t easily access all translations<br />
- Querying entities is not straightforward (e.g. fulltext search)</p>
<p><strong>2nd Solution (my preferred):</strong></p>
<p>Due to the cons I could not really use the 1st approach within the project and decided to build an own solution. Here is my suggestions&#8230;<br />
We create a Domain object called <code>PhraseDictionary</code> acts as a generic container for translation phrases. It can be bound to any property of any of our domain objects. I have added its class diagram to show its members:</p>
<p><img src="http://www.webdevbros.net/wp-content/uploads/2009/06/PhraseDictionary.png" alt="PhraseDictionary" title="PhraseDictionary" width="315" height="355" class="aligncenter size-full wp-image-298" /></p>
<p>That type can be used now for each of our domain properties which require translation. The <code>Name</code> property helps us to differentiate all the different phrases we will have in the DB (you can skip it if you want &#8211; though it&#8217;s nice if you browse the data directly). The <code>SetPhrase()</code> method sets a phrase for a given LCID and <code>getPhrase()</code> gets a phrase translation for the requested LCID. Both methods return the instance itself to make <a href="http://martinfowler.com/dslwip/MethodChaining.html">method chaining</a> possible.<br />
For easier usage there is a <code>Phrase</code> property which does the same but always for the current domain culture (Domain culture could be handled in a <code>DomainCulture</code> class &#8211; which gives us the ability to get the current culture and/or switch it). All Phrases are stored in a Dictionary with LCID as key and the actual translation as the value.</p>
<p>As we are going to persist it, thats the resulting mapping file:</p>
<div class="code">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<pre style="margin: 0px;"><span style="color: blue;">&lt;?</span><span style="color: maroon;">xml</span><span style="color: blue;"> </span><span style="color: red;">version</span><span style="color: blue;">=</span>"<span style="color: blue;">1.0</span>"<span style="color: blue;"> </span><span style="color: red;">encoding</span><span style="color: blue;">=</span>"<span style="color: blue;">utf-8</span>"<span style="color: blue;"> ?&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: maroon;">hibernate-mapping</span><span style="color: blue;"> </span><span style="color: red;">xmlns</span><span style="color: blue;">=</span>"<span style="color: blue;">urn:nhibernate-mapping-2.2</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &lt;</span><span style="color: maroon;">class</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>"<span style="color: blue;">MyProject.Core.Domain.PhraseDictionary, MyProject.Core</span>"<span style="color: blue;"> </span><span style="color: red;">table</span><span style="color: blue;">=</span>"<span style="color: blue;">dictionary</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: maroon;">id</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>"<span style="color: blue;">ID</span>"<span style="color: blue;"> </span><span style="color: red;">unsaved-value</span><span style="color: blue;">=</span>"<span style="color: blue;">0</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;</span><span style="color: maroon;">generator</span><span style="color: blue;"> </span><span style="color: red;">class</span><span style="color: blue;">=</span>"<span style="color: blue;">identity</span>"<span style="color: blue;"> /&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;/</span><span style="color: maroon;">id</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: maroon;">property</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>"<span style="color: blue;">Name</span>"<span style="color: blue;">&gt;&lt;/</span><span style="color: maroon;">property</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;</span><span style="color: maroon;">map</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>"<span style="color: blue;">Phrases</span>"<span style="color: blue;"> </span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; </span><span style="color: red;">access</span><span style="color: blue;">=</span>"<span style="color: blue;">nosetter.camelcase-underscore</span>"<span style="color: blue;"> </span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; </span><span style="color: red;">table</span><span style="color: blue;">=</span>"<span style="color: blue;">phrase</span>"<span style="color: blue;"> </span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; </span><span style="color: red;">cascade</span><span style="color: blue;">=</span>"<span style="color: blue;">all-delete-orphan</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;</span><span style="color: maroon;">key</span><span style="color: blue;"> </span><span style="color: red;">column</span><span style="color: blue;">=</span>"<span style="color: blue;">dictionary_id</span>"<span style="color: blue;">&gt;&lt;/</span><span style="color: maroon;">key</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;</span><span style="color: maroon;">index</span><span style="color: blue;"> </span><span style="color: red;">column</span><span style="color: blue;">=</span>"<span style="color: blue;">culture_id</span>"<span style="color: blue;"> </span><span style="color: red;">type</span><span style="color: blue;">=</span>"<span style="color: blue;">Int32</span>"<span style="color: blue;">&gt;&lt;/</span><span style="color: maroon;">index</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &nbsp; &lt;</span><span style="color: maroon;">element</span><span style="color: blue;"> </span><span style="color: red;">column</span><span style="color: blue;">=</span>"<span style="color: blue;">phrase</span>"<span style="color: blue;"> </span><span style="color: red;">type</span><span style="color: blue;">=</span>"<span style="color: blue;">String</span>"<span style="color: blue;">&gt;&lt;/</span><span style="color: maroon;">element</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;/</span><span style="color: maroon;">map</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &lt;/</span><span style="color: maroon;">class</span><span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&lt;/</span><span style="color: maroon;">hibernate-mapping</span><span style="color: blue;">&gt;</span></pre>
</div>
</div>
<p>The most important part here is the Dictionary (map) which stores the LCID as key and the translation as value. Thoses &#8220;phrases&#8221; are fully cascaded as it makes no sense for them to live without the <code>PhraseDictionary</code>. </p>
<p>Okay lets dig into the actual implementation&#8230;</p>
<div class="code">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<pre style="margin: 0px;"><span style="color: blue;">using</span> System;</pre>
<pre style="margin: 0px;"><span style="color: blue;">using</span> System.Collections.Generic;</pre>
<pre style="margin: 0px;"><span style="color: blue;">using</span> System.Text;</pre>
<pre style="margin: 0px;"><span style="color: blue;">using</span> System.Threading;</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;"><span style="color: blue;">namespace</span> MyProject.Core.Domain {</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">class</span> <span style="color: teal;">PhraseDictionary</span> : <span style="color: teal;">DomainObject</span>&lt;<span style="color: blue;">int</span>&gt; {</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> required for NHiberante</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">protected</span> PhraseDictionary()</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; : <span style="color: blue;">base</span>() {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Phrases = <span style="color: blue;">new</span> <span style="color: teal;">Dictionary</span>&lt;<span style="color: blue;">int</span>, <span style="color: blue;">string</span>&gt;();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;param name="name"&gt;</span><span style="color: green;">internal name only</span><span style="color: gray;">&lt;/param&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> PhraseDictionary(<span style="color: blue;">string</span> name)</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; : <span style="color: blue;">this</span>() {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Name = name;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: teal;">IDictionary</span>&lt;<span style="color: blue;">int</span>, <span style="color: blue;">string</span>&gt; _phrases;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">string</span> _name;</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Name of the dictionary. </span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> - only for internal use. Eases browsing data directly in database</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">virtual</span> <span style="color: blue;">string</span> Name {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { </pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> _name; </pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">set</span> { </pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _name = <span style="color: blue;">value</span>; </pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Stores all phrases for a lcid</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">virtual</span> <span style="color: teal;">IDictionary</span>&lt;<span style="color: blue;">int</span>, <span style="color: blue;">string</span>&gt; Phrases {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> { </pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> _phrases; </pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">private</span> <span style="color: blue;">set</span> { </pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _phrases = <span style="color: blue;">value</span>; </pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Adds a phrase for a given lcid</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> - if the lcid exists then the phrase is updated</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> - empty strings are considered as "not available". </span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;">&nbsp;&nbsp;&nbsp;  Thus adding an empty phrase might remove an existing if already existed</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;param name="lcid"&gt;&lt;/param&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;param name="phrase"&gt;&lt;/param&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;returns&gt;</span><span style="color: green;">Returns itself. Useful for method chaining.</span><span style="color: gray;">&lt;/returns&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">virtual</span> <span style="color: teal;">PhraseDictionary</span> SetPhrase(<span style="color: blue;">string</span> phrase, <span style="color: blue;">int</span> lcid) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (Phrases.ContainsKey(lcid)) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (<span style="color: blue;">string</span>.IsNullOrEmpty(phrase)) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Phrases.Remove(lcid);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } <span style="color: blue;">else</span> {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Phrases[lcid] = phrase;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } <span style="color: blue;">else</span> <span style="color: blue;">if</span> (!<span style="color: blue;">string</span>.IsNullOrEmpty(phrase)) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Phrases.Add(lcid, phrase);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">this</span>;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">virtual</span> <span style="color: teal;">PhraseDictionary</span> SetPhrase(<span style="color: blue;">string</span> phrase) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> SetPhrase(phrase, <span style="color: teal;">Thread</span>.CurrentThread.CurrentUICulture.LCID);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Gets a phrase for a given lcid</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> - returns empty string if it does not exist</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;param name="lcid"&gt;&lt;/param&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;returns&gt;&lt;/returns&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">virtual</span> <span style="color: blue;">string</span> GetPhrase(<span style="color: blue;">int</span> lcid) {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">if</span> (Phrases.ContainsKey(lcid)) <span style="color: blue;">return</span> Phrases[lcid];</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> <span style="color: blue;">string</span>.Empty;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">virtual</span> <span style="color: blue;">string</span> GetPhrase() {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> GetPhrase(<span style="color: teal;">Thread</span>.CurrentThread.CurrentUICulture.LCID);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> Gets the phrase for the current UI culrute</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">virtual</span> <span style="color: blue;">string</span> Phrase {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">get</span> {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> GetPhrase();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">set</span> {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; SetPhrase(<span style="color: blue;">value</span>);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">int</span> GetHashCode() {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> Phrases.GetHashCode();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">object</span> Copy() {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: teal;">PhraseDictionary</span> d = <span style="color: blue;">new</span> <span style="color: teal;">PhraseDictionary</span>();</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; d.Name = Name;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; d.Phrases = <span style="color: blue;">new</span> <span style="color: teal;">Dictionary</span>&lt;<span style="color: blue;">int</span>, <span style="color: blue;">string</span>&gt;(Phrases);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> d;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">public</span> <span style="color: blue;">override</span> <span style="color: blue;">string</span> ToString() {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> Phrase;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; }</pre>
<pre style="margin: 0px;">&nbsp;</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div>
<p>And here is how we would hook it up onto e.g. the <code>Name</code> property of a <code>Product</code> domain object.</p>
<div class="code">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<pre style="margin: 0px;"><span style="color: blue;">public</span> <span style="color: blue;">interface</span> <span style="color: teal;">IProduct</span> {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: green;">// contains all name translations</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: green;">// AllNames.Phrase gets/sets the translation for the current domain culture</span></pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: teal;">PhraseDictionary</span> AllNames { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div>
<p>This would allow us to set/receive the translation of name property conveniently as follows:</p>
<div class="code">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<pre style="margin: 0px;"><span style="color: green;">// set the culture to US english</span></pre>
<pre style="margin: 0px;"><span style="color: teal;">Thread</span>.CurrentThread.CurrentUICulture = <span style="color: blue;">new</span> CultureInfo(<span style="color: maroon;">"en-US"</span>);</pre>
<pre style="margin: 0px;">IProduct p = <span style="color: blue;">new</span> Product();</pre>
<pre style="margin: 0px;"><span style="color: green;">// sets product name in english</span></pre>
<pre style="margin: 0px;">p.AllNames.Phrase = <span style="color: maroon;">"Book"</span>;</pre>
<pre style="margin: 0px;"><span style="color: green;">// switch domain culture to german</span></pre>
<pre style="margin: 0px;"><span style="color: teal;">Thread</span>.CurrentThread.CurrentUICulture = <span style="color: blue;">new</span> CultureInfo(<span style="color: maroon;">"de-DE"</span>);</pre>
<pre style="margin: 0px;"><span style="color: green;">// sets product name in german</span></pre>
<pre style="margin: 0px;">p.AllNames.Phrase = <span style="color: maroon;">"Buch"</span>;</pre>
<pre style="margin: 0px;"><span style="color: green;">// both translations have been stored</span></pre>
<pre style="margin: 0px;">Debug.Assert(p.AllNames == 2);</pre>
<pre style="margin: 0px;"><span style="color: green;">// the current one should be german</span></pre>
<pre style="margin: 0px;">Debug.Assert(p.AllNames.Phrase == <span style="color: maroon;">"Buch"</span>);</pre>
</div>
</div>
<p>To keep everything together. Here is the part you require in your Product mapping file:</p>
<div class="code">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<pre style="margin: 0px;"><span style="color: blue;">&lt;</span><span style="color: maroon;">many-to-one</span><span style="color: blue;"> </span><span style="color: red;">name</span><span style="color: blue;">=</span>"<span style="color: blue;">AllNames</span>"</pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; </span><span style="color: red;">cascade</span><span style="color: blue;">=</span>"<span style="color: blue;">all-delete-orphan</span>"<span style="color: blue;">&gt;</span></pre>
<pre style="margin: 0px;"><span style="color: blue;">&nbsp; &nbsp; &lt;/</span><span style="color: maroon;">many-to-one</span><span style="color: blue;">&gt;</span></pre>
</div>
</div>
<p>One requirement was an easy use of the translations within HQL query. Here is an example of how we would fet all products sorted by the name of the current culture:</p>
<div class="code">
<div style="font-family: Courier New; font-size: 8pt; color: black; background: white;">
<pre style="margin: 0px;"><span style="color: green;">// Gets all products sorted by the name of the current culture</span></pre>
<pre style="margin: 0px;"><span style="color: teal;">IList</span>&lt;Product&gt; GetAllSortedByName() {</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">string</span> query = <span style="color: maroon;">@"</span></pre>
<pre style="margin: 0px;"><span style="color: maroon;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; from Product product</span></pre>
<pre style="margin: 0px;"><span style="color: maroon;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; left outer join product.AllNames allNames </span></pre>
<pre style="margin: 0px;"><span style="color: maroon;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; where index(allNames) = :lcid </span></pre>
<pre style="margin: 0px;"><span style="color: maroon;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; order by allNames"</span>;</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; IQuery q = NHibernateSession.CreateQuery(query);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; q.SetParameter(<span style="color: maroon;">"lcid"</span>, <span style="color: teal;">Thread</span>.CurrentThread.CurrentUICulture.LCID);</pre>
<pre style="margin: 0px;">&nbsp;&nbsp;&nbsp; <span style="color: blue;">return</span> q.List&lt;Product&gt;();</pre>
<pre style="margin: 0px;">}</pre>
</div>
</div>
<p>That was it. Thanks for having the read and I am looking forward to lots of feedback which could improve that approach. </p>
<p><strong>Here are some quick remarks:</strong><br />
- I strongly recommend creating a <code>DomainCulture</code> class which encapsulates the current culture and how it is stored.<br />
- Probably it would be useful to make the <code>PhraseDictionary.Phrases</code> readonly to the public.</p>
<div><table> <td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://www.webdevbros.net/2009/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/&amp;t=Create+a+multi+languaged+domain+model+with+NHibernate+and+C%23&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/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/&amp;title=Create+a+multi+languaged+domain+model+with+NHibernate+and+C%23&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/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/&amp;title=Create+a+multi+languaged+domain+model+with+NHibernate+and+C%23&amp;t=1 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><script type="text/javascript"><!--yahooBuzzArticleHeadline=Create+a+multi+languaged+domain+model+with+NHibernate+and+C%23;//--></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/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/'; 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/06/24/create-a-multi-languaged-domain-model-with-nhibernate-and-c/feed/</wfw:commentRss>
		<slash:comments>2</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 [...]]]></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-13"><a href="#" onclick="javascript:showCodeTxt('csharp-13'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-13">
<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-14"><a href="#" onclick="javascript:showCodeTxt('csharp-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-14">
<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-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: #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-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: #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-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: #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-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;">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-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: #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-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;">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-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;">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-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: #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-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;">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-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: #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>Singelton application with C#</title>
		<link>http://www.webdevbros.net/2007/11/14/singelton-application-with-c/</link>
		<comments>http://www.webdevbros.net/2007/11/14/singelton-application-with-c/#comments</comments>
		<pubDate>Wed, 14 Nov 2007 11:06:36 +0000</pubDate>
		<dc:creator>Michal</dc:creator>
				<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.webdevbros.net/2007/11/14/singelton-application-with-c/</guid>
		<description><![CDATA[A singelton application is an application which only allows running one instance of itself 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. Here [...]]]></description>
			<content:encoded><![CDATA[<p>A singelton application is an application which only allows running one instance of itself 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. Here is the source of how to create a singelton application with C# and .net ...<span id="more-117"></span></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: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Threading</span>;</div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</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;">//Allows to run the application only once per machine</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;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">class</span> SingeltonApplication <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: #0600FF;">static</span> Mutex _mutex;</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> Run<span style="color: #000000;">&#40;</span>Form form<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; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>IsFirstInstance<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</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; Application.<span style="color: #0000FF;">ApplicationExit</span> += OnExit;</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; Application.<span style="color: #0000FF;">Run</span><span style="color: #000000;">&#40;</span>mainForm<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; <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; </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: #FF0000;">bool</span> IsFirstInstance<span style="color: #000000;">&#40;</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; Assembly a = Assembly.<span style="color: #0000FF;">GetEntyAssembly</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; &nbsp; _mutex = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Mutex<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">false</span>, a.<span style="color: #0000FF;">FullName</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; <span style="color: #FF0000;">bool</span> owned = <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; &nbsp; &nbsp; owned = _mutex.<span style="color: #0000FF;">WaitOne</span><span style="color: #000000;">&#40;</span>TimeSpan.<span style="color: #0000FF;">Zero</span>, <span style="color: #0600FF;">false</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; <span style="color: #0600FF;">return</span> owned;</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; </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;">static</span> <span style="color: #0600FF;">void</span> OnExit<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs args<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; _mutex.<span style="color: #0000FF;">ReleaseMutex</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; &nbsp; _mutex.<span style="color: #0000FF;">Close</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;">&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>The trick here is to get a cross-process communication because every application runs in its own process. This is achieved with a <a href="http://msdn2.microsoft.com/en-us/library/system.threading.mutex.aspx">Mutex</a> which is available system wide. We use the name of the application for the identification of the Mutex.</p>
<p>the source is inspired from the book ".net Components, Juval Löwy, O'Reilly 2005 United States" page 231</p>
<div><table> <td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://www.webdevbros.net/2007/11/14/singelton-application-with-c/&amp;t=Singelton+application+with+C%23&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/singelton-application-with-c/&amp;title=Singelton+application+with+C%23&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/singelton-application-with-c/&amp;title=Singelton+application+with+C%23&amp;t=1 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><script type="text/javascript"><!--yahooBuzzArticleHeadline=Singelton+application+with+C%23;//--></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/singelton-application-with-c/'; 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/singelton-application-with-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TimeRange object for C#</title>
		<link>http://www.webdevbros.net/2007/08/24/timerange-object-for-c/</link>
		<comments>http://www.webdevbros.net/2007/08/24/timerange-object-for-c/#comments</comments>
		<pubDate>Fri, 24 Aug 2007 13:36:48 +0000</pubDate>
		<dc:creator>Michal</dc:creator>
				<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://www.webdevbros.net/2007/08/24/timerange-object-for-c/</guid>
		<description><![CDATA[In one of my recent .net projects (C#) I had the need to implement an object which represents a time range which is defined by a start time and an end time. In addition it should detect collision with other time ranges and check if given TimeSpans are within the time range itself. Last but [...]]]></description>
			<content:encoded><![CDATA[<p>In one of my recent .net projects (C#) I had the need to implement an object which represents a time range which is defined by a start time and an end time. In addition it should detect collision with other time ranges and check if given TimeSpans are within the time range itself. Last but not least it should support parsing time ranges from strings and format the range in a nice way. For this I've written a small class which could be useful for some of you out there.<span id="more-112"></span></p>
<p>Let's have a look how this thing needs to be used. There are three constructors for the usage so the creation of instances can be done in various ways. Additionally an instance can be obtained by using the Parse method. Some examples of creation...</p>
<div class="igBar"><span id="lcsharp-30"><a href="#" onclick="javascript:showCodeTxt('csharp-30'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-30">
<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;">TimeRange t = <span style="color: #0600FF;">null</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;">//instances for a range of 10:00 - 12:00</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;">t = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TimeRange<span style="color: #000000;">&#40;</span><span style="color: #FF0000;color:#800000;">10</span>, <span style="color: #FF0000;color:#800000;">0</span>, <span style="color: #FF0000;color:#800000;">12</span>, <span style="color: #FF0000;color:#800000;">0</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;">t = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TimeRange<span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TimeSpan<span style="color: #000000;">&#40;</span><span style="color: #FF0000;color:#800000;">10</span>, <span style="color: #FF0000;color:#800000;">0</span>, <span style="color: #FF0000;color:#800000;">0</span><span style="color: #000000;">&#41;</span>, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TimeSpan<span style="color: #000000;">&#40;</span><span style="color: #FF0000;color:#800000;">12</span>, <span style="color: #FF0000;color:#800000;">0</span>, <span style="color: #FF0000;color:#800000;">0</span><span style="color: #000000;">&#41;</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;</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;">//time range 00:00-24:00</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;">t = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TimeRange<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;</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;">//parsing time ranges from a string</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">t = TimeRange.<span style="color: #0000FF;">Parse</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">"10-12"</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;">t = TimeRange.<span style="color: #0000FF;">Parse</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">"10:00-12:00"</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>If we have to ore more time ranges we can check if they colide. In other words if they overlap or clash. Everything the same :)</p>
<div class="igBar"><span id="lcsharp-31"><a href="#" onclick="javascript:showCodeTxt('csharp-31'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-31">
<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;">TimeRange t = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TimeRange<span style="color: #000000;">&#40;</span><span style="color: #FF0000;color:#800000;">10</span>, <span style="color: #FF0000;color:#800000;">0</span>, <span style="color: #FF0000;color:#800000;">12</span>, <span style="color: #FF0000;color:#800000;">0</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;"><span style="color: #FF0000;">bool</span> clash = t.<span style="color: #0000FF;">Clashes</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TimeRange<span style="color: #000000;">&#40;</span><span style="color: #FF0000;color:#800000;">10</span>, <span style="color: #FF0000;color:#800000;">0</span>, <span style="color: #FF0000;color:#800000;">11</span>, <span style="color: #FF0000;color:#800000;">0</span><span style="color: #000000;">&#41;</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;</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;">//clash with bounds will clash 10:00-12:00 with 09:00-10:00</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;">t = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TimeRange<span style="color: #000000;">&#40;</span><span style="color: #FF0000;color:#800000;">10</span>, <span style="color: #FF0000;color:#800000;">0</span>, <span style="color: #FF0000;color:#800000;">12</span>, <span style="color: #FF0000;color:#800000;">0</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;">clash = t.<span style="color: #0000FF;">Clashes</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TimeRange<span style="color: #000000;">&#40;</span><span style="color: #FF0000;color:#800000;">9</span>, <span style="color: #FF0000;color:#800000;">0</span>, <span style="color: #FF0000;color:#800000;">10</span>, <span style="color: #FF0000;color:#800000;">0</span><span style="color: #000000;">&#41;</span>, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>We can also check if a given TimeSpan is within the range or even another TimeRange.</p>
<div class="igBar"><span id="lcsharp-32"><a href="#" onclick="javascript:showCodeTxt('csharp-32'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-32">
<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;">//we check if the timerange 11:10-12:00 lies completely in 10:30-12:45</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">TimeRange t = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TimeRange<span style="color: #000000;">&#40;</span><span style="color: #FF0000;color:#800000;">10</span>, <span style="color: #FF0000;color:#800000;">30</span>, <span style="color: #FF0000;color:#800000;">12</span>, <span style="color: #FF0000;color:#800000;">45</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: #FF0000;">bool</span> inside = t.<span style="color: #0000FF;">IsIn</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TimeRange<span style="color: #000000;">&#40;</span><span style="color: #FF0000;color:#800000;">11</span>, <span style="color: #FF0000;color:#800000;">10</span>, <span style="color: #FF0000;color:#800000;">12</span>, <span style="color: #FF0000;color:#800000;">0</span><span style="color: #000000;">&#41;</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;</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;">//check if 18:00 is in the range</span></div>
</li>
<li style="font-weight: bold;color:#767676;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">inside = t.<span style="color: #0000FF;">IsIn</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TimeSpan<span style="color: #000000;">&#40;</span><span style="color: #FF0000;color:#800000;">18</span>, <span style="color: #FF0000;color:#800000;">0</span>, <span style="color: #FF0000;color:#800000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>That should be it. Have fun with it and dont hasitate to drop me any feedback about this stuff or even extensions.</p>
<p><strong>Overview of C# TimeRange:</strong></p>
<ul>
<li>Parsing a timerange out of a string (supports various formats)</li>
<li>Parses also TimeSpan from a string (ParseTimeSpan method)</li>
<li>Collision detection of two timeranges (inclusive and exclusive)</li>
<li>Detection of a given TimeSpan is within the Range</li>
<li>Equality check of time ranges</li>
<li>Formatting in a user friendly way (ToString overridden)</li>
</ul>
<p>The TimeRange for C# tested with .net framework 2.0 can be downloaded here...</p>
<p><a href='/wp-content/uploads/2007/08/timerange.zip' title='TimeRange for C#'><strong>TimeRange class for C#</strong></a></p>
<div><table> <td><iframe src='http://digg.com/api/diggthis.php?w=new&amp;u=http://www.webdevbros.net/2007/08/24/timerange-object-for-c/&amp;t=TimeRange+object+for+C%23&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/08/24/timerange-object-for-c/&amp;title=TimeRange+object+for+C%23&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/08/24/timerange-object-for-c/&amp;title=TimeRange+object+for+C%23&amp;t=1 ' height='80' width='52' scrolling='no' frameborder='0' ></iframe></td> <td><script type="text/javascript"><!--yahooBuzzArticleHeadline=TimeRange+object+for+C%23;//--></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/08/24/timerange-object-for-c/'; 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/08/24/timerange-object-for-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
