<?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>Ivan Zlatev &#187; Testing</title>
	<atom:link href="http://ivanz.com/tag/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://ivanz.com</link>
	<description></description>
	<lastBuildDate>Sat, 05 Jun 2010 16:19:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Three new featurelets for Fluent NHibernate</title>
		<link>http://ivanz.com/2009/12/13/three-new-featurelets-for-fluent-nhibernate/</link>
		<comments>http://ivanz.com/2009/12/13/three-new-featurelets-for-fluent-nhibernate/#comments</comments>
		<pubDate>Sun, 13 Dec 2009 17:58:21 +0000</pubDate>
		<dc:creator>Ivan Zlatev</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Fluent NHibernate]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[ORM]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://ivanz.com/?p=702</guid>
		<description><![CDATA[Lately I have been using Fluent NHibernate and NHibernate respectively for my ORM (Object-Relational Mapping) when working on my top secret pet project. Simply put &#8211; it&#8217;s great. Mapping is done using a fluent C# interface which makes use of lambda expressions for referencing properties instead of strings (check my previous post for more info) [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I have been using <a href="http://fluentnhibernate.org/">Fluent NHibernate</a> and <a href="http://nhforge.org">NHibernate</a> respectively for my ORM (Object-Relational Mapping) when working on my top secret pet project. Simply put &#8211; it&#8217;s great. Mapping is done using a fluent C# interface which makes use of lambda expressions for referencing properties instead of strings (check my <a href="http://ivanz.com/2009/12/04/how-to-avoid-passing-property-names-as-strings-using-c-3-0-expression-trees/">previous post</a> for more info) so the domain model is <strong>safe to refactor</strong> because the mapping will be kept up-to-date too.</p>
<p>I spend some time hacking on Fluent NHibernate to add three little features I needed for my integration unit testing via Fluent NHibernate&#8217;s <em>PersistenceSpecification</em>. They all currently live in my GitHub(also used by Fluent NHibernate) fork <a href="http://github.com/ivanz/fluent-nhibernate">here</a> and have all been submitted upstream. <strong>UPDATE:</strong> The changes are as of now (21.12.2009) officially part of Fluent NHibernate. I can only hope you will find them useful as well.</p>
<h3>PersistenceSpecification: More Informational Error Reporting</h3>
<p>Particularly useful to troubleshoot failing mapping integration unit tests when a property check fails it will print both expected and actual types of the values. Also notice that chained properties (object.PropA.PropB) are now supported as well:</p>
<p style="padding-left: 30px;"><em>For property &#8216;Nutrition.Calories&#8217; expected &#8217;400&#8242; of type &#8216;System.Int32&#8242; but got &#8217;400&#8242; of type &#8216;System.Single&#8217;.</em></p>
<p>instead of:</p>
<p style="padding-left: 30px;"><em>Expected &#8217;400&#8242; but got &#8217;400&#8242; for Property &#8216;NutritionCalories&#8217;</em></p>
<h3>PersistenceSpecification: Entity Components Property Testing</h3>
<p>It is now possible to test entity component properties. For example given this domain model:</p>
<pre class="brush: csharp;">
public class Food
{
    public Food ()
    {
        Nutrition = new NutritionInfo ();
    }

    public virtual NutritionInfo Nutrition { get; protected set }
    public virtual int Id { get; set; }

}

public class NutritionInfo
{
    public virtual float? Fat { get; set; }
}
</pre>
<p>and this mapping:</p>
<pre class="brush: csharp;">
public class FoodMap : ClassMap&lt;Food&gt;
{
    public FoodMap ()
    {
        Id (food =&gt; food.Id)
            .GeneratedBy.Native ();

        Component&lt;NutritionInfo&gt; (food =&gt; food.Nutrition,
            mapping =&gt; {
                mapping.Map (nutrition =&gt; nutrition.Fat)
                    .Nullable ();
            }
        ).Access.BackingField ();
    }
}
</pre>
<p>One can now use this in unit tests:</p>
<pre class="brush: csharp;">
[TestMethod]
public void Food_Mapping ()
{
    new PersistenceSpecification&lt;Food&gt; (Session)
    .CheckProperty (food =&gt; food.Id, 1)
    .CheckProperty (food =&gt; food.Nutrition.Fat, 40f) // &lt;-- This one
    .VerifyTheMappings ();

}
</pre>
<p>Prior to my patch this wasn&#8217;t possible because Fluent Nhibernate <em>PersistenceSpecification</em> did not support chained properties and was trying to set a <em>Fat</em> property on the <em>food</em> object. Note that this is useful only for components and <span style="text-decoration: underline;">not</span> references. For the latter <em>.CheckReference (&#8230;) </em>should be used which will also commit the reference to the Database before doing anything else.</p>
<h3>PersistenceSpecification: IEqualityComparer for Individual Properties</h3>
<p>Given the setup from above it is now possible to set an IEqualityComparer per property, e.g.:</p>
<pre class="brush: csharp;">
[TestMethod]
public void Serving_Mapping ()
{

    new PersistenceSpecification&lt;Food&gt; (Session, new EqualityComparerForAllProperties ())
    .CheckProperty (food =&gt; food.Id, 1)
    .CheckProperty (food =&gt; food.Nutrition.Fat, 40f, FloatEqualityComparer.Instance)
    .VerifyTheMappings ();
}
</pre>
<p>If there is no comparer set for the property it will fall back to using the <em>EqualityComparerForAllProperties</em> and if that is not specified it will just use Object.Equals.</p>
]]></content:encoded>
			<wfw:commentRss>http://ivanz.com/2009/12/13/three-new-featurelets-for-fluent-nhibernate/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
