<?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>sides of march &#187; C#/.Net</title>
	<atom:link href="http://www.sidesofmarch.com/index.php/archive/tag/cnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sidesofmarch.com</link>
	<description>Thoughts on life, liberty, and information technology</description>
	<lastBuildDate>Mon, 16 Jan 2012 02:43:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Operator overloading your domain model with interfaces and base classes</title>
		<link>http://www.sidesofmarch.com/index.php/archive/2008/02/20/operator-overloading-your-domain-model-with-interfaces-and-base-classes/</link>
		<comments>http://www.sidesofmarch.com/index.php/archive/2008/02/20/operator-overloading-your-domain-model-with-interfaces-and-base-classes/#comments</comments>
		<pubDate>Wed, 20 Feb 2008 14:22:50 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[Baseball]]></category>
		<category><![CDATA[C#/.Net]]></category>

		<guid isPermaLink="false">http://www.sidesofmarch.com/index.php/archive/2008/02/20/operator-overloading-your-domain-model-with-interfaces-and-base-classes/</guid>
		<description><![CDATA[<p>One of the challenges in rewriting <a href="http://www.csfbl.com">my online baseball game</a> is dealing with enormous amounts of data that needs to be stored as aggregates, and coming up with a domain model and data mapping pattern that works. In this blog post, I&#8217;ll outline how I addressed some of those issues.</p>

The Data Model
<p>Baseball is very much a statistics-oriented game. Consider fielding statistics: putouts (PO), assists (A), errors (E) and others. These stats need to be stored:</p>

Per game, for each player who played in the game, for each position he played (key fields: game, player, position) 
Per season, for each player, for each team he played for, for each position he played (key fields: season, player, team, position) 
Career, for each player, for each position he played (key fields: player, position) 

<p>On the database side, that results in three tables: GameFieldingStats, SeasonFieldingStats, and CareerFieldingStats. Each has the same set of fields to store the statistics (PO, A, and E); the differences are <span style="color:#777"> . . .<br /><br />&#8594; Read More: <a href="http://www.sidesofmarch.com/index.php/archive/2008/02/20/operator-overloading-your-domain-model-with-interfaces-and-base-classes/">Operator overloading your domain model with interfaces and base classes</a></span>]]></description>
			<content:encoded><![CDATA[<p>One of the challenges in rewriting <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.csfbl.com&sref=rss">my online baseball game</a> is dealing with enormous amounts of data that needs to be stored as aggregates, and coming up with a domain model and data mapping pattern that works. In this blog post, I&#8217;ll outline how I addressed some of those issues.</p>
<h3></h3>
<h3>The Data Model</h3>
<p>Baseball is very much a statistics-oriented game. Consider fielding statistics: putouts (PO), assists (A), errors (E) and others. These stats need to be stored:</p>
<ul>
<li>Per game, for each player who played in the game, for each position he played (key fields: game, player, position) </li>
<li>Per season, for each player, for each team he played for, for each position he played (key fields: season, player, team, position) </li>
<li>Career, for each player, for each position he played (key fields: player, position) </li>
</ul>
<p>On the database side, that results in three tables: <code>GameFieldingStats</code>, <code>SeasonFieldingStats</code>, and <code>CareerFieldingStats</code>. Each has the same set of fields to store the statistics (<code>PO</code>, <code>A</code>, and <code>E</code>); the differences are in the key fields for each, as outlined in the diagram below. (Note: For the remainder of this post, I&#8217;ll include only the first two of those tables to keep things short.)</p>
<p><span id="more-225"></span></p>
<p><a href="http://www.sidesofmarch.com/wp-content/uploads/image2.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="167" alt="image" src="http://www.sidesofmarch.com/wp-content/uploads/image_thumb2.png" width="296" border="0" /></a> </p>
<p>Writing the domain model for each of these tables is quite easy. The following snippets shows the class declaration for each, including <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.castleproject.org%2FActiveRecord&sref=rss">Castle ActiveRecord</a> attributes. Note that these classes inherit from a base class, <code>EntityBase&lt;T&gt;</code>, which in this project is a requirement for all domain objects.</p>
<pre class="brush: c-sharp; ">

[ActiveRecord(&quot;bb_gamefieldingstats&quot;, Lazy = true)]
public class GameFieldingStats : EntityBase&lt;gamefieldingstats&gt;
{
    //...
}
[ActiveRecord(&quot;bb_seasonfieldingstats&quot;, Lazy = true)]
public class SeasonFieldingStats : EntityBase&lt;seasonfieldingstats&gt;
{
    //...
}
</pre>
<h3>Use cases, interfaces, base classes, and operator overloads (whew!)</h3>
<p>Now that I have these classes, I want to use them! After each baseball game is simulated, a number of <code>GameFieldingStats</code> records are created to reflect the statistics of that game. I want to add those stats to the player&#8217;s <code>SeasonFieldingStats</code>. I could access the similar properties in each class (PO, A, E) and add them together, but that&#8217;s not what I want. I want to do this:</p>
<pre class="brush: c-sharp; ">

GameBattingStats gbs = new GameBattingStats();
SeasonBattingStats sbs = new SeasonBattingStats();
// use cases follow
SeasonBattingStats combined = sbs + gbs;
SeasonBattingStats removed = sbs - gbs;
sbs += gbs; //!!
</pre>
<p>It&#8217;s easy enough to do that using operator overloads in C#. To make that work, I need all fielding stats classes to inherit from a base <code>FieldingStats</code> class that implements the operator overloading. I&#8217;m going to take it one step further and add an <code>IFieldingStats</code> interface. The class diagram below shows how this was done.</p>
<p><a href="http://www.sidesofmarch.com/wp-content/uploads/image3.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="175" alt="image" src="http://www.sidesofmarch.com/wp-content/uploads/image_thumb3.png" width="619" border="0" /></a> </p>
<p>Unfortunately, by doing this, I can no longer have <code>GameFieldingStats</code> and <code>SeasonFieldingStats</code> inherit from my domain object base class, <code>EntityBase&lt;T&gt;</code> (since C# doesn&#8217;t allow multiple inheritance of classes). To resolve that, I made <code>FieldingStats</code> into a generic class, and had it inherit from <code>EntityBase&lt;T&gt;</code>. The class diagram shows the adjusted model. </p>
<p><a href="http://www.sidesofmarch.com/wp-content/uploads/image4.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="175" alt="image" src="http://www.sidesofmarch.com/wp-content/uploads/image_thumb4.png" width="619" border="0" /></a> </p>
<p>The operator overloads are implemented on the generic <code>FieldingStats&lt;T&gt;</code> class, as follows:</p>
<pre class="brush: c-sharp; ">

public static T operator +(FieldingStats&lt;t&gt; fs1, IFieldingStats fs2)
{
    if (fs1 == null)
        throw new ArgumentNullException(&quot;fs1&quot;);
    if (fs2 == null)
        throw new ArgumentNullException(&quot;fs2&quot;);
    T fs3 = new T();
    fs3.A = fs1.A + fs2.A;
    fs3.E = fs1.E + fs2.E;
    fs3.PO = fs1.PO + fs2.PO;
    return fs3;
}
</pre>
<p>So far, so good. Everything compiles, and on paper it works. But how do we test it?</p>
<h3>Unit testing: does it work?</h3>
<p>To test this, I wrote the following unit tests.</p>
<pre class="brush: c-sharp; ">

[TestFixture]
public class FieldingStatsTests
{
	[Test]
	public void AddFieldingStatsTests()
	{
		GameFieldingStats fs1 = new GameFieldingStats();
		fs1.PO = 1;
		fs1.A = 2;
		fs1.E = 3;

		IFieldingStats fs2 = new GameFieldingStats();
		fs2.PO = 100;
		fs2.A = 200;
		fs2.E = 300;

		IFieldingStats fs3 = fs1 + fs2;
		Assert.AreEqual(fs3.PO, (fs1.PO + fs2.PO), &quot;Adding FieldingStats yields incorrect PO&quot;);
		Assert.AreEqual(fs3.A, (fs1.A + fs2.A), &quot;Adding FieldingStats yields incorrect A&quot;);
		Assert.AreEqual(fs3.E, (fs1.E + fs2.E), &quot;Adding FieldingStats yields incorrect E&quot;);
	}
}
</pre>
<p>All tests pass!</p>
<p>By combining the base classes, interfaces, generics, and operator overloads, I can now add and subtract domain objects from each other, and my code has become much more readable. It wasn&#8217;t obvious how to do it at first, but once the pieces fell together, it made perfect sense.</p>
<p></t></seasonfieldingstats></gamefieldingstats></p>
<img src="http://www.sidesofmarch.com/?ak_action=api_record_view&id=225&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.sidesofmarch.com/index.php/archive/2008/02/20/operator-overloading-your-domain-model-with-interfaces-and-base-classes/' addthis:title='Operator overloading your domain model with interfaces and base classes ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.sidesofmarch.com/index.php/archive/2008/02/20/operator-overloading-your-domain-model-with-interfaces-and-base-classes/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Assess your .Net skills (and get a job) with this online test</title>
		<link>http://www.sidesofmarch.com/index.php/archive/2007/11/28/assess-your-net-skills-and-get-a-job-with-this-online-test/</link>
		<comments>http://www.sidesofmarch.com/index.php/archive/2007/11/28/assess-your-net-skills-and-get-a-job-with-this-online-test/#comments</comments>
		<pubDate>Thu, 29 Nov 2007 02:06:50 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[C#/.Net]]></category>

		<guid isPermaLink="false">http://www.sidesofmarch.com/index.php/archive/2007/11/28/assess-your-net-skills-and-get-a-job-with-this-online-test/</guid>
		<description><![CDATA[<p><a href="http://www.brantestes.net/">Brant Estes</a> of <a href="http://www.magenic.com/">Magenic Technologies</a> has just posted a very nice <a href="http://www.brantestes.net/Test.aspx">online quiz for .Net programmers</a>. The test is there to pre-screen potential candidates to work at Magenic, but it&#8217;s also a very nice test for any .Net programmer to take (or to give to any .Net programmer you have considered hiring).</p>
<p>I took the test and scored a 75 out of 100 (without cheating, mind you), and was told I may make a great addition to Magenic. Unfortunately, I live in New York, and I&#8217;m not relocating. Besides, I just put in my resignation with my current employer so I can go back to independent consulting, so I&#8217;m not exactly on the market for full-time employment&#8230; but more on that another day.</p>
<a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a <span style="color:#777"> . . .<br /><br />&#8594; Read More: <a href="http://www.sidesofmarch.com/index.php/archive/2007/11/28/assess-your-net-skills-and-get-a-job-with-this-online-test/">Assess your .Net skills (and get a job) with this online test</a></span>]]></description>
			<content:encoded><![CDATA[<p><a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.brantestes.net%2F&sref=rss">Brant Estes</a> of <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.magenic.com%2F&sref=rss">Magenic Technologies</a> has just posted a very nice <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.brantestes.net%2FTest.aspx&sref=rss">online quiz for .Net programmers</a>. The test is there to pre-screen potential candidates to work at Magenic, but it&#8217;s also a very nice test for any .Net programmer to take (or to give to any .Net programmer you have considered hiring).</p>
<p><img id="image181" style="float:right; margin:0 0 8px 8px;" src="http://www.sidesofmarch.com/wp-content/uploads/test_score.gif" alt="My Magenic Technologies .Net test score" align="right" />I took the test and scored a 75 out of 100 (without cheating, mind you), and was told I may make a great addition to Magenic. Unfortunately, I live in New York, and I&#8217;m not relocating. Besides, I just put in my resignation with my current employer so I can go back to independent consulting, so I&#8217;m not exactly on the market for full-time employment&#8230; but more on that another day.</p>
<img src="http://www.sidesofmarch.com/?ak_action=api_record_view&id=182&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.sidesofmarch.com/index.php/archive/2007/11/28/assess-your-net-skills-and-get-a-job-with-this-online-test/' addthis:title='Assess your .Net skills (and get a job) with this online test ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.sidesofmarch.com/index.php/archive/2007/11/28/assess-your-net-skills-and-get-a-job-with-this-online-test/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A simple asp:Repeater replacement for simple needs</title>
		<link>http://www.sidesofmarch.com/index.php/archive/2007/10/28/a-simple-asprepeater-replacement-for-simple-needs/</link>
		<comments>http://www.sidesofmarch.com/index.php/archive/2007/10/28/a-simple-asprepeater-replacement-for-simple-needs/#comments</comments>
		<pubDate>Sun, 28 Oct 2007 12:49:44 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C#/.Net]]></category>

		<guid isPermaLink="false">http://www.sidesofmarch.com/index.php/archive/2007/10/28/a-simple-asprepeater-replacement-for-simple-needs/</guid>
		<description><![CDATA[<p>How many times have you had to parse through a collection of objects, outputting nothing more than a comma-delimited list of items in the result set. Typically, you&#8217;ll do this:</p>


&#60;asp :repeater runat=&#34;server&#34;&#62;
	&#60;itemtemplate&#62;Eval(&#34;Name&#34;)&#60;/itemtemplate&#62;
	&#60;separatortemplate&#62;, &#60;/separatortemplate&#62;
&#60;/asp&#62;

<p>If you were parsing a list of states, it might look like this:</p>
<blockquote><p>Alabama, Alaska, Arizona, Arkansas</p></blockquote>
<p>There&#8217;s an easier way to handle these simple needs: create a CollectionToString() method. This method would accept an IEnumerable and, using reflection, read a property and return a delimited string. Of course, you choose the property and delimiter.</p>
<p>Note that we have two versions of this method: one which accepts an IEnumerable, the other which accepts an object. This is done to avoid the necessary typecasting in your code, since Eval() returns everything as an object.</p>


public static string CollectionToString(IEnumerable collection, string property, string delimiter)
{
	IEnumerator enumerator = collection.GetEnumerator();
	if (enumerator == null &#124;&#124; enumerator.MoveNext() == false)
		return String.Empty;

	Type type = enumerator.Current.GetType();
	PropertyInfo propInfo = type.GetProperty(property);
	if (propInfo == null)
		throw new Exception(String.Format(&#34;Property &#039;{0}&#039; not found in collection&#34;, property));

	StringBuilder output = new <span style="color:#777"> . . .<br /><br />&#8594; Read More: <a href="http://www.sidesofmarch.com/index.php/archive/2007/10/28/a-simple-asprepeater-replacement-for-simple-needs/">A simple asp:Repeater replacement for simple needs</a></span>]]></description>
			<content:encoded><![CDATA[<p>How many times have you had to parse through a collection of objects, outputting nothing more than a comma-delimited list of items in the result set. Typically, you&#8217;ll do this:</p>
<pre class="brush: html; ">

&lt;asp :repeater runat=&quot;server&quot;&gt;
	&lt;itemtemplate&gt;Eval(&quot;Name&quot;)&lt;/itemtemplate&gt;
	&lt;separatortemplate&gt;, &lt;/separatortemplate&gt;
&lt;/asp&gt;
</pre>
<p>If you were parsing a list of states, it might look like this:</p>
<blockquote><p>Alabama, Alaska, Arizona, Arkansas</p></blockquote>
<p>There&#8217;s an easier way to handle these simple needs: create a <code>CollectionToString()</code> method. This method would accept an <code>IEnumerable</code> and, using reflection, read a property and return a delimited string. Of course, you choose the property and delimiter.</p>
<p>Note that we have two versions of this method: one which accepts an <code>IEnumerable</code>, the other which accepts an object. This is done to avoid the necessary typecasting in your code, since <code>Eval()</code> returns everything as an <code>object</code>.</p>
<pre class="brush: c-sharp; ">

public static string CollectionToString(IEnumerable collection, string property, string delimiter)
{
	IEnumerator enumerator = collection.GetEnumerator();
	if (enumerator == null || enumerator.MoveNext() == false)
		return String.Empty;

	Type type = enumerator.Current.GetType();
	PropertyInfo propInfo = type.GetProperty(property);
	if (propInfo == null)
		throw new Exception(String.Format(&quot;Property &#039;{0}&#039; not found in collection&quot;, property));

	StringBuilder output = new StringBuilder();
	output.Append(propInfo.GetValue(enumerator.Current, null).ToString());

	while (enumerator.MoveNext())
	{
		output.Append(delimiter);
		output.Append(propInfo.GetValue(enumerator.Current, null).ToString());
	}

	return output.ToString();
}

public static string CollectionToString(object collection, string property, string delimiter)
{
	return CollectionToString(collection as IEnumerable, property, delimiter);
}
</pre>
<p>This method &#8212; which I typically apply to a <code>StringHelper</code> class whose namespace is added to my web.config (more on that another day) &#8212; can replace the Repeater code above as follows.</p>
<pre class="brush: html; ">

&lt;!-- you must first expose the collection to the ASPX page as a public property --&gt;
&lt; %= StringHelper.CollectionToString(&quot;ListOfStates&quot;, &quot;Name&quot;, &quot;, &quot;) %&gt;
</pre>
<p>A similar method could be added to a helper class in <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.castleproject.org%2Fmonorail&sref=rss">Castle MonoRail</a> to do the same without using <code>foreach</code> loops in your view.</p>
<img src="http://www.sidesofmarch.com/?ak_action=api_record_view&id=177&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.sidesofmarch.com/index.php/archive/2007/10/28/a-simple-asprepeater-replacement-for-simple-needs/' addthis:title='A simple asp:Repeater replacement for simple needs ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.sidesofmarch.com/index.php/archive/2007/10/28/a-simple-asprepeater-replacement-for-simple-needs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A repository that works with multiple O/R mappers &#8212; is it possible?</title>
		<link>http://www.sidesofmarch.com/index.php/archive/2007/08/16/a-repository-that-works-with-multiple-or-mappers-is-it-possible/</link>
		<comments>http://www.sidesofmarch.com/index.php/archive/2007/08/16/a-repository-that-works-with-multiple-or-mappers-is-it-possible/#comments</comments>
		<pubDate>Thu, 16 Aug 2007 17:12:58 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[C#/.Net]]></category>
		<category><![CDATA[O/R Mappers]]></category>

		<guid isPermaLink="false">http://www.sidesofmarch.com/index.php/archive/2007/08/16/a-repository-that-works-with-multiple-or-mappers-is-it-possible/</guid>
		<description><![CDATA[<p>A <a href="http://groups.google.com/group/wilsonorwrapper/browse_thread/thread/efbec0125b6ed696/93c257fd2f14a3c3?hl=en#93c257fd2f14a3c3">recent post</a> on the <a href="http://groups.google.com/group/wilsonorwrapper?hl=en">WilsonORWrapper Google group</a> talked about making WORW multi-database aware by removing static classes and singletons. Well, I&#8217;ve kind of been thinking of something similar lately&#8230;</p>
<p>For some time I have been toying with the idea of rewriting WORW to support a generic repository service that would work with any O/R mapper, much
like the logger and cache services work over different underlying libraries. By removing the static classes and singletons and adding a provider model for the O/R wrapper element, you would be able to instantiate multiple instances of a &#8220;Registry&#8221; class, a class which provides a set of services (Repository, Cache, Logger, etc.) based on a given configuration criteira.</p>
<p>As a test I wrote up a simple project (with unit tests!) as a proof-of-concept, using WORM and NPersist as underlying O/R mappers. The WORM part worked fine. The NPersist part almost worked &#8212; however, I believe the errors are related to using NPersist&#8217;s not-too-well-documented XML provider, <span style="color:#777"> . . .<br /><br />&#8594; Read More: <a href="http://www.sidesofmarch.com/index.php/archive/2007/08/16/a-repository-that-works-with-multiple-or-mappers-is-it-possible/">A repository that works with multiple O/R mappers &#8212; is it possible?</a></span>]]></description>
			<content:encoded><![CDATA[<p>A <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fgroups.google.com%2Fgroup%2Fwilsonorwrapper%2Fbrowse_thread%2Fthread%2Fefbec0125b6ed696%2F93c257fd2f14a3c3%3Fhl%3Den%2393c257fd2f14a3c3&sref=rss">recent post</a> on the <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fgroups.google.com%2Fgroup%2Fwilsonorwrapper%3Fhl%3Den&sref=rss">WilsonORWrapper Google group</a> talked about making WORW multi-database aware by removing static classes and singletons. Well, I&#8217;ve kind of been thinking of something similar lately&#8230;</p>
<p>For some time I have been toying with the idea of rewriting WORW to support a generic repository service that would work with any O/R mapper, much<br />
like the logger and cache services work over different underlying libraries. By removing the static classes and singletons and adding a provider model for the O/R wrapper element, you would be able to instantiate multiple instances of a &#8220;Registry&#8221; class, a class which provides a set of services (Repository, Cache, Logger, etc.) based on a given configuration criteira.</p>
<p>As a test I wrote up a simple project (with unit tests!) as a proof-of-concept, using WORM and NPersist as underlying O/R mappers. The WORM part worked fine. The NPersist part almost worked &#8212; however, I believe the errors are related to using NPersist&#8217;s not-too-well-documented XML provider, not errors with the O/R mapper provider itself (NPersist does initializes properly, which indicates at least something is working right).</p>
<p>If you&#8217;re interested in seeing this little stub of a project, <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fgroups.google.com%2Fgroup%2Fwilsonorwrapper%2Ffiles%3Fhl%3Den&sref=rss">download the source</a> (27.5kb). I&#8217;ve dubbed it <strong>NRepository</strong> right now, not for any reason except it&#8217;s the first thing that came to mind. Let me know what you think, and whether you think it&#8217;s worth pursuing such a project.</p>
<img src="http://www.sidesofmarch.com/?ak_action=api_record_view&id=145&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.sidesofmarch.com/index.php/archive/2007/08/16/a-repository-that-works-with-multiple-or-mappers-is-it-possible/' addthis:title='A repository that works with multiple O/R mappers &#8212; is it possible? ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.sidesofmarch.com/index.php/archive/2007/08/16/a-repository-that-works-with-multiple-or-mappers-is-it-possible/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use reflection to compare the properties of two objects</title>
		<link>http://www.sidesofmarch.com/index.php/archive/2007/08/03/use-reflection-to-compare-the-properties-of-two-objects/</link>
		<comments>http://www.sidesofmarch.com/index.php/archive/2007/08/03/use-reflection-to-compare-the-properties-of-two-objects/#comments</comments>
		<pubDate>Sat, 04 Aug 2007 00:43:05 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[C#/.Net]]></category>

		<guid isPermaLink="false">http://www.sidesofmarch.com/index.php/archive/2007/08/03/use-reflection-to-compare-the-properties-of-two-objects/</guid>
		<description><![CDATA[<p>In an update to <a href="http://www.sidesofmarch.com/index.php/projects/wilsonorwrapper/">WilsonORWrapper</a>, I added a method which takes two objects of the same type and compares the properties of each, returning a value reflecting the results of the comparison. Any value other than zero would indicate that at least one property on the objects are not equal.</p>
<p>This method may have interest to people who don&#8217;t use WilsonORWrapper, so here&#8217;s an extracted version of the code that does the comparison.</p>


using System;
using System.Reflection;

public static class ObjectHelper&#60;t&#62;
{
	public static int Compare(T x, T y)
	{
		Type type = typeof(T);
		PropertyInfo[] properties = type.GetProperties(BindingFlags.DeclaredOnly &#124; BindingFlags.Public);
		FieldInfo[] fields = type.GetFields(BindingFlags.DeclaredOnly &#124; BindingFlags.Public);
		int compareValue = 0;

		foreach (PropertyInfo property in properties)
		{
			IComparable valx = property.GetValue(x, null) as IComparable;
			if (valx == null)
				continue;
			object valy = property.GetValue(y, null);
			compareValue = valx.CompareTo(valy);
			if (compareValue != 0)
				return compareValue;
		}
		foreach (FieldInfo field in fields)
		{
			IComparable valx = field.GetValue(x) as IComparable;
			if (valx == null)
				continue;
			object valy = field.GetValue(y);
			compareValue = valx.CompareTo(valy);
			if (compareValue != 0)
				return compareValue;
		}

		return compareValue;
	}
}

<p>With that, if you had a Name class in your code that had two properties, First and <span style="color:#777"> . . .<br /><br />&#8594; Read More: <a href="http://www.sidesofmarch.com/index.php/archive/2007/08/03/use-reflection-to-compare-the-properties-of-two-objects/">Use reflection to compare the properties of two objects</a></span>]]></description>
			<content:encoded><![CDATA[<p>In an update to <a href="http://www.sidesofmarch.com/index.php/projects/wilsonorwrapper/">WilsonORWrapper</a>, I added a method which takes two objects of the same type and compares the properties of each, returning a value reflecting the results of the comparison. Any value other than zero would indicate that at least one property on the objects are not equal.</p>
<p>This method may have interest to people who don&#8217;t use WilsonORWrapper, so here&#8217;s an extracted version of the code that does the comparison.</p>
<pre class="brush: c-sharp; ">

using System;
using System.Reflection;

public static class ObjectHelper&lt;t&gt;
{
	public static int Compare(T x, T y)
	{
		Type type = typeof(T);
		PropertyInfo[] properties = type.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public);
		FieldInfo[] fields = type.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public);
		int compareValue = 0;

		foreach (PropertyInfo property in properties)
		{
			IComparable valx = property.GetValue(x, null) as IComparable;
			if (valx == null)
				continue;
			object valy = property.GetValue(y, null);
			compareValue = valx.CompareTo(valy);
			if (compareValue != 0)
				return compareValue;
		}
		foreach (FieldInfo field in fields)
		{
			IComparable valx = field.GetValue(x) as IComparable;
			if (valx == null)
				continue;
			object valy = field.GetValue(y);
			compareValue = valx.CompareTo(valy);
			if (compareValue != 0)
				return compareValue;
		}

		return compareValue;
	}
}
</pre>
<p>With that, if you had a <code>Name</code> class in your code that had two properties, <code>First</code> and <code>Last</code>, you could do something like this:</p>
<pre class="brush: c-sharp; ">

Name n1 = new Name();
n1.First = &quot;Brian&quot;;
n1.Last = &quot;DeMarzo&quot;;

Name n2 = new Name();
n2.First = &quot;Brian&quot;;
n2.Last = &quot;DeMarzo&quot;;

int result1 = ObjectHelper&lt;name&gt;.Compare(n1, n2);
// result1 == 0 because n1 and n2 have equal properties

n1.First = &quot;Alyssa&quot;;
// change the first name, so n1 should no longer equal n2

int result2 = ObjectHelper&lt;/name&gt;&lt;name&gt;.Compare(n1, n2);
// result2 != 0 because n1 and n2 do not have equal properties
</pre>
<p>Code like this came in handy in my project where I use <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.filehelpers.com&sref=rss">FileHelpers</a> (see my <a href="http://www.sidesofmarch.com/index.php/archive/2007/08/03/filehelpers-the-net-way-to-import-text-files/">blog entry from earlier today</a>), where I was able to compare a class loaded from the database (using an O/R mapper) with a class loaded from a text file (using FileHelpers). Since the O/R mapper and FileHelpers used the same class, using this comparison method to determine if the objects were &#8220;equal&#8221; let me determine whether or not the data loaded from the database was different from the data in the text file.</p>
<p>Even though it used reflection, there wasn&#8217;t a huge performance hit, either. Sure beats writing manual comparison methods, which could take a while when you have a few dozen classes!</name></t></p>
<img src="http://www.sidesofmarch.com/?ak_action=api_record_view&id=143&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.sidesofmarch.com/index.php/archive/2007/08/03/use-reflection-to-compare-the-properties-of-two-objects/' addthis:title='Use reflection to compare the properties of two objects ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.sidesofmarch.com/index.php/archive/2007/08/03/use-reflection-to-compare-the-properties-of-two-objects/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Simplify configuration with a generic ConfigurationElementCollection class</title>
		<link>http://www.sidesofmarch.com/index.php/archive/2007/07/27/simplify-configuration-with-a-generic-configurationelementcollection-class/</link>
		<comments>http://www.sidesofmarch.com/index.php/archive/2007/07/27/simplify-configuration-with-a-generic-configurationelementcollection-class/#comments</comments>
		<pubDate>Fri, 27 Jul 2007 17:18:04 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[C#/.Net]]></category>

		<guid isPermaLink="false">http://www.sidesofmarch.com/index.php/archive/2007/07/27/simplify-configuration-with-a-generic-configurationelementcollection-class/</guid>
		<description><![CDATA[<p>I&#8217;ve been using <a href="http://www.haacked.com/">Phil Haack</a>&#8216;s <em><a href="http://www.haacked.com/archive/2007/03/12/custom-configuration-sections-in-3-easy-steps.aspx">Custom Configuration Sections in 3 Easy Steps</a></em> for some time now, and my configuration file management has never been happier&#8230; until I needed&#160;to read a&#160;collection of configuration elements.</p>
<p>Here&#8217;s a sample of the XML I needed to incorporate:</p>


&#60;importer&#62;
	&#60;filespecs&#62;
		&#60;add type=&#34;EmployeeCSV&#34; path=&#34;d:\import\employees.csv&#34; /&#62;
		&#60;add type=&#34;OrderCSV&#34; path=&#34;d:\import\orders_*.csv&#34; /&#62;
	&#60;/filespecs&#62;
&#60;/importer&#62;

<p>The typical solution is to write your own collection class (FilespecConfigurationElementCollection), inheriting from ConfigurationElementCollection, and ensuring your Filespec object inherits from ConfigurationElement. I figured there has to be a better way &#8212; and there is.</p>
<p>I wrote a generic version of ConfigurationElementCollection, which you can use to avoid writing the custom collection class. The code that follows is the generic class, the Filespec object I used, and the property declaration from my ConfigurationSettings class (as described in Phil&#8217;s previously mentioned article).</p>
<p>Note one key part of this implementation: <strong>You must override the ToString() method of your custom ConfigurationElement class to return a unique value</strong>. The generic ConfigurationElementCollection uses the ToString() method to obtain a <span style="color:#777"> . . .<br /><br />&#8594; Read More: <a href="http://www.sidesofmarch.com/index.php/archive/2007/07/27/simplify-configuration-with-a-generic-configurationelementcollection-class/">Simplify configuration with a generic ConfigurationElementCollection class</a></span>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.haacked.com%2F&sref=rss">Phil Haack</a>&#8216;s <em><a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.haacked.com%2Farchive%2F2007%2F03%2F12%2Fcustom-configuration-sections-in-3-easy-steps.aspx&sref=rss">Custom Configuration Sections in 3 Easy Steps</a></em> for some time now, and my configuration file management has never been happier&#8230; until I needed&nbsp;to read a&nbsp;collection of configuration elements.</p>
<p>Here&#8217;s a sample of the XML I needed to incorporate:</p>
<pre class="brush: xml; ">

&lt;importer&gt;
	&lt;filespecs&gt;
		&lt;add type=&quot;EmployeeCSV&quot; path=&quot;d:\import\employees.csv&quot; /&gt;
		&lt;add type=&quot;OrderCSV&quot; path=&quot;d:\import\orders_*.csv&quot; /&gt;
	&lt;/filespecs&gt;
&lt;/importer&gt;
</pre>
<p>The typical solution is to write your own collection class (<code>FilespecConfigurationElementCollection</code>), inheriting from <code>ConfigurationElementCollection</code>, and ensuring your <code>Filespec</code> object inherits from <code>ConfigurationElement</code>. I figured there has to be a better way &#8212; and there is.</p>
<p>I wrote a generic version of <code>ConfigurationElementCollection</code>, which you can use to avoid writing the custom collection class. The code that follows is the generic class, the <code>Filespec</code> object I used, and the property declaration from my <code>ConfigurationSettings</code> class (as described in Phil&#8217;s previously mentioned article).</p>
<p>Note one key part of this implementation: <strong>You must override the <code>ToString()</code> method of your custom <code>ConfigurationElement</code> class to return a unique value</strong>. The generic <code>ConfigurationElementCollection</code> uses the <code>ToString()</code> method to obtain a unique key for each element in the collection.</p>
<pre class="brush: c-sharp; ">

// The ConfigurationElementCollection&lt;t&gt; provides a simple generic implementation of ConfigurationElementCollection.
[ConfigurationCollection(typeof(ConfigurationElement))]
public class ConfigurationElementCollection&lt;/t&gt;&lt;t&gt; : ConfigurationElementCollection where T : ConfigurationElement, new()
{
	protected override ConfigurationElement CreateNewElement()
	{
		return new T();
	}
	protected override object GetElementKey(ConfigurationElement element)
	{
		return ((T)(element)).ToString();
	}
	public T this[int idx]
	{
		get { return (T)BaseGet(idx); }
	}
}
// The Filespec class is an example of a custom configuration element.
// Note that we inherit from ConfigurationElement, and use ConfigurationProperty attributes.
public class Filespec : ConfigurationElement
{
	public Filespec()
	{
	}
	[ConfigurationProperty(&quot;path&quot;, DefaultValue=&quot;&quot;, IsKey=true, IsRequired=true)]
	public string Path
	{
		get { return (string)(base[&quot;path&quot;]); }
		set { base[&quot;path&quot;] = value; }
	}
	[ConfigurationProperty(&quot;type&quot;, IsKey=false, IsRequired = true)]
	public FilespecType Type
	{
		get { return (FilespecType)(base[&quot;type&quot;]); }
		set { base[&quot;type&quot;] = value; }
	}

	public override string ToString()
	{
		return this.Path;
	}
}
// Finally, our ConfigurationSettings class (only part of the class is included).
// Note how we use the generic ConfigurationElementCollection.
public class ConfigurationSettings : ConfigurationSection
{
// ...
	[ConfigurationProperty(&quot;filespecs&quot;, IsRequired=true)]
	public ConfigurationElementCollection&lt;filespec&gt; FileSpecs
	{
		get { return (ConfigurationElementCollection&lt;/filespec&gt;&lt;filespec&gt;)this[&quot;filespecs&quot;]; }
	}
// ...
}
</pre>
<p>If you&#8217;re using a number of <code>ConfigurationElementCollection</code>s, this is a great way to simplify your code.</p>
<p></filespec></t></p>
<img src="http://www.sidesofmarch.com/?ak_action=api_record_view&id=140&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.sidesofmarch.com/index.php/archive/2007/07/27/simplify-configuration-with-a-generic-configurationelementcollection-class/' addthis:title='Simplify configuration with a generic ConfigurationElementCollection class ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.sidesofmarch.com/index.php/archive/2007/07/27/simplify-configuration-with-a-generic-configurationelementcollection-class/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Article to appear in September 2004 DNDJ</title>
		<link>http://www.sidesofmarch.com/index.php/archive/2004/08/03/article-to-appear-in-september-2004-dndj/</link>
		<comments>http://www.sidesofmarch.com/index.php/archive/2004/08/03/article-to-appear-in-september-2004-dndj/#comments</comments>
		<pubDate>Wed, 04 Aug 2004 01:12:11 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[C#/.Net]]></category>

		<guid isPermaLink="false">http://www.sidesofmarch.com/index.php/archive/2004/08/03/article-to-appear-in-september-2004-dndj/</guid>
		<description><![CDATA[<p>I received word that my third article for <a href="http://www.sys-con.com/dotnet/"><em>.Net Developer&#8217;s Journal</em></a> will appear in the September 2004 issue. It&#8217;s the third in the series of articles entitled, &#8220;C# and the .Net Framework: Tying It All Together&#8221;. The <a href="http://www.sys-con.com/story/?storyid=39062">first article</a> was in the January 2004 issue, the <a href="http://www.sys-con.com/story/?storyid=44868">second article</a> was in the May 2004 issue. Why such a delay between articles? They never told me they were publishing the first article, so I didn&#8217;t get the second article to them until after it printed. They never received the third article (which was originally sent in May 2004) and we didn&#8217;t catch up with each other until last month. I&#8217;m working on the fourth article now and hopefully it&#8217;ll be in the October or November issue.</p>
<a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a <span style="color:#777"> . . .<br /><br />&#8594; Read More: <a href="http://www.sidesofmarch.com/index.php/archive/2004/08/03/article-to-appear-in-september-2004-dndj/">Article to appear in September 2004 DNDJ</a></span>]]></description>
			<content:encoded><![CDATA[<p>I received word that my third article for <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.sys-con.com%2Fdotnet%2F&sref=rss"><em>.Net Developer&#8217;s Journal</em></a> will appear in the September 2004 issue. It&#8217;s the third in the series of articles entitled, &#8220;C# and the .Net Framework: Tying It All Together&#8221;. The <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.sys-con.com%2Fstory%2F%3Fstoryid%3D39062&sref=rss">first article</a> was in the January 2004 issue, the <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.sys-con.com%2Fstory%2F%3Fstoryid%3D44868&sref=rss">second article</a> was in the May 2004 issue. Why such a delay between articles? They never told me they were publishing the first article, so I didn&#8217;t get the second article to them until after it printed. They never received the third article (which was originally sent in May 2004) and we didn&#8217;t catch up with each other until last month. I&#8217;m working on the fourth article now and hopefully it&#8217;ll be in the October or November issue.</p>
<img src="http://www.sidesofmarch.com/?ak_action=api_record_view&id=208&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.sidesofmarch.com/index.php/archive/2004/08/03/article-to-appear-in-september-2004-dndj/' addthis:title='Article to appear in September 2004 DNDJ ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.sidesofmarch.com/index.php/archive/2004/08/03/article-to-appear-in-september-2004-dndj/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New C# article published in the .Net Developer&#8217;s Journal</title>
		<link>http://www.sidesofmarch.com/index.php/archive/2004/06/01/new-c-article-published-in-the-net-developers-journal/</link>
		<comments>http://www.sidesofmarch.com/index.php/archive/2004/06/01/new-c-article-published-in-the-net-developers-journal/#comments</comments>
		<pubDate>Tue, 01 Jun 2004 23:07:28 +0000</pubDate>
		<dc:creator>brian</dc:creator>
				<category><![CDATA[C#/.Net]]></category>

		<guid isPermaLink="false">http://www.sidesofmarch.com/index.php/archive/2004/06/01/new-c-article-published-in-the-net-developers-journal/</guid>
		<description><![CDATA[<p>My latest article, <em>C# and the .Net Framework: Tying It All Together (Part 2)</em> has been published in the <a href="http://www.sys-con.com/magazine/?issueid=229&#38;src=false">May 2004 issue</a> of the <a href="http://www.sys-con.com/dotnet/">.Net Developer&#8217;s Journal</a>. Check it out, and let me know what you think!</p>
<a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a <span style="color:#777"> . . .<br /><br />&#8594; Read More: <a href="http://www.sidesofmarch.com/index.php/archive/2004/06/01/new-c-article-published-in-the-net-developers-journal/">New C# article published in the .Net Developer&#8217;s Journal</a></span>]]></description>
			<content:encoded><![CDATA[<p>My latest article, <em>C# and the .Net Framework: Tying It All Together (Part 2)</em> has been published in the <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.sys-con.com%2Fmagazine%2F%3Fissueid%3D229%26amp%3Bsrc%3Dfalse&sref=rss">May 2004 issue</a> of the <a href="http://redirectingat.com?id=17923X751173&xs=1&url=http%3A%2F%2Fwww.sys-con.com%2Fdotnet%2F&sref=rss">.Net Developer&#8217;s Journal</a>. Check it out, and let me know what you think!</p>
<img src="http://www.sidesofmarch.com/?ak_action=api_record_view&id=203&type=feed" alt="" /><div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.sidesofmarch.com/index.php/archive/2004/06/01/new-c-article-published-in-the-net-developers-journal/' addthis:title='New C# article published in the .Net Developer&#8217;s Journal ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.sidesofmarch.com/index.php/archive/2004/06/01/new-c-article-published-in-the-net-developers-journal/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

