<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>The FwkLight blog</title>
	<atom:link href="http://fwklight.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://fwklight.wordpress.com</link>
	<description>simple guidelines for productive developers</description>
	<lastBuildDate>Wed, 28 Apr 2010 16:33:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='fwklight.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>The FwkLight blog</title>
		<link>http://fwklight.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://fwklight.wordpress.com/osd.xml" title="The FwkLight blog" />
	<atom:link rel='hub' href='http://fwklight.wordpress.com/?pushpress=hub'/>
		<item>
		<title>The lazy = &#8220;false&#8221; mapping</title>
		<link>http://fwklight.wordpress.com/2010/04/28/the-lazy-false-mapping/</link>
		<comments>http://fwklight.wordpress.com/2010/04/28/the-lazy-false-mapping/#comments</comments>
		<pubDate>Wed, 28 Apr 2010 16:32:00 +0000</pubDate>
		<dc:creator>abutnaru</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://fwklight.wordpress.com/2010/04/28/the-lazy-false-mapping/</guid>
		<description><![CDATA[Using lazy = &#34;false&#34; mappings is a very bad practice, and overrides any possibility to use fetching strategies. Even if at a certain point something seems to be necessary all the time and lazy = &#34;false&#34; might make sense, the application will evolve and a context will appear in which that thing will not be [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fwklight.wordpress.com&amp;blog=13183410&amp;post=154&amp;subd=fwklight&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Using lazy = &quot;false&quot; mappings is a very bad practice, and overrides any possibility to use fetching strategies.</p>
<p>Even if at a certain point something seems to be necessary all the time and lazy = &quot;false&quot; might make sense, the application will evolve and a context will appear in which that thing will not be necessary &#8211; unfortunately you will load it even if you don&#8217;t want it, because of the mapping. The solution if you are in this situation and you want to solve it is to remove the lazy = &quot;false&quot; mapping and create fetching strategies, but the refactoring can be quite painful.</p>
<p>IT IS YOUR RESPONSABILITY NOT TO CREATE lazy=&quot;false&quot; MAPPINGS.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/fwklight.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/fwklight.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/fwklight.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/fwklight.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/fwklight.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/fwklight.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/fwklight.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/fwklight.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/fwklight.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/fwklight.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/fwklight.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/fwklight.wordpress.com/154/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/fwklight.wordpress.com/154/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/fwklight.wordpress.com/154/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fwklight.wordpress.com&amp;blog=13183410&amp;post=154&amp;subd=fwklight&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://fwklight.wordpress.com/2010/04/28/the-lazy-false-mapping/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8671b84cdbc9d549c082568ebc36f50e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">abutnaru</media:title>
		</media:content>
	</item>
		<item>
		<title>Fetching strategies and complex hierarchies</title>
		<link>http://fwklight.wordpress.com/2010/04/28/fetching-strategies-and-complex-hierarchies/</link>
		<comments>http://fwklight.wordpress.com/2010/04/28/fetching-strategies-and-complex-hierarchies/#comments</comments>
		<pubDate>Wed, 28 Apr 2010 16:27:18 +0000</pubDate>
		<dc:creator>abutnaru</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://fwklight.wordpress.com/2010/04/28/fetching-strategies-and-complex-hierarchies/</guid>
		<description><![CDATA[ALL THE EXAMPLES FROM THIS POST ARE EXTRACTED FROM THE FWKLIGHT DEMO. TO FIND OUT HOW TO GET STARTED WITH THE DEMO, READ THIS POST. Fetching strategies can be applied to aggregates with complex hierarchies, not only to the first level inside the aggregate. In order to optimize the queries that will be run, each [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fwklight.wordpress.com&amp;blog=13183410&amp;post=152&amp;subd=fwklight&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>ALL THE EXAMPLES FROM THIS POST ARE EXTRACTED FROM <a href="http://code.google.com/p/fwklight/downloads/list">THE FWKLIGHT DEMO</a>. TO FIND OUT HOW TO GET STARTED WITH THE DEMO, READ <a href="../2010/04/21/getting-started-with-the-fwklight-demo/">THIS</a> POST.</p>
<p>Fetching strategies can be applied to aggregates with complex hierarchies, not only to the first level inside the aggregate.</p>
<p>In order to optimize the queries that will be run, each element (entity or list) from the main hierarchy which has a list specified in its internal hierarchy should have the parent field configured; this is because separated selects are generated for the lists, and joins need to be made to reach the initial filters; if the element does not know its parent, the select will bring much more information than it should.</p>
<p>In the ComplexHierarchy action of the FetchingStrategiesController, we first instruct NHibernate to use a certain fetching strategy when executing the load task:</p>
<div id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:79ed4785-715a-4b54-ae90-aa9edcf47277" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:#ffffff;overflow:auto;"><span style="color:#000000;">Task</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrderSet5LoadTask</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">().ShouldUseFetchingStrategy</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrderSet5FetchingForFetchingStrategiesComplexHierarchy</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">();</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></p>
</div>
<p>and after that we execute the task:</p>
<div id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b773b31d-94ba-4cee-9473-250b665be770" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:#ffffff;overflow:auto;"><span style="color:#000000;">var orderSet </span><span style="color:#000000;">=</span><span style="color:#000000;"> _customerOrderSet5LoadTask.Execute(</span><span style="color:#800080;">1</span><span style="color:#000000;">);</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></p>
</div>
<p>Here is how the fetching strategy looks:</p>
<div id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8769b541-3d71-4b65-916f-79942db1888a" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:#ffffff;overflow:auto;"><span style="color:#0000ff;">public</span><span style="color:#000000;"> </span><span style="color:#0000ff;">class</span><span style="color:#000000;"> CustomerOrderSet5FetchingForFetchingStrategiesComplexHierarchy: IFetchingStrategy</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrderSet5</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000ff;">public</span><span style="color:#000000;"> </span><span style="color:#0000ff;">void</span><span style="color:#000000;"> ApplyStrategyOn(IMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrderSet5</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> repository)
    {
        repository
            .InitFetchingHierarchy()
            .WhichAlsoContainsList(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.CustomerOrders, u </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> u.CustomerOrderSet5)
            .WhichAlsoContainsList(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.OrderDetails)
            .WhichAlsoContainsEntity(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.Product5)
            .BackTo</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrderSet5</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">()
            .WhichAlsoContainsEntity(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.Client5)
            .WhichAlsoContainsList(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.Addresses)
            .BackTo</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrderSet5</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">()
            .WhichAlsoContainsEntity(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.PaymentType5);
    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></p>
</div>
<p>This is all that is required to load the CustomerOrderSet with Id 1 together with its orders, each order with its order details, each order detail with its product, also with the client and its addresses, and also with the payment type.</p>
<p>You can see here:</p>
<div id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:33f865f9-2292-40fc-a648-d6f93f89496c" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:#ffffff;overflow:auto;"><span style="color:#000000;">.WhichAlsoContainsList(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.CustomerOrders, u </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> u.CustomerOrderSet5)</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></p>
</div>
<p>how we tell FwkLight in the second parameter about the parent field for the CustomerOrders list, in order to load the list contained in each CustomerOrder (the list of order details) efficiently.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/fwklight.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/fwklight.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/fwklight.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/fwklight.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/fwklight.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/fwklight.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/fwklight.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/fwklight.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/fwklight.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/fwklight.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/fwklight.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/fwklight.wordpress.com/152/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/fwklight.wordpress.com/152/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/fwklight.wordpress.com/152/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fwklight.wordpress.com&amp;blog=13183410&amp;post=152&amp;subd=fwklight&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://fwklight.wordpress.com/2010/04/28/fetching-strategies-and-complex-hierarchies/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8671b84cdbc9d549c082568ebc36f50e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">abutnaru</media:title>
		</media:content>
	</item>
		<item>
		<title>Fetching strategies</title>
		<link>http://fwklight.wordpress.com/2010/04/28/fetching-strategies/</link>
		<comments>http://fwklight.wordpress.com/2010/04/28/fetching-strategies/#comments</comments>
		<pubDate>Wed, 28 Apr 2010 16:15:19 +0000</pubDate>
		<dc:creator>abutnaru</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://fwklight.wordpress.com/2010/04/28/fetching-strategies/</guid>
		<description><![CDATA[ALL THE EXAMPLES FROM THIS POST ARE EXTRACTED FROM THE FWKLIGHT DEMO. TO FIND OUT HOW TO GET STARTED WITH THE DEMO, READ THIS POST. Fetching strategies represent a way to tell NHibernate what you want to load inside an aggregate (or list of aggregates) before you load it. You should use a fetching strategy [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fwklight.wordpress.com&amp;blog=13183410&amp;post=148&amp;subd=fwklight&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>ALL THE EXAMPLES FROM THIS POST ARE EXTRACTED FROM <a href="http://code.google.com/p/fwklight/downloads/list">THE FWKLIGHT DEMO</a>. TO FIND OUT HOW TO GET STARTED WITH THE DEMO, READ <a href="http://fwklight.wordpress.com/2010/04/21/getting-started-with-the-fwklight-demo/">THIS</a> POST.</p>
<p>Fetching strategies represent a way to tell NHibernate what you want to load inside an aggregate (or list of aggregates) before you load it.</p>
<p>You should use a fetching strategy whenever you need to load an aggregate with entities in it (or lists of entities in it), or lists of aggregates in which every element has entities in it (or lists of entities in it).</p>
<p>IT IS YOUR RESPONSABILITY TO USE FETCHING STRATEGIES WHENEVER YOU NEED TO LOAD AGGREGATES AND THEIR SUBENTITIES, WHEN WORKING WITH FWKLIGHT.</p>
<p>This mechanism provided by FwkLight offers you a way to completely define everything you want to load inside the aggregate, so you should never need to manually load something related to an aggregate after you already loaded the aggregate (doing so would make your implementation more complicated, and lower the stability of the code).</p>
<p>You create fetching strategies by writing classes which implement the IFetchingStrategy&lt;T&gt; interface. The key to configuring a fetching strategy is the IMultiCriteriaFetcher&lt;T&gt; interface, which offers a fluent interface (IListMultiCriteriaFetcher) through which you can instruct NHibernate on how to generate the NHibernate multicriteria for loading the aggregate (aggregates). When executed, the multicriteria will run a batch select on the database and load everything very efficiently, in one roundtrip.</p>
<p>In this example, the fetching strategy instructs FwkLight to load the CustomerOrder with its OrderDetails:</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:edae4e84-326f-41fb-8df2-aeefda58e541" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:#FFFFFF;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> CustomerOrder1Fetching: IFetchingStrategy</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> ApplyStrategyOn(IMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> repository)
    {
        repository
            .InitFetchingHierarchy()
            .WhichAlsoContainsList(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.OrderDetails);
    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>The query has the CustomerOrder1Fetching strategy specified, so each time the query is executed it will load its list of orders according to this strategy:</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c5dcb029-a36d-4a68-bd5b-c49a3f2d642d" style="display:inline;float:none;margin:0;padding:0;">
<pre style="background-color:#FFFFFF;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> AggregateListQuery3 : MultiCriteriaQuery</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">readonly</span><span style="color:#000000;"> CustomerOrder1Fetching _customerOrder1Fetching;
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">decimal</span><span style="color:#000000;"> _minPrice;

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> AggregateListQuery3(CustomerOrder1Fetching customerOrder1Fetching)
    {
        _customerOrder1Fetching </span><span style="color:#000000;">=</span><span style="color:#000000;"> customerOrder1Fetching;
    }

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> Execute(ISession session, </span><span style="color:#0000FF;">decimal</span><span style="color:#000000;"> minPrice)
    {
        _minPrice </span><span style="color:#000000;">=</span><span style="color:#000000;"> minPrice;
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> Load(session);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> ApplyMainFilters(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria)
    {
        criteria.Where(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.OrderPrice).Gt(_minPrice);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> IFetchingStrategy</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> FetchingStrategy
    {
        </span><span style="color:#0000FF;">get</span><span style="color:#000000;">
        {
            </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> _customerOrder1Fetching;
        }
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> FilterOnlyActive(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria) { }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> Sort(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria) { }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/fwklight.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/fwklight.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/fwklight.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/fwklight.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/fwklight.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/fwklight.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/fwklight.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/fwklight.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/fwklight.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/fwklight.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/fwklight.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/fwklight.wordpress.com/148/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/fwklight.wordpress.com/148/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/fwklight.wordpress.com/148/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fwklight.wordpress.com&amp;blog=13183410&amp;post=148&amp;subd=fwklight&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://fwklight.wordpress.com/2010/04/28/fetching-strategies/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8671b84cdbc9d549c082568ebc36f50e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">abutnaru</media:title>
		</media:content>
	</item>
		<item>
		<title>Active items and sorting</title>
		<link>http://fwklight.wordpress.com/2010/04/28/active-items-and-sorting/</link>
		<comments>http://fwklight.wordpress.com/2010/04/28/active-items-and-sorting/#comments</comments>
		<pubDate>Wed, 28 Apr 2010 12:54:43 +0000</pubDate>
		<dc:creator>abutnaru</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://fwklight.wordpress.com/2010/04/28/active-items-and-sorting/</guid>
		<description><![CDATA[ALL THE EXAMPLES FROM THIS POST ARE EXTRACTED FROM THE FWKLIGHT DEMO. TO FIND OUT HOW TO GET STARTED WITH THE DEMO, READ THIS POST. Each query implementation asks you (through abstract methods) how it should bring only the active items and how it should do the sorting. IT IS YOUR RESPONSABILITY TO IMPLEMENT THESE [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fwklight.wordpress.com&amp;blog=13183410&amp;post=140&amp;subd=fwklight&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>ALL THE EXAMPLES FROM THIS POST ARE EXTRACTED FROM <a href="http://code.google.com/p/fwklight/downloads/list">THE FWKLIGHT DEMO</a>. TO FIND OUT HOW TO GET STARTED WITH THE DEMO, READ <a href="http://fwklight.wordpress.com/2010/04/21/getting-started-with-the-fwklight-demo/">THIS</a> POST.</p>
<p>Each query implementation asks you (through abstract methods) how it should bring only the active items and how it should do the sorting.</p>
<p>IT IS YOUR RESPONSABILITY TO IMPLEMENT THESE METHODS WHEN NECCESARY, AND NOT JUST OVERRIDE THEM AND MOVE ON TO SOMETHING ELSE.</p>
<p><strong><u>Sorting</u></strong></p>
<p>Sorting is ussualy not critical, but it&#8217;s quite important from time to time.</p>
<p>Here are 2 queries which implement sorting by overriding the Sort method:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a89c03fe-7ad4-4142-aeeb-0f037f660403" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> AggregateListQuery1: MultiCriteriaQuery</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">decimal</span><span style="color:#000000;"> _minPrice;

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> Execute(ISession session, </span><span style="color:#0000FF;">decimal</span><span style="color:#000000;"> minPrice)
    {
        _minPrice </span><span style="color:#000000;">=</span><span style="color:#000000;"> minPrice;
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> Load(session);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> ApplyMainFilters(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria)
    {
        criteria.Where(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.OrderPrice).Gt(_minPrice);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> FilterOnlyActive(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria){}

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> Sort(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria)
    {
        criteria.OrderAscBy(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.OrderPrice);
    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:2fac4de3-a743-4c6e-8e6b-83d8496f5310" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> BonusHistory2ListQuery : LinqQuery</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">BonusHistory2</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> DateTime _startDate;
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> DateTime</span><span style="color:#000000;">?</span><span style="color:#000000;"> _endDate;
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> Client2 _client;

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">BonusHistory2</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> Execute(ISession session, DateTime startDate, DateTime</span><span style="color:#000000;">?</span><span style="color:#000000;"> endDate, Client2 client)
    {
        _startDate </span><span style="color:#000000;">=</span><span style="color:#000000;"> startDate;
        _endDate </span><span style="color:#000000;">=</span><span style="color:#000000;"> endDate;
        _client </span><span style="color:#000000;">=</span><span style="color:#000000;"> client;

        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> Load(session);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> GetMainQuery()
    {
        _queryable </span><span style="color:#000000;">=</span><span style="color:#000000;"> from q </span><span style="color:#0000FF;">in</span><span style="color:#000000;"> _queryable
                     </span><span style="color:#0000FF;">where</span><span style="color:#000000;"> q.Year </span><span style="color:#000000;">&gt;=</span><span style="color:#000000;"> _startDate.Year
                           </span><span style="color:#000000;">&amp;&amp;</span><span style="color:#000000;"> q.Month </span><span style="color:#000000;">&gt;=</span><span style="color:#000000;"> _startDate.Month
                           </span><span style="color:#000000;">&amp;&amp;</span><span style="color:#000000;"> q.Client2 </span><span style="color:#000000;">==</span><span style="color:#000000;"> _client
                     select q;

        </span><span style="color:#0000FF;">if</span><span style="color:#000000;"> (_endDate </span><span style="color:#000000;">!=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">null</span><span style="color:#000000;">)
        {
            _queryable </span><span style="color:#000000;">=</span><span style="color:#000000;"> from q </span><span style="color:#0000FF;">in</span><span style="color:#000000;"> _queryable
                         </span><span style="color:#0000FF;">where</span><span style="color:#000000;"> q.Year </span><span style="color:#000000;">&lt;=</span><span style="color:#000000;"> _endDate.Value.Year
                               </span><span style="color:#000000;">&amp;&amp;</span><span style="color:#000000;"> q.Month </span><span style="color:#000000;">&lt;=</span><span style="color:#000000;"> _endDate.Value.Month
                         select q;
        }
    }

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> FilterOnlyActive() { }

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> Sort()
    {
        _queryable </span><span style="color:#000000;">=</span><span style="color:#000000;"> (from q </span><span style="color:#0000FF;">in</span><span style="color:#000000;"> _queryable
                      orderby q.Month descending
                                    orderby q.Year descending
                      select q);
    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>&#160;</p>
<p><strong><u>Active items</u></strong></p>
<p>Fetching only active items is usually critical, when it applies.</p>
<p>Imagine that you want to be able to see all products in the admin area (so you can edit them, activate or deactivate them), but you only want your clients to see the active products. If you use the same query to load the products for both admin and client area, you have to tell your query when to include the inactive products and when not, otherwise your clients might have the unpleasant surprise of working with inactive products.</p>
<p>The decision of including the inactive items can be made at the UI / controller layer or at the application layer, depending on the requirements. The difference is that the application task which wraps the query will expect the parameter when executed by the controller, or make the decision itself without expecting a parameter.</p>
<p>In the SortingAndActiveItems action of the QueryController:</p>
<p>- we first load the list of active products by making the decision in the controller (the application task expects the parameter for including the inactive items or not)</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3e3c577a-fbc8-4a96-b5f8-307fc8319f1e" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">var productList1 </span><span style="color:#000000;">=</span><span style="color:#000000;"> _product2ListTask.Execute(</span><span style="color:#0000FF;">false</span><span style="color:#000000;">);</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Product2ListTask is a wrapper for Product2Query, which also passes the decision from the controller to the query:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:7374c7b4-a1af-470e-a5cb-dcceca97be4d" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> Product2ListTask: BaseListQueryTask</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">Product2</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">readonly</span><span style="color:#000000;"> Product2Query _product2Query;
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">bool</span><span style="color:#000000;"> _includeInactive;

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> Product2ListTask(INHUnitOfWorkProvider uowProvider, Product2Query product2Query) : </span><span style="color:#0000FF;">base</span><span style="color:#000000;">(uowProvider)
    {
        _product2Query </span><span style="color:#000000;">=</span><span style="color:#000000;"> product2Query;
    }

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">Product2</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> Execute(</span><span style="color:#0000FF;">bool</span><span style="color:#000000;"> includeInactive)
    {
        _includeInactive </span><span style="color:#000000;">=</span><span style="color:#000000;"> includeInactive;
        ExecuteInternal();
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> Entity;
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">Product2</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> ExecuteQuery()
    {
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> _product2Query.Execute(UOW.Session, _includeInactive);
    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>The query implements a method to bring only the active items by overriding the FilterOnlyActive method, and offers 2 Execute entry points (one which expects the decision from outside, and one which defaults to excluding the inactive items):</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0ba46cd3-cea9-4113-bd17-17cead6d1493" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> Product2Query: MultiCriteriaQuery</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">Product2</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">Product2</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> Execute(ISession session, </span><span style="color:#0000FF;">bool</span><span style="color:#000000;"> includeInactive)
    {
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> Load(session, includeInactive);
    }

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">Product2</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> Execute(ISession session)
    {
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> Execute(session, </span><span style="color:#0000FF;">false</span><span style="color:#000000;">);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> ApplyMainFilters(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">Product2</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria)
    {

    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> FilterOnlyActive(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">Product2</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria)
    {
        criteria.Where(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.IsActive).Eq(</span><span style="color:#0000FF;">true</span><span style="color:#000000;">);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> Sort(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">Product2</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria)
    {
        criteria.OrderAscBy(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.ProductName);
    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>Here is how the entity looks like:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ee0d70af-6c2b-47a8-98b2-65d6af21d100" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> Product2
{
    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">virtual</span><span style="color:#000000;"> </span><span style="color:#0000FF;">int</span><span style="color:#000000;"> Id { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">virtual</span><span style="color:#000000;"> </span><span style="color:#0000FF;">string</span><span style="color:#000000;"> ProductName { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">virtual</span><span style="color:#000000;"> </span><span style="color:#0000FF;">bool</span><span style="color:#000000;"> IsActive { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>- in the second example, we load the same list of active products by making the decision in the application layer (the application task always excludes inactive items when executing the query):</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d5e8a068-cf71-4557-8e16-4b3184b56625" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">var productList2 </span><span style="color:#000000;">=</span><span style="color:#000000;"> _clientProductListTask.Execute();</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:751e1756-b695-4fc1-9458-6fcb514430f9" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> ClientProductListTask: BaseListQueryTask</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">Product2</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">readonly</span><span style="color:#000000;"> Product2Query _product2Query;

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> ClientProductListTask(INHUnitOfWorkProvider uowProvider, Product2Query product2Query) : </span><span style="color:#0000FF;">base</span><span style="color:#000000;">(uowProvider)
    {
        _product2Query </span><span style="color:#000000;">=</span><span style="color:#000000;"> product2Query;
    }

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">Product2</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> Execute()
    {
        ExecuteInternal();
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> Entity;
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">Product2</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> ExecuteQuery()
    {
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> _product2Query.Execute(UOW.Session, </span><span style="color:#0000FF;">false</span><span style="color:#000000;">);
    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>&#160;</p>
<p>This also applies to situations when you have a certain state which is equivalent to the item being disabled (for example, a Cancelled state), and the same query needs to load the list including / excluding items in that state.</p>
<p>- in the third example, we load only orders which are not cancelled:</p>
<p><div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:bce8ee93-758a-4878-9e5a-1ad9027805fb" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">var ordersNotCancelled </span><span style="color:#000000;">=</span><span style="color:#000000;"> _customerOrder4ListTask.Execute(</span><span style="color:#0000FF;">false</span><span style="color:#000000;">);</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>The task is simply a wrapper for the query:</p>
<p><div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:675ec1f2-c5a4-4cd6-958a-f399fe50d3ab" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> CustomerOrder4ListTask: BaseListQueryTask</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder4</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">readonly</span><span style="color:#000000;"> CustomerOrder4Query _customerOrder4Query;
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">bool</span><span style="color:#000000;"> _includeCancelled;

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> CustomerOrder4ListTask(INHUnitOfWorkProvider uowProvider, CustomerOrder4Query customerOrder4Query) : </span><span style="color:#0000FF;">base</span><span style="color:#000000;">(uowProvider)
    {
        _customerOrder4Query </span><span style="color:#000000;">=</span><span style="color:#000000;"> customerOrder4Query;
    }

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder4</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> Execute(</span><span style="color:#0000FF;">bool</span><span style="color:#000000;"> includeCancelled)
    {
        _includeCancelled </span><span style="color:#000000;">=</span><span style="color:#000000;"> includeCancelled;
        ExecuteInternal();
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> Entity;
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder4</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> ExecuteQuery()
    {
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> _customerOrder4Query.Execute(UOW.Session, _includeCancelled);
    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>The query implements a method to filter the Cancelled orders:</p>
<p><div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3d014fa8-a82e-4544-850a-8cf53af43606" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> CustomerOrder4Query: MultiCriteriaQuery</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder4</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder4</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> Execute(ISession session, </span><span style="color:#0000FF;">bool</span><span style="color:#000000;"> includeCancelled)
    {
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> Load(session, includeCancelled);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> ApplyMainFilters(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder4</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria)
    {

    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> FilterOnlyActive(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder4</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria)
    {
        criteria.Where(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.State).NotEq(CustomerOrderState.Cancelled);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> Sort(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder4</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria)
    {
        criteria.OrderDescBy(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.OrderPrice);
    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>And here are the entity and states enum:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d929d225-3eb6-4b7e-98ca-0e81530b5b2c" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">enum</span><span style="color:#000000;"> CustomerOrderState { InWork </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#800080;">1</span><span style="color:#000000;">, Sent </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#800080;">2</span><span style="color:#000000;">, Cancelled </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#800080;">3</span><span style="color:#000000;">, Accepted </span><span style="color:#000000;">=</span><span style="color:#000000;"> </span><span style="color:#800080;">4</span><span style="color:#000000;">}

</span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> CustomerOrder4
{
    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">virtual</span><span style="color:#000000;"> </span><span style="color:#0000FF;">int</span><span style="color:#000000;"> Id { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">virtual</span><span style="color:#000000;"> </span><span style="color:#0000FF;">decimal</span><span style="color:#000000;"> OrderPrice { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">virtual</span><span style="color:#000000;"> CustomerOrderState State { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/fwklight.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/fwklight.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/fwklight.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/fwklight.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/fwklight.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/fwklight.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/fwklight.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/fwklight.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/fwklight.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/fwklight.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/fwklight.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/fwklight.wordpress.com/140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/fwklight.wordpress.com/140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/fwklight.wordpress.com/140/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fwklight.wordpress.com&amp;blog=13183410&amp;post=140&amp;subd=fwklight&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://fwklight.wordpress.com/2010/04/28/active-items-and-sorting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8671b84cdbc9d549c082568ebc36f50e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">abutnaru</media:title>
		</media:content>
	</item>
		<item>
		<title>Query types</title>
		<link>http://fwklight.wordpress.com/2010/04/27/query-types/</link>
		<comments>http://fwklight.wordpress.com/2010/04/27/query-types/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 13:52:57 +0000</pubDate>
		<dc:creator>abutnaru</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://fwklight.wordpress.com/2010/04/27/query-types/</guid>
		<description><![CDATA[ALL THE EXAMPLES FROM THIS POST ARE EXTRACTED FROM THE FWKLIGHT DEMO. TO FIND OUT HOW TO GET STARTED WITH THE DEMO, READ THIS POST. There are 5 implementations of queries in FwkLight, and each time you should choose the appropriate one based on what you need by following this rule: 1. if you are [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fwklight.wordpress.com&amp;blog=13183410&amp;post=134&amp;subd=fwklight&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>ALL THE EXAMPLES FROM THIS POST ARE EXTRACTED FROM <a href="http://code.google.com/p/fwklight/downloads/list">THE FWKLIGHT DEMO</a>. TO FIND OUT HOW TO GET STARTED WITH THE DEMO, READ <a href="http://fwklight.wordpress.com/2010/04/21/getting-started-with-the-fwklight-demo/">THIS</a> POST.</p>
<p>There are 5 implementations of queries in FwkLight, and each time you should choose the appropriate one based on what you need by following this rule:</p>
<p>1. if you are using a stored procedure (you should do that very rarely), use a NamedQuery    <br />2. if you are not using a stored procedure, try to use a Linq query     <br />3. if 2 is not possible, try to use a MultiCriteria query     <br />4. is 3 is not possible, try to use a Criteria query     <br />5. if 4 is not possible, use a HQL query</p>
<p>&#160;</p>
<p><strong><u>HqlQuery</u></strong></p>
<p>This is a query which can be configured with NHibernate&#8217;s HQL API. </p>
<p>It is quite flexible, but you should use it only if none of the others work because it has 2 big drawbacks: you will use strings to build the queries, and you don&#8217;t have good support for loading complex hierarchies.</p>
<p>When deciding to use it, you should also consider that this type of query cannot load complex aggregates efficiently and cannot use fetching strategies. If you need to load a list of aggregates in which every aggregate contains a complex hierarchy, your only good choice is MultiCriteriaQuery.</p>
<p>Example:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f2218a08-7a34-4c12-beda-c324045b7240" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> CalculatedPrimitiveQuery1 : HqlQuery</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">Client2, </span><span style="color:#0000FF;">int</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> Client2 _client;

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">decimal</span><span style="color:#000000;"> Execute(ISession session, Client2 client)
    {
        _client </span><span style="color:#000000;">=</span><span style="color:#000000;"> client;
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> LoadUnique(session);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">string</span><span style="color:#000000;"> HQLForActiveItems
    {
        </span><span style="color:#0000FF;">get</span><span style="color:#000000;">
        {
            </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> </span><span style="color:#800000;">@"</span><span style="color:#800000;">select max(dl.DiscountPercentage) from DiscountLevel2 as dl
              where dl.DiscountPercentage &lt;=
                 (select max(dl1.DiscountPercentage)
                        from DiscountLevel2 as dl1
                          where dl1.ClientTypeWithLimit = :ClientType
                          or
                          not exists (select dl1.DiscountPercentage from DiscountLevel2 as dl1
                            where dl1.ClientTypeWithLimit = :ClientType)
                )</span><span style="color:#800000;">"</span><span style="color:#000000;">;
        }
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">string</span><span style="color:#000000;"> HQLForAllItems
    {
        </span><span style="color:#0000FF;">get</span><span style="color:#000000;"> { </span><span style="color:#0000FF;">throw</span><span style="color:#000000;"> </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> NotImplementedException(); }
    }

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> SetParameters(IQuery query)
    {
        query.SetInt32(</span><span style="color:#800000;">"</span><span style="color:#800000;">ClientType</span><span style="color:#800000;">"</span><span style="color:#000000;">, (</span><span style="color:#0000FF;">int</span><span style="color:#000000;">)_client.ClientType);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">string</span><span style="color:#000000;"> Sorting
    {
        </span><span style="color:#0000FF;">get</span><span style="color:#000000;">
        {
            </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> </span><span style="color:#800000;">""</span><span style="color:#000000;">;
        }
    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>&#160;</p>
<p><strong><u>CriteriaQuery</u></strong></p>
<p>This is a query which can be configured with NHibernate&#8217;s Criteria API. </p>
<p>It still uses some strings, but you will hardcode less information than with a HQL query.</p>
<p>When deciding to use it, you should also consider that this type of query cannot load complex aggregates efficiently and cannot use fetching strategies. If you need to load a list of aggregates in which every aggregate contains a complex hierarchy, your only good choice is MultiCriteriaQuery</p>
<p>This type of query is the best choice for paged UI lists.</p>
<p>Example:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3fa69d4a-7c25-4883-a7aa-986ad38f3483" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> CalculatedPrimitiveQuery2 : CriteriaQuery</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder2, </span><span style="color:#0000FF;">int</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
  </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> Client2 _client;

  </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> GetMainCriteria(ICriteria criteria)
  {
    criteria.Add(Restrictions.Eq(</span><span style="color:#800000;">"</span><span style="color:#800000;">IsLaunched</span><span style="color:#800000;">"</span><span style="color:#000000;">, </span><span style="color:#0000FF;">true</span><span style="color:#000000;">))
      .Add(Restrictions.Eq(</span><span style="color:#800000;">"</span><span style="color:#800000;">Client2</span><span style="color:#800000;">"</span><span style="color:#000000;">, _client))
      .SetProjection(Projections.Id());
  }

  </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">bool</span><span style="color:#000000;"> Execute(ISession session, Client2 client)
  {
    _client </span><span style="color:#000000;">=</span><span style="color:#000000;"> client;
    </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> Load(session).Count </span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> </span><span style="color:#800080;">0</span><span style="color:#000000;">;
  }

  </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> FilterOnlyActive(ICriteria criteria){}

  </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> Sort(ICriteria criteria){}
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>&#160;</p>
<p><strong><u>MultiCriteriaQuery</u></strong></p>
<p>This is FwkLight&#8217;s implementation for a query, which uses a fluent interface (IListMultiCriteriaFetcher) to generate an NHibernate multicriteria.</p>
<p>To configure the fetching strategy of the query, you override the FetchingStrategy property.</p>
</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:9cbcb56d-3372-4ec4-bc16-235207f407b3" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> AggregateListQuery3 : MultiCriteriaQuery</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">readonly</span><span style="color:#000000;"> CustomerOrder1Fetching _customerOrder1Fetching;
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">decimal</span><span style="color:#000000;"> _minPrice;

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> AggregateListQuery3(CustomerOrder1Fetching customerOrder1Fetching)
    {
        _customerOrder1Fetching </span><span style="color:#000000;">=</span><span style="color:#000000;"> customerOrder1Fetching;
    }

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> Execute(ISession session, </span><span style="color:#0000FF;">decimal</span><span style="color:#000000;"> minPrice)
    {
        _minPrice </span><span style="color:#000000;">=</span><span style="color:#000000;"> minPrice;
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> Load(session);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> ApplyMainFilters(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria)
    {
        criteria.Where(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.OrderPrice).Gt(_minPrice);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> IFetchingStrategy</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> FetchingStrategy
    {
        </span><span style="color:#0000FF;">get</span><span style="color:#000000;">
        {
            </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> _customerOrder1Fetching;
        }
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> FilterOnlyActive(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria) { }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> Sort(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria) { }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>Here is how the fetching strategy looks like:</p>
</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:7e5c6a09-bc3a-406b-854f-ebbd993581d5" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> CustomerOrder1Fetching: IFetchingStrategy</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> ApplyStrategyOn(IMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> repository)
    {
        repository
            .InitFetchingHierarchy()
            .WhichAlsoContainsList(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.OrderDetails);
    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>&#160;</p>
<p><strong><u>LinqQuery</u></strong></p>
<p>This is a query which can be configured through Linq means, and uses NHibernate2Linq. This is the best way to define queries, will be upgraded and maintained by the community, but it is very new, not yet mature, and has many limitations. You can use it only in very simple scenarios.</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:10d8ee59-1181-45f3-9c4f-f607be64ff54" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> BonusHistory2ListQuery : LinqQuery</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">BonusHistory2</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> DateTime _startDate;
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> DateTime</span><span style="color:#000000;">?</span><span style="color:#000000;"> _endDate;
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> Client2 _client;

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">BonusHistory2</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> Execute(ISession session, DateTime startDate, DateTime</span><span style="color:#000000;">?</span><span style="color:#000000;"> endDate, Client2 client)
    {
        _startDate </span><span style="color:#000000;">=</span><span style="color:#000000;"> startDate;
        _endDate </span><span style="color:#000000;">=</span><span style="color:#000000;"> endDate;
        _client </span><span style="color:#000000;">=</span><span style="color:#000000;"> client;

        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> Load(session);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> GetMainQuery()
    {
        _queryable </span><span style="color:#000000;">=</span><span style="color:#000000;"> from q </span><span style="color:#0000FF;">in</span><span style="color:#000000;"> _queryable
                     </span><span style="color:#0000FF;">where</span><span style="color:#000000;"> q.Year </span><span style="color:#000000;">&gt;=</span><span style="color:#000000;"> _startDate.Year
                           </span><span style="color:#000000;">&amp;&amp;</span><span style="color:#000000;"> q.Month </span><span style="color:#000000;">&gt;=</span><span style="color:#000000;"> _startDate.Month
                           </span><span style="color:#000000;">&amp;&amp;</span><span style="color:#000000;"> q.Client2 </span><span style="color:#000000;">==</span><span style="color:#000000;"> _client
                     select q;

        </span><span style="color:#0000FF;">if</span><span style="color:#000000;"> (_endDate </span><span style="color:#000000;">!=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">null</span><span style="color:#000000;">)
        {
            _queryable </span><span style="color:#000000;">=</span><span style="color:#000000;"> from q </span><span style="color:#0000FF;">in</span><span style="color:#000000;"> _queryable
                         </span><span style="color:#0000FF;">where</span><span style="color:#000000;"> q.Year </span><span style="color:#000000;">&lt;=</span><span style="color:#000000;"> _endDate.Value.Year
                               </span><span style="color:#000000;">&amp;&amp;</span><span style="color:#000000;"> q.Month </span><span style="color:#000000;">&lt;=</span><span style="color:#000000;"> _endDate.Value.Month
                         select q;
        }
    }

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> FilterOnlyActive() { }

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> Sort()
    {
        _queryable </span><span style="color:#000000;">=</span><span style="color:#000000;"> (from q </span><span style="color:#0000FF;">in</span><span style="color:#000000;"> _queryable
                      orderby q.Month descending
                                    orderby q.Year descending
                      select q);
    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>&#160;</p>
<p><u>AllItemsQuery</u></p>
<p>This is a Linq query which loads all the entities of a certain type. It is a shortcut to load all the information from small database tables.</p>
<p>&#160;</p>
<p><strong><u>NamedQuery</u></strong></p>
<p>This is a query which works with a stored procedure.</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:08e52320-1f9d-4940-9274-2a07611bb6f1" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> BonusStatisticsQuery : NamedQuery</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">BonusStatistic</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> Client2 _client;

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> BonusStatistic Execute(ISession session, Client2 client)
    {
        _client </span><span style="color:#000000;">=</span><span style="color:#000000;"> client;
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> UniqueResult(session);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> SetParameters(IQuery query)
    {
        query.SetParameter(</span><span style="color:#800000;">"</span><span style="color:#800000;">clientId</span><span style="color:#800000;">"</span><span style="color:#000000;">, _client.Id);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">string</span><span style="color:#000000;"> QueryName
    {
        </span><span style="color:#0000FF;">get</span><span style="color:#000000;">
        {
            </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> </span><span style="color:#800000;">"</span><span style="color:#800000;">ClientBonusStatistics</span><span style="color:#800000;">"</span><span style="color:#000000;">;
        }
    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>The BonusStatistic entity looks like this:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e0917fdf-d93e-4252-a670-f5564089969b" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> BonusStatistic
{
    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">virtual</span><span style="color:#000000;"> </span><span style="color:#0000FF;">int</span><span style="color:#000000;"> Id { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">virtual</span><span style="color:#000000;"> </span><span style="color:#0000FF;">decimal</span><span style="color:#000000;"> TotalBonusGained { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>The entity mapping also maps the stored procedure:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d2defcb2-5919-4cd6-ac63-299f6c5e7e97" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">&lt;?</span><span style="color:#FF00FF;">xml version="1.0" encoding="utf-8" </span><span style="color:#0000FF;">?&gt;</span><span style="color:#000000;">

</span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">hibernate-mapping </span><span style="color:#FF0000;">xmlns</span><span style="color:#0000FF;">="urn:nhibernate-mapping-2.2"</span><span style="color:#FF0000;"> schema</span><span style="color:#0000FF;">="dbo"</span><span style="color:#FF0000;"> assembly</span><span style="color:#0000FF;">="FwkLightDemo.Domain"</span><span style="color:#FF0000;">  namespace</span><span style="color:#0000FF;">="FwkLightDemo.Domain.Entities"</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
  </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">class </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">="BonusStatistic"</span><span style="color:#FF0000;"> table</span><span style="color:#0000FF;">="BonusStatistic"</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">id </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">="Id"</span><span style="color:#FF0000;"> column</span><span style="color:#0000FF;">="Id"</span><span style="color:#FF0000;"> type</span><span style="color:#0000FF;">="Int32"</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
            </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">generator </span><span style="color:#FF0000;">class</span><span style="color:#0000FF;">="assigned"</span><span style="color:#0000FF;">&gt;&lt;/</span><span style="color:#800000;">generator</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
        </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">id</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">

        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">property </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">="TotalBonusGained"</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">

  </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">class</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">

    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">sql-query </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">="ClientBonusStatistics"</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">return </span><span style="color:#FF0000;">class</span><span style="color:#0000FF;">="BonusStatistic"</span><span style="color:#FF0000;"> </span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
        exec dbo.[FwkLightDemo_ClientBonusStatistics] :clientId
    </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">sql-query</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">

</span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">hibernate-mapping</span><span style="color:#0000FF;">&gt;</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>And the stored procedure expects the clientId parameter:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c6f7c528-f1c7-4e3d-88d5-8b743ef63bb8" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">CREATE</span><span style="color:#000000;"> </span><span style="color:#0000FF;">PROCEDURE</span><span style="color:#000000;"> </span><span style="color:#FF0000;">[</span><span style="color:#FF0000;">dbo</span><span style="color:#FF0000;">]</span><span style="color:#000000;">.</span><span style="color:#FF0000;">[</span><span style="color:#FF0000;">FwkLightDemo_ClientBonusStatistics</span><span style="color:#FF0000;">]</span><span style="color:#000000;"> 

</span><span style="color:#008000;">@clientId</span><span style="color:#000000;"> </span><span style="color:#0000FF;">int</span><span style="color:#000000;">

</span><span style="color:#0000FF;">AS</span><span style="color:#000000;">
</span><span style="color:#0000FF;">BEGIN</span><span style="color:#000000;">

    </span><span style="color:#0000FF;">select</span><span style="color:#000000;">
    </span><span style="color:#FF00FF;">sum</span><span style="color:#000000;">(BonusGained) </span><span style="color:#0000FF;">as</span><span style="color:#000000;"> TotalBonusGained,
    </span><span style="color:#FF00FF;">convert</span><span style="color:#000000;">(</span><span style="color:#0000FF;">int</span><span style="color:#000000;">, </span><span style="color:#FF00FF;">rand</span><span style="color:#000000;">()</span><span style="color:#808080;">*</span><span style="color:#000000;"> </span><span style="color:#800000;font-weight:bold;">100000</span><span style="color:#000000;">) </span><span style="color:#0000FF;">as</span><span style="color:#000000;"> Id
    </span><span style="color:#0000FF;">from</span><span style="color:#000000;"> BonusHistory2
    </span><span style="color:#0000FF;">where</span><span style="color:#000000;"> Client2Id </span><span style="color:#808080;">=</span><span style="color:#000000;"> </span><span style="color:#008000;">@clientId</span><span style="color:#000000;">

</span><span style="color:#0000FF;">END</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/fwklight.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/fwklight.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/fwklight.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/fwklight.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/fwklight.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/fwklight.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/fwklight.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/fwklight.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/fwklight.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/fwklight.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/fwklight.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/fwklight.wordpress.com/134/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/fwklight.wordpress.com/134/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/fwklight.wordpress.com/134/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fwklight.wordpress.com&amp;blog=13183410&amp;post=134&amp;subd=fwklight&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://fwklight.wordpress.com/2010/04/27/query-types/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8671b84cdbc9d549c082568ebc36f50e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">abutnaru</media:title>
		</media:content>
	</item>
		<item>
		<title>Executing and using queries</title>
		<link>http://fwklight.wordpress.com/2010/04/27/executing-and-using-queries/</link>
		<comments>http://fwklight.wordpress.com/2010/04/27/executing-and-using-queries/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 13:19:12 +0000</pubDate>
		<dc:creator>abutnaru</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://fwklight.wordpress.com/2010/04/27/executing-and-using-queries/</guid>
		<description><![CDATA[ALL THE EXAMPLES FROM THIS POST ARE EXTRACTED FROM THE FWKLIGHT DEMO. TO FIND OUT HOW TO GET STARTED WITH THE DEMO, READ THIS POST. You should Execute queries only in Application Tasks, and only in these 2 places: in the LoadAdditionalInitialData method of an Application Task Example: public class CalculatedPrimitiveTask2 : BaseApplicationTask&#60;decimal&#62; { private [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fwklight.wordpress.com&amp;blog=13183410&amp;post=132&amp;subd=fwklight&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>ALL THE EXAMPLES FROM THIS POST ARE EXTRACTED FROM <a href="http://code.google.com/p/fwklight/downloads/list">THE FWKLIGHT DEMO</a>. TO FIND OUT HOW TO GET STARTED WITH THE DEMO, READ <a href="http://fwklight.wordpress.com/2010/04/21/getting-started-with-the-fwklight-demo/">THIS</a> POST.</p>
<p>You should Execute queries only in Application Tasks, and only in these 2 places:</p>
<ul>
<li>in the LoadAdditionalInitialData method of an Application Task </li>
</ul>
<p>Example:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:9b0058f8-03a7-4de3-ace7-e198378cab3f" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> CalculatedPrimitiveTask2 : BaseApplicationTask</span><span style="color:#000000;">&lt;</span><span style="color:#0000FF;">decimal</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
  </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">readonly</span><span style="color:#000000;"> DiscountLevel2ListTask _discountLevel2ListTask;
  </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">readonly</span><span style="color:#000000;"> MaxDiscountPercentage _maxDiscountPercentage;
  </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> Client2 _client;
  </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">DiscountLevel2</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> _discountLevel2List;

  </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> CalculatedPrimitiveTask2(INHUnitOfWorkProvider uowProvider, DiscountLevel2ListTask discountLevel2ListTask, MaxDiscountPercentage maxDiscountPercentage) : </span><span style="color:#0000FF;">base</span><span style="color:#000000;">(uowProvider)
  {
    _discountLevel2ListTask </span><span style="color:#000000;">=</span><span style="color:#000000;"> discountLevel2ListTask;
    _maxDiscountPercentage </span><span style="color:#000000;">=</span><span style="color:#000000;"> maxDiscountPercentage;
  }

  </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">decimal</span><span style="color:#000000;"> Execute(Client2 client)
  {
    _client </span><span style="color:#000000;">=</span><span style="color:#000000;"> client;
    ExecuteInternal();
    </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> Entity;
  }

  </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> LoadAdditionalInitialData()
  {
    _discountLevel2List </span><span style="color:#000000;">=</span><span style="color:#000000;"> _discountLevel2ListTask.Execute();
  }

  </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> ExecuteBusinessLogic()
  {
    Entity </span><span style="color:#000000;">=</span><span style="color:#000000;"> _client.CalculateThe(_maxDiscountPercentage, _discountLevel2List);
  }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>DiscountLevel2ListTask is an AllItemsTask, which is a wrapper for an AllItemsQuery (which loads all the entities of a certain type):</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:239280d6-4b90-4308-8610-62d0055d5bbd" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> DiscountLevel2ListTask: AllItemsTask</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">DiscountLevel2</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
  </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> DiscountLevel2ListTask(INHUnitOfWorkProvider uowProvider, AllItemsQuery</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">DiscountLevel2</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> allItemsQuery) : </span><span style="color:#0000FF;">base</span><span style="color:#000000;">(uowProvider, allItemsQuery)
  {
  }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<ul>
<li>in a Query Task </li>
</ul>
</p>
<p>Example:</p>
<p>The AggregateListQuery1 is executed in the ExecuteQuery method of the QueryTask:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:1058dba3-fdbc-467a-a4fa-a34177d792d3" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> AggregateListTask1 : BaseListQueryTask</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">readonly</span><span style="color:#000000;"> AggregateListQuery1 _aggregateListQuery1;
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">decimal</span><span style="color:#000000;"> _minPrice;

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> AggregateListTask1(INHUnitOfWorkProvider uowProvider, AggregateListQuery1 aggregateListQuery1) : </span><span style="color:#0000FF;">base</span><span style="color:#000000;">(uowProvider)
    {
        _aggregateListQuery1 </span><span style="color:#000000;">=</span><span style="color:#000000;"> aggregateListQuery1;
    }

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> Execute(</span><span style="color:#0000FF;">decimal</span><span style="color:#000000;"> minPrice)
    {
        _minPrice </span><span style="color:#000000;">=</span><span style="color:#000000;"> minPrice;
        ExecuteInternal();
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> Entity;
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> ExecuteQuery()
    {
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> _aggregateListQuery1.Execute(UOW.Session, _minPrice);
    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:134ece2e-15eb-4d43-8eb3-f9281b212fa8" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> AggregateListQuery1: MultiCriteriaQuery</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">decimal</span><span style="color:#000000;"> _minPrice;

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> Execute(ISession session, </span><span style="color:#0000FF;">decimal</span><span style="color:#000000;"> minPrice)
    {
        _minPrice </span><span style="color:#000000;">=</span><span style="color:#000000;"> minPrice;
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> Load(session);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> ApplyMainFilters(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria)
    {
        criteria.Where(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.OrderPrice).Gt(_minPrice);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> FilterOnlyActive(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria){}

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> Sort(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria)
    {
        criteria.OrderAscBy(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.OrderPrice);
    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>IT IS YOUR RESPONSABILITY TO NOT EXECUTE QUERIES ANYWHERE ELSE WHEN WORKING WITH FWKLIGHT.</p>
<p>The Query uses an Execute convention, and one of the parameters of the Execute method should be the current NHibernate Session (the Session property of the current UnitOfWork, which is offered by the Application Task through the UOW property – example above).</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/fwklight.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/fwklight.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/fwklight.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/fwklight.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/fwklight.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/fwklight.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/fwklight.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/fwklight.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/fwklight.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/fwklight.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/fwklight.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/fwklight.wordpress.com/132/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/fwklight.wordpress.com/132/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/fwklight.wordpress.com/132/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fwklight.wordpress.com&amp;blog=13183410&amp;post=132&amp;subd=fwklight&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://fwklight.wordpress.com/2010/04/27/executing-and-using-queries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8671b84cdbc9d549c082568ebc36f50e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">abutnaru</media:title>
		</media:content>
	</item>
		<item>
		<title>Queries and calculated primitives</title>
		<link>http://fwklight.wordpress.com/2010/04/27/queries-and-calculated-primitives/</link>
		<comments>http://fwklight.wordpress.com/2010/04/27/queries-and-calculated-primitives/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 10:10:53 +0000</pubDate>
		<dc:creator>abutnaru</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://fwklight.wordpress.com/2010/04/27/queries-and-calculated-primitives/</guid>
		<description><![CDATA[ALL THE EXAMPLES FROM THIS POST ARE EXTRACTED FROM THE FWKLIGHT DEMO. TO FIND OUT HOW TO GET STARTED WITH THE DEMO, READ THIS POST. By primitive we are referring to a boolean, an int, a decimal, etc. You sometimes need to apply a formula to calculate a primitive, and you have the option of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fwklight.wordpress.com&amp;blog=13183410&amp;post=129&amp;subd=fwklight&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>ALL THE EXAMPLES FROM THIS POST ARE EXTRACTED FROM <a href="http://code.google.com/p/fwklight/downloads/list">THE FWKLIGHT DEMO</a>. TO FIND OUT HOW TO GET STARTED WITH THE DEMO, READ <a href="http://fwklight.wordpress.com/2010/04/21/getting-started-with-the-fwklight-demo/">THIS</a> POST.</p>
<p>By primitive we are referring to a boolean, an int, a decimal, etc. You sometimes need to apply a formula to calculate a primitive, and you have the option of doing it in the domain or in the database.</p>
<p>To let the database apply the formula and return the result, you need to create a query which makes the necessary database call. Here is an example of a query which calculates the maximum discount percentage that a client can receive, based on a list of discount levels and the client type. The query uses HQL (Hibernate Query Language) to apply the formula in the HQLForActiveItems method:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:cc5334b0-c000-47d2-b684-edc2b85ffcc6" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> CalculatedPrimitiveQuery1 : HqlQuery</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">Client2, </span><span style="color:#0000FF;">int</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> Client2 _client;

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">decimal</span><span style="color:#000000;"> Execute(ISession session, Client2 client)
    {
        _client </span><span style="color:#000000;">=</span><span style="color:#000000;"> client;
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> LoadUnique(session);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">string</span><span style="color:#000000;"> HQLForActiveItems
    {
        </span><span style="color:#0000FF;">get</span><span style="color:#000000;">
        {
            </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> </span><span style="color:#800000;">@"</span><span style="color:#800000;">select max(dl.DiscountPercentage) from DiscountLevel2 as dl
              where dl.DiscountPercentage &lt;=
                 (select max(dl1.DiscountPercentage)
                        from DiscountLevel2 as dl1
                          where dl1.ClientTypeWithLimit = :ClientType
                          or
                          not exists (select dl1.DiscountPercentage from DiscountLevel2 as dl1
                            where dl1.ClientTypeWithLimit = :ClientType)
                )</span><span style="color:#800000;">"</span><span style="color:#000000;">;
        }
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">string</span><span style="color:#000000;"> HQLForAllItems
    {
        </span><span style="color:#0000FF;">get</span><span style="color:#000000;"> { </span><span style="color:#0000FF;">throw</span><span style="color:#000000;"> </span><span style="color:#0000FF;">new</span><span style="color:#000000;"> NotImplementedException(); }
    }

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> SetParameters(IQuery query)
    {
        query.SetInt32(</span><span style="color:#800000;">"</span><span style="color:#800000;">ClientType</span><span style="color:#800000;">"</span><span style="color:#000000;">, (</span><span style="color:#0000FF;">int</span><span style="color:#000000;">)_client.ClientType);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">string</span><span style="color:#000000;"> Sorting
    {
        </span><span style="color:#0000FF;">get</span><span style="color:#000000;">
        {
            </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> </span><span style="color:#800000;">""</span><span style="color:#000000;">;
        }
    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Most of the times when you seem to need a query with this purpose, the calculation should be done inside one of the existing aggregates or one that you did not identify yet. The fact that the aggregate is not used is already a problem, and isolating the calculation in a query adds more to it.</p>
<p>When you isolate a calculation inside a query, you make a decision that the calculation will always be needed directly from the database and you give it a form that is database-related (as you can see in the example above); in a way, you put that formula in the database instead of the domain and you lose the flexibility that the domain offers. As soon as the calculation will be needed inside the domain, you will face a duplication of logic (the calculation will be written once more, this time in the domain) or a painful refactor (to extract the calculation from the query, into a form that both the query and domain can use). </p>
<p>Plus, you are giving the query 2 responsibilities: the classical responsibility of loading something from the database, plus the responsibility of being the keeper of that business formula; both these responsibilities are heavy, so it&#8217;s best to not mix them.</p>
<p>It&#8217;s good to avoid these problems altogether, and use queries only for loading lists of aggregates as much as you can.</p>
<p>In conclusion, when you seem to need a query which loads a primitive from the database, use this simple guideline:</p>
<ul>
<li>if the formula must be applied over a big list of entities (&gt;1000) or a list which you expect to grow very fast (today it will be applied on 100 database entries, in one month it will be applied on 1000, and so on), use a query to apply the formula </li>
<li>otherwise, apply the formula in the domain
<ul>
<li>what makes sense to be in the aggregate &#8211; load with the aggregate </li>
<li>what does not make sense to be in the aggregate &#8211; load independently </li>
<li>create an aggregate extension which uses the aggregate and the other dependencies as parameters, to apply the formula and calculate the primitive </li>
</ul>
</li>
</ul>
<p>&#160;</p>
<p>In the CalculatedPrimitives action of the QueryController:</p>
<p>- we first load the client:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:845e2024-1cf2-40f9-8852-243c8b6aac2a" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">var client </span><span style="color:#000000;">=</span><span style="color:#000000;"> _client2LoadTask.Execute(</span><span style="color:#800080;">1</span><span style="color:#000000;">);</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>- after that, we calculate the maximum discount percentage that the client can receive using the above query:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:161cdc2b-15e1-4a21-ac9c-f19a840c7c55" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">var maxDiscountPercentageBad </span><span style="color:#000000;">=</span><span style="color:#000000;"> _calculatedPrimitiveTask1.Execute(client);</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>CalculatedPrimitiveTask1 is simply a wrapper for the CalculatedPrimitiveQuery1:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:fb2f1403-f525-4357-a633-c1b32a6180d9" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> CalculatedPrimitiveTask1 : BaseQueryTask</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">Client2, </span><span style="color:#0000FF;">decimal</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
  </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">readonly</span><span style="color:#000000;"> CalculatedPrimitiveQuery1 _calculatedPrimitiveQuery1;
  </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> Client2 _client;

  </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> CalculatedPrimitiveTask1(INHUnitOfWorkProvider uowProvider, CalculatedPrimitiveQuery1 calculatedPrimitiveQuery1) : </span><span style="color:#0000FF;">base</span><span style="color:#000000;">(uowProvider)
    {
      _calculatedPrimitiveQuery1 </span><span style="color:#000000;">=</span><span style="color:#000000;"> calculatedPrimitiveQuery1;
    }

  </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">decimal</span><span style="color:#000000;"> Execute(Client2 client)
    {
    _client </span><span style="color:#000000;">=</span><span style="color:#000000;"> client;
        ExecuteInternal();
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> Entity;
    }

  </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">decimal</span><span style="color:#000000;"> ExecuteQuery()
    {
    </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> _calculatedPrimitiveQuery1.Execute(UOW.Session, _client);
    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>As we have seen in the CalculatedPrimitiveQuery1 query, we will end up with a complicated HQL formula which will surely bring problems in the long run.</p>
<p>- in the second example, we make the same calculation but this time in the domain:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:b680a22b-8dfc-44f2-9d85-419840499199" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">var maxDiscountPercentageGood </span><span style="color:#000000;">=</span><span style="color:#000000;"> _calculatedPrimitiveTask2.Execute(client);</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This solution uses the guideline that we presented earlier, because the task loads the list of discount levels separately in the LoadAdditionalInitialData method:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:4ab543e3-bd2f-4adc-9c1e-110fbd96a749" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> CalculatedPrimitiveTask2 : BaseApplicationTask</span><span style="color:#000000;">&lt;</span><span style="color:#0000FF;">decimal</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
  </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">readonly</span><span style="color:#000000;"> DiscountLevel2ListTask _discountLevel2ListTask;
  </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">readonly</span><span style="color:#000000;"> MaxDiscountPercentage _maxDiscountPercentage;
  </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> Client2 _client;
  </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">DiscountLevel2</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> _discountLevel2List;

  </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> CalculatedPrimitiveTask2(INHUnitOfWorkProvider uowProvider, DiscountLevel2ListTask discountLevel2ListTask, MaxDiscountPercentage maxDiscountPercentage) : </span><span style="color:#0000FF;">base</span><span style="color:#000000;">(uowProvider)
  {
    _discountLevel2ListTask </span><span style="color:#000000;">=</span><span style="color:#000000;"> discountLevel2ListTask;
    _maxDiscountPercentage </span><span style="color:#000000;">=</span><span style="color:#000000;"> maxDiscountPercentage;
  }

  </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">decimal</span><span style="color:#000000;"> Execute(Client2 client)
  {
    _client </span><span style="color:#000000;">=</span><span style="color:#000000;"> client;
    ExecuteInternal();
    </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> Entity;
  }

  </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> LoadAdditionalInitialData()
  {
    _discountLevel2List </span><span style="color:#000000;">=</span><span style="color:#000000;"> _discountLevel2ListTask.Execute();
  }

  </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> ExecuteBusinessLogic()
  {
    Entity </span><span style="color:#000000;">=</span><span style="color:#000000;"> _client.CalculateThe(_maxDiscountPercentage, _discountLevel2List);
  }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>and the formula is encapsulated in an entity extension (a Calculator) which is executed in the ExecuteBusinessLogic method:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0658ed08-6a99-478e-91ef-b8a5a7cf4343" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> MaxDiscountPercentage : ICalculator</span><span style="color:#000000;">&lt;</span><span style="color:#0000FF;">decimal</span><span style="color:#000000;">, Client2, IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">DiscountLevel2</span><span style="color:#000000;">&gt;&gt;</span><span style="color:#000000;">
{
  </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">decimal</span><span style="color:#000000;"> Calculate(Client2 client, IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">DiscountLevel2</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> discountLevels)
  {
    var maxLevel </span><span style="color:#000000;">=</span><span style="color:#000000;"> discountLevels.FirstOrDefault(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.ClientTypeWithLimit </span><span style="color:#000000;">==</span><span style="color:#000000;"> client.ClientType);
    </span><span style="color:#0000FF;">if</span><span style="color:#000000;"> (maxLevel </span><span style="color:#000000;">!=</span><span style="color:#000000;"> </span><span style="color:#0000FF;">null</span><span style="color:#000000;">)
      </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> maxLevel.DiscountPercentage;

    </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> discountLevels.Max(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.DiscountPercentage);
  }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
</p>
</p>
<p>With this solution, everything remains in the domain, nicely encapsulated, and we don’t lose any flexibility.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/fwklight.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/fwklight.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/fwklight.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/fwklight.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/fwklight.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/fwklight.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/fwklight.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/fwklight.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/fwklight.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/fwklight.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/fwklight.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/fwklight.wordpress.com/129/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/fwklight.wordpress.com/129/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/fwklight.wordpress.com/129/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fwklight.wordpress.com&amp;blog=13183410&amp;post=129&amp;subd=fwklight&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://fwklight.wordpress.com/2010/04/27/queries-and-calculated-primitives/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8671b84cdbc9d549c082568ebc36f50e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">abutnaru</media:title>
		</media:content>
	</item>
		<item>
		<title>Aggregate lists vs Queries</title>
		<link>http://fwklight.wordpress.com/2010/04/26/aggregate-lists-vs-queries/</link>
		<comments>http://fwklight.wordpress.com/2010/04/26/aggregate-lists-vs-queries/#comments</comments>
		<pubDate>Mon, 26 Apr 2010 10:54:06 +0000</pubDate>
		<dc:creator>abutnaru</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://fwklight.wordpress.com/2010/04/26/aggregate-lists-vs-queries/</guid>
		<description><![CDATA[ALL THE EXAMPLES FROM THIS POST ARE EXTRACTED FROM THE FWKLIGHT DEMO. TO FIND OUT HOW TO GET STARTED WITH THE DEMO, READ THIS POST. Loading lists of aggregates is not the same with loading a list inside an aggregate. For example, if CustomerOrder is an aggregate and the business of the application suggests that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fwklight.wordpress.com&amp;blog=13183410&amp;post=116&amp;subd=fwklight&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>ALL THE EXAMPLES FROM THIS POST ARE EXTRACTED FROM <a href="http://code.google.com/p/fwklight/downloads/list">THE FWKLIGHT DEMO</a>. TO FIND OUT HOW TO GET STARTED WITH THE DEMO, READ <a href="http://fwklight.wordpress.com/2010/04/21/getting-started-with-the-fwklight-demo/">THIS</a> POST.</p>
<p>Loading lists of aggregates is not the same with loading a list inside an aggregate. </p>
<p>For example, if CustomerOrder is an aggregate and the business of the application suggests that it should contain a list of OrderDetails, you might load a list of CustomerOrders with a query at some point, but you should not use a query to load the list of OrderDetails for a CustomerOrder. Instead, to obtain the list of OrderDetails for a CustomerOrder you should load the CustomerOrder with a repository (or load a list of CustomerOrders with a query), and ask the CustomerOrder for its OrderDetails.</p>
<p>Here is how these entities and their mappings look like:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3e3402ac-072e-417a-9a63-c46c86204eb3" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> OrderDetail1
{
    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">virtual</span><span style="color:#000000;"> </span><span style="color:#0000FF;">int</span><span style="color:#000000;"> Id { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">virtual</span><span style="color:#000000;"> </span><span style="color:#0000FF;">decimal</span><span style="color:#000000;"> OrderDetailPrice { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">virtual</span><span style="color:#000000;"> CustomerOrder1 CustomerOrder1 { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:4ba1ab68-6623-4721-81b8-0d92e310c700" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">&lt;?</span><span style="color:#FF00FF;">xml version="1.0" encoding="utf-8" </span><span style="color:#0000FF;">?&gt;</span><span style="color:#000000;">

</span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">hibernate-mapping </span><span style="color:#FF0000;">xmlns</span><span style="color:#0000FF;">="urn:nhibernate-mapping-2.2"</span><span style="color:#FF0000;"> schema</span><span style="color:#0000FF;">="dbo"</span><span style="color:#FF0000;"> assembly</span><span style="color:#0000FF;">="FwkLightDemo.Domain"</span><span style="color:#FF0000;">  namespace</span><span style="color:#0000FF;">="FwkLightDemo.Domain.Entities"</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
  </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">class </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">="OrderDetail1"</span><span style="color:#FF0000;"> table</span><span style="color:#0000FF;">="OrderDetail1"</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">id </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">="Id"</span><span style="color:#FF0000;"> type</span><span style="color:#0000FF;">="Int32"</span><span style="color:#FF0000;"> column</span><span style="color:#0000FF;">="OrderDetail1Id"</span><span style="color:#FF0000;"> </span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
      </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">generator </span><span style="color:#FF0000;">class</span><span style="color:#0000FF;">="identity"</span><span style="color:#FF0000;"> </span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
    </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">id</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">

        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">many-to-one </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">="CustomerOrder1"</span><span style="color:#FF0000;"> column</span><span style="color:#0000FF;">="CustomerOrder1Id"</span><span style="color:#FF0000;"> class</span><span style="color:#0000FF;">="CustomerOrder1"</span><span style="color:#FF0000;"> not-null</span><span style="color:#0000FF;">="true"</span><span style="color:#FF0000;"> </span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">

        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">property </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">="OrderDetailPrice"</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">

  </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">class</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">hibernate-mapping</span><span style="color:#0000FF;">&gt;</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5be5b37c-491d-41b9-b4f0-188b62ee855e" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> CustomerOrder1
{
    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">virtual</span><span style="color:#000000;"> </span><span style="color:#0000FF;">int</span><span style="color:#000000;"> Id { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">virtual</span><span style="color:#000000;"> </span><span style="color:#0000FF;">decimal</span><span style="color:#000000;"> OrderPrice { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">virtual</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">OrderDetail1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> OrderDetails { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:830c5475-72b6-4f05-b555-9311eaba6f0f" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">&lt;?</span><span style="color:#FF00FF;">xml version="1.0" encoding="utf-8" </span><span style="color:#0000FF;">?&gt;</span><span style="color:#000000;">

</span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">hibernate-mapping </span><span style="color:#FF0000;">xmlns</span><span style="color:#0000FF;">="urn:nhibernate-mapping-2.2"</span><span style="color:#FF0000;"> schema</span><span style="color:#0000FF;">="dbo"</span><span style="color:#FF0000;"> assembly</span><span style="color:#0000FF;">="FwkLightDemo.Domain"</span><span style="color:#FF0000;">  namespace</span><span style="color:#0000FF;">="FwkLightDemo.Domain.Entities"</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
  </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">class </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">="CustomerOrder1"</span><span style="color:#FF0000;"> table</span><span style="color:#0000FF;">="CustomerOrder1"</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">id </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">="Id"</span><span style="color:#FF0000;"> type</span><span style="color:#0000FF;">="Int32"</span><span style="color:#FF0000;"> column</span><span style="color:#0000FF;">="CustomerOrder1Id"</span><span style="color:#FF0000;"> </span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
      </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">generator </span><span style="color:#FF0000;">class</span><span style="color:#0000FF;">="identity"</span><span style="color:#FF0000;"> </span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
    </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">id</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">

        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">property </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">="OrderPrice"</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">

        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">bag </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">="OrderDetails"</span><span style="color:#FF0000;"> inverse</span><span style="color:#0000FF;">="true"</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
            </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">key </span><span style="color:#FF0000;">column</span><span style="color:#0000FF;">="CustomerOrder1Id"</span><span style="color:#FF0000;"> </span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
            </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">one-to-many </span><span style="color:#FF0000;">class</span><span style="color:#0000FF;">="OrderDetail1"</span><span style="color:#FF0000;"> </span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
        </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">bag</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">

  </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">class</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">hibernate-mapping</span><span style="color:#0000FF;">&gt;</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>In the QueryController we have the AggregateLists action, where:</p>
<p>- we first load a list of CustomerOrders, select the one for which we need to retrieve the OrderDetails and ask it for its OrderDetails:</p>
</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:be1eb034-44a4-4c5c-b122-82fbac2c116e" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">var customerOrderList </span><span style="color:#000000;">=</span><span style="color:#000000;"> _aggregateListTask1.Execute(</span><span style="color:#800080;">2</span><span style="color:#000000;">);
var orderForWhichWeRetrieveDetails </span><span style="color:#000000;">=</span><span style="color:#000000;"> customerOrderList.FirstOrDefault(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.Id </span><span style="color:#000000;">==</span><span style="color:#000000;"> </span><span style="color:#800080;">3</span><span style="color:#000000;">);

var orderDetailListGood </span><span style="color:#000000;">=</span><span style="color:#000000;"> orderForWhichWeRetrieveDetails.OrderDetails;</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This is the efficient way to access the list of OrderDetails for a CustomerOrder, and the efficient way to access a list of subentities inside an aggregate.</p>
<p>- after that, we obtain the same list of OrderDetails by executing a query specialized in retrieving the OrderDetails for a CustomerOrder:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:5e69e990-643b-4f40-bb68-c1a1bfc6b06f" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">var orderDetailListBad </span><span style="color:#000000;">=</span><span style="color:#000000;"> _aggregateListTask2.Execute(orderForWhichWeRetrieveDetails);</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>The AggregateListTask2 QueryTask is simply a wrapper for AggregateListQuery2:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a8a95211-dcd0-4e94-919f-e9aad1c6ecbb" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> AggregateListQuery2: MultiCriteriaQuery</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">OrderDetail1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> CustomerOrder1 _order;

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">OrderDetail1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> Execute(ISession session, CustomerOrder1 order)
    {
        _order </span><span style="color:#000000;">=</span><span style="color:#000000;"> order;
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> Load(session);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> ApplyMainFilters(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">OrderDetail1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria)
    {
        criteria.Where(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.CustomerOrder1).Eq(_order);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> FilterOnlyActive(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">OrderDetail1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria) { }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> Sort(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">OrderDetail1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria) { }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:67467e2c-d70d-4fc6-ae4a-68c24d07892d" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> AggregateListTask2 : BaseListQueryTask</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">OrderDetail1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">readonly</span><span style="color:#000000;"> AggregateListQuery2 _aggregateListQuery2;
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> CustomerOrder1 _order;

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> AggregateListTask2(INHUnitOfWorkProvider uowProvider, AggregateListQuery2 aggregateListQuery2) : </span><span style="color:#0000FF;">base</span><span style="color:#000000;">(uowProvider)
    {
        _aggregateListQuery2 </span><span style="color:#000000;">=</span><span style="color:#000000;"> aggregateListQuery2;
    }

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">OrderDetail1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> Execute(CustomerOrder1 order)
    {
        _order </span><span style="color:#000000;">=</span><span style="color:#000000;"> order;
        ExecuteInternal();
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> Entity;
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">OrderDetail1</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> ExecuteQuery()
    {
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> _aggregateListQuery2.Execute(UOW.Session, _order);
    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This is the complicated way to access the list of OrderDetails for a CustomerOrder, and the complicated way to access a list of subentities inside an aggregate. By using this method you work more (you create at least 2 more classes), only to obtain code that is harder to control and maintain.</p>
<p>You should use the efficient method as often as you can, but there are a few things to consider.</p>
<p>&#160;</p>
<p><strong><u>When to favor loading lists inside aggregates over dedicated queries</u></strong></p>
<p>We first have to make an observation: for one context an entity must be considered an aggregate (it cannot be loaded inside any of the entities which are loaded in that context), and for another context the same entity can be considered a subentity (it can be loaded inside one of the other entities which are loaded in that context). This subtle difference is dictated by the business of the application and by each functionality / context that we are implementing, and making good use of it is essential.</p>
<p>As an example, let’s consider that a CustomerOrderSet can contain 1-100 CustomerOrders. If we need to show the list with all CustomerOrders, then we are only talking about the orders so there is no aggregate inside which we could load them – CustomerOrder is the aggregate. If we need to show the list of CustomerOrders for a CustomerOrderSet, then we have a context for the CustomerOrders, the CustomerOrderSet – CustomerOrderSet is the aggregate, and we can consider CustomerOrder its subentity.</p>
<p>Here is how we use this observation:</p>
<p>- when the entity must be considered an aggregate, we always load the single instance with a repository and the list with a query</p>
<p>- when the entity can be considered a subentity, the simplified rule is: whenever you can load the list inside an aggregate without too much pain, do it<br />
  <br />Here is how the detailed rule looks:</p>
<ul>
<li>if you are implementing a list with a pager and / or filters, you should load its contents with a query (no aggregate should take responsibility for filtering / paging through one of its lists); you should also consider using a linear entity (which is more appropriate for displaying lists) instead of a normal entity, but that’s another story</li>
<li>if you need to load a big list of entities (&gt;1000) or a list which you expect to grow very fast, you should use a query (such a list should be cached, and if displayed in the UI it should be filtered or paged &#8211; none of these are responsibilities for an aggregate; because of its size, such a list should never be included inside an aggregate because it has the potential to create performance bottlenecks – loading it with a dedicated query isolates it and reduces the risks)</li>
<li>if the aggregate inside which you can load the subentities must be loaded for the current context / page, you should load the list inside the aggregate </li>
<li>otherwise, you should use a query </li>
</ul>
<p>Although there is only one case in which you should load the list inside the aggregate, it’s the one that happens most.</p>
<p>&#160;</p>
<p>As an example, in the ListsInsideAggregatesOverQueries action inside the QueryController:</p>
<p>- we are first loading a list with all the CustomerOrders by using an AllItemsTask (which uses a query for loading all entities of a certain type):</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:f6ec8553-2904-4a5b-8304-f16ebfb06def" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">var ordersWhenAggregateOk </span><span style="color:#000000;">=</span><span style="color:#000000;"> _customerOrder3ListTask.Execute();</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Here is how the entity and mapping looks like:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:1d1eac21-ea69-4f5d-b043-98d2ef70b07f" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> CustomerOrder3
{
  </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">virtual</span><span style="color:#000000;"> </span><span style="color:#0000FF;">int</span><span style="color:#000000;"> Id { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">virtual</span><span style="color:#000000;"> </span><span style="color:#0000FF;">decimal</span><span style="color:#000000;"> OrderPrice { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">virtual</span><span style="color:#000000;"> CustomerOrderSet3 CustomerOrderSet3 { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:943ad793-21be-4ea1-8ca2-d3a8ba5e2320" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">&lt;?</span><span style="color:#FF00FF;">xml version="1.0" encoding="utf-8" </span><span style="color:#0000FF;">?&gt;</span><span style="color:#000000;">

</span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">hibernate-mapping </span><span style="color:#FF0000;">xmlns</span><span style="color:#0000FF;">="urn:nhibernate-mapping-2.2"</span><span style="color:#FF0000;"> schema</span><span style="color:#0000FF;">="dbo"</span><span style="color:#FF0000;"> assembly</span><span style="color:#0000FF;">="FwkLightDemo.Domain"</span><span style="color:#FF0000;">  namespace</span><span style="color:#0000FF;">="FwkLightDemo.Domain.Entities"</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
  </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">class </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">="CustomerOrder3"</span><span style="color:#FF0000;"> table</span><span style="color:#0000FF;">="CustomerOrder3"</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">id </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">="Id"</span><span style="color:#FF0000;"> type</span><span style="color:#0000FF;">="Int32"</span><span style="color:#FF0000;"> column</span><span style="color:#0000FF;">="CustomerOrder3Id"</span><span style="color:#FF0000;"> </span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
      </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">generator </span><span style="color:#FF0000;">class</span><span style="color:#0000FF;">="identity"</span><span style="color:#FF0000;"> </span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
    </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">id</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">

        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">property </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">="OrderPrice"</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">

        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">many-to-one </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">="CustomerOrderSet3"</span><span style="color:#FF0000;"> column</span><span style="color:#0000FF;">="CustomerOrderSet3Id"</span><span style="color:#FF0000;"> class</span><span style="color:#0000FF;">="CustomerOrderSet3"</span><span style="color:#FF0000;"> not-null</span><span style="color:#0000FF;">="true"</span><span style="color:#FF0000;"> </span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">

  </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">class</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">hibernate-mapping</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This is a case in which CustomerOrder is the aggregate, so it’s ok to load it separately with its own query.</p>
<p>- after that, we load a CustomerOrderSet with a LoadTask, and its CustomerOrders separately with a QueryTask:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:d1855567-fa4d-4c99-83b4-9663e0f0f0b9" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">var customerOrderSet </span><span style="color:#000000;">=</span><span style="color:#000000;"> _customerOrderSet3LoadTask.Execute(</span><span style="color:#800080;">1</span><span style="color:#000000;">);
var ordersWhenNotAggregateBad </span><span style="color:#000000;">=</span><span style="color:#000000;"> _customerOrder3ListTask2.Execute(customerOrderSet);</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Here is the CustomerOrderSet entity looks like:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:1b065f5e-17d7-4fae-a2e5-594fbf519496" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> CustomerOrderSet3
{
    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">virtual</span><span style="color:#000000;"> </span><span style="color:#0000FF;">int</span><span style="color:#000000;"> Id { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">virtual</span><span style="color:#000000;"> </span><span style="color:#0000FF;">decimal</span><span style="color:#000000;"> OrderSetPrice { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">virtual</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder3</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> CustomerOrders { </span><span style="color:#0000FF;">get</span><span style="color:#000000;">; </span><span style="color:#0000FF;">set</span><span style="color:#000000;">; }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8991366b-91c8-4fb8-bee6-ab2c7bd743e2" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">&lt;?</span><span style="color:#FF00FF;">xml version="1.0" encoding="utf-8" </span><span style="color:#0000FF;">?&gt;</span><span style="color:#000000;">

</span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">hibernate-mapping </span><span style="color:#FF0000;">xmlns</span><span style="color:#0000FF;">="urn:nhibernate-mapping-2.2"</span><span style="color:#FF0000;"> schema</span><span style="color:#0000FF;">="dbo"</span><span style="color:#FF0000;"> assembly</span><span style="color:#0000FF;">="FwkLightDemo.Domain"</span><span style="color:#FF0000;">  namespace</span><span style="color:#0000FF;">="FwkLightDemo.Domain.Entities"</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
  </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">class </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">="CustomerOrderSet3"</span><span style="color:#FF0000;"> table</span><span style="color:#0000FF;">="CustomerOrderSet3"</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
    </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">id </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">="Id"</span><span style="color:#FF0000;"> type</span><span style="color:#0000FF;">="Int32"</span><span style="color:#FF0000;"> column</span><span style="color:#0000FF;">="CustomerOrderSet3Id"</span><span style="color:#FF0000;"> </span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
      </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">generator </span><span style="color:#FF0000;">class</span><span style="color:#0000FF;">="identity"</span><span style="color:#FF0000;"> </span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
    </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">id</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">

        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">property </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">="OrderSetPrice"</span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">

        </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">bag </span><span style="color:#FF0000;">name</span><span style="color:#0000FF;">="CustomerOrders"</span><span style="color:#FF0000;"> inverse</span><span style="color:#0000FF;">="true"</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
            </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">key </span><span style="color:#FF0000;">column</span><span style="color:#0000FF;">="CustomerOrderSet3Id"</span><span style="color:#FF0000;"> </span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
            </span><span style="color:#0000FF;">&lt;</span><span style="color:#800000;">one-to-many </span><span style="color:#FF0000;">class</span><span style="color:#0000FF;">="CustomerOrder3"</span><span style="color:#FF0000;"> </span><span style="color:#0000FF;">/&gt;</span><span style="color:#000000;">
        </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">bag</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">

  </span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">class</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span><span style="color:#0000FF;">&lt;/</span><span style="color:#800000;">hibernate-mapping</span><span style="color:#0000FF;">&gt;</span><span style="color:#000000;">
</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>CustomerOrderSet3LoadTask is a simple LoadTask:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:1e04ef2a-95d3-4726-864f-b559e8713c18" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> CustomerOrderSet3LoadTask : BaseLoadTask</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrderSet3</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">int</span><span style="color:#000000;"> _id;

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> CustomerOrderSet3LoadTask(IBaseRepo</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrderSet3</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> repository, INHUnitOfWorkProvider uowProvider)
        : </span><span style="color:#0000FF;">base</span><span style="color:#000000;">(repository, uowProvider)
    {
    }

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> CustomerOrderSet3 Execute(</span><span style="color:#0000FF;">int</span><span style="color:#000000;"> id)
    {
        _id </span><span style="color:#000000;">=</span><span style="color:#000000;"> id;
        ExecuteInternal();
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> Entity;
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> ConfigureFilters()
    {
        Fetcher.Where(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.Id).Eq(_id);
    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>CustomerOrder3Query2 is a query which loads the orders for an orderSet:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3cb0c30a-abf2-4b9c-b49e-90f160a28b56" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> CustomerOrder3Query2: MultiCriteriaQuery</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder3</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> CustomerOrderSet3 _customerOrderSet3;

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder3</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> Execute(ISession session, CustomerOrderSet3 customerOrderSet3)
    {
        _customerOrderSet3 </span><span style="color:#000000;">=</span><span style="color:#000000;"> customerOrderSet3;
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> Load(session);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> ApplyMainFilters(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder3</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria)
    {
        criteria.Where(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.CustomerOrderSet3).Eq(_customerOrderSet3);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> FilterOnlyActive(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder3</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria){}

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> Sort(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder3</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria){}
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>and the CustomerOrder3ListTask2 is a wrapper for it:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6166ee0b-1c1b-4d68-a0a6-886fecb981fd" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> CustomerOrder3ListTask2: BaseListQueryTask</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder3</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> </span><span style="color:#0000FF;">readonly</span><span style="color:#000000;"> CustomerOrder3Query2 _customerOrder3Query;
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> CustomerOrderSet3 _customerOrderSet3;

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> CustomerOrder3ListTask2(INHUnitOfWorkProvider uowProvider, CustomerOrder3Query2 customerOrder3Query) : </span><span style="color:#0000FF;">base</span><span style="color:#000000;">(uowProvider)
    {
        _customerOrder3Query </span><span style="color:#000000;">=</span><span style="color:#000000;"> customerOrder3Query;
    }

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder3</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> Execute(CustomerOrderSet3 customerOrderSet3)
    {
        _customerOrderSet3 </span><span style="color:#000000;">=</span><span style="color:#000000;"> customerOrderSet3;
        ExecuteInternal();
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> Entity;
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">CustomerOrder3</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> ExecuteQuery()
    {
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> _customerOrder3Query.Execute(UOW.Session, _customerOrderSet3);
    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This is a case in which CustomerOrder can be considered a subentity (because we have an aggregate inside which we can load it – the CustomerOrderSet). We are not showing a list with a pager or filters, it’s not a big list (max 100) and it does not grow in time, so it’s not ok to load it separately with its own query like we did. Instead, we should load it together with the CustomerOrderSet aggregate.</p>
<p>- next, we obtain the same list of CustomerOrders directly from the CustomerOrderSet:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e28541b7-a8c9-494b-bd52-4051d80d9d53" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#000000;">var ordersWhenNotAggregateGood </span><span style="color:#000000;">=</span><span style="color:#000000;"> customerOrderSet.CustomerOrders;</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>Considering the conditions that we just described, this is the right approach.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/fwklight.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/fwklight.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/fwklight.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/fwklight.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/fwklight.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/fwklight.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/fwklight.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/fwklight.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/fwklight.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/fwklight.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/fwklight.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/fwklight.wordpress.com/116/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/fwklight.wordpress.com/116/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/fwklight.wordpress.com/116/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fwklight.wordpress.com&amp;blog=13183410&amp;post=116&amp;subd=fwklight&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://fwklight.wordpress.com/2010/04/26/aggregate-lists-vs-queries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8671b84cdbc9d549c082568ebc36f50e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">abutnaru</media:title>
		</media:content>
	</item>
		<item>
		<title>Queries</title>
		<link>http://fwklight.wordpress.com/2010/04/26/queries/</link>
		<comments>http://fwklight.wordpress.com/2010/04/26/queries/#comments</comments>
		<pubDate>Mon, 26 Apr 2010 10:24:39 +0000</pubDate>
		<dc:creator>abutnaru</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://fwklight.wordpress.com/2010/04/26/queries/</guid>
		<description><![CDATA[ALL THE EXAMPLES FROM THIS POST ARE EXTRACTED FROM THE FWKLIGHT DEMO. TO FIND OUT HOW TO GET STARTED WITH THE DEMO, READ THIS POST. The Query is the interface through which you will load lists of aggregates. Whenever you need to load a list of aggregates from the database, you should think of a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fwklight.wordpress.com&amp;blog=13183410&amp;post=114&amp;subd=fwklight&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>ALL THE EXAMPLES FROM THIS POST ARE EXTRACTED FROM <a href="http://code.google.com/p/fwklight/downloads/list">THE FWKLIGHT DEMO</a>. TO FIND OUT HOW TO GET STARTED WITH THE DEMO, READ <a href="http://fwklight.wordpress.com/2010/04/21/getting-started-with-the-fwklight-demo/">THIS</a> POST.</p>
<p>The Query is the interface through which you will load lists of aggregates.</p>
<p>Whenever you need to load a list of aggregates from the database, you should think of a query.</p>
<p>Whenever you need to load one aggregate based on certain filters / rules, you should not use a query. By convention, the query is for loading lists of aggregates. To load one aggregate, you should always use a LoadTask (which will automatically use a repository).</p>
<p>Queries are services which live somewhere between the database and the domain. Together with the repositories, they form a bridge between your application and the database. Using them for more than just loading lists of aggregates can lead to instable or low quality code, because you are taking business logic away from the domain.</p>
<p>Here is an example of a Query which loads all entities with IDs in the given list:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:cd5a5a49-3746-4e2f-b498-ef31af48fb9d" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;white-space:pre-wrap;overflow:auto;"><span style="color:#0000FF;">public</span><span style="color:#000000;"> </span><span style="color:#0000FF;">class</span><span style="color:#000000;"> SomeEntityListQuery: MultiCriteriaQuery</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">SomeEntity</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000FF;">private</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#0000FF;">int</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> _ids;

    </span><span style="color:#0000FF;">public</span><span style="color:#000000;"> IList</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">SomeEntity</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> Execute(ISession session, IList</span><span style="color:#000000;">&lt;</span><span style="color:#0000FF;">int</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> ids)
    {
        _ids </span><span style="color:#000000;">=</span><span style="color:#000000;"> ids;
        </span><span style="color:#0000FF;">return</span><span style="color:#000000;"> Load(session);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> ApplyMainFilters(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">SomeEntity</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria)
    {
        criteria.Where(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.Id).In(_ids);
    }

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> FilterOnlyActive(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">SomeEntity</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria){}

    </span><span style="color:#0000FF;">protected</span><span style="color:#000000;"> </span><span style="color:#0000FF;">override</span><span style="color:#000000;"> </span><span style="color:#0000FF;">void</span><span style="color:#000000;"> Sort(IListMultiCriteriaFetcher</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">SomeEntity</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> criteria){}
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/fwklight.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/fwklight.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/fwklight.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/fwklight.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/fwklight.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/fwklight.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/fwklight.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/fwklight.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/fwklight.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/fwklight.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/fwklight.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/fwklight.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/fwklight.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/fwklight.wordpress.com/114/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fwklight.wordpress.com&amp;blog=13183410&amp;post=114&amp;subd=fwklight&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://fwklight.wordpress.com/2010/04/26/queries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8671b84cdbc9d549c082568ebc36f50e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">abutnaru</media:title>
		</media:content>
	</item>
		<item>
		<title>Repository</title>
		<link>http://fwklight.wordpress.com/2010/04/24/repository/</link>
		<comments>http://fwklight.wordpress.com/2010/04/24/repository/#comments</comments>
		<pubDate>Sat, 24 Apr 2010 11:07:05 +0000</pubDate>
		<dc:creator>abutnaru</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://fwklight.wordpress.com/2010/04/24/repository/</guid>
		<description><![CDATA[ALL THE EXAMPLES FROM THIS POST ARE EXTRACTED FROM THE FWKLIGHT DEMO. TO FIND OUT HOW TO GET STARTED WITH THE DEMO, READ THIS POST. The Repository is the interface through which FwkLight manages the database related tasks for one aggregate (save, load, etc). It works only with individual aggregates, not with lists of aggregates. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fwklight.wordpress.com&amp;blog=13183410&amp;post=97&amp;subd=fwklight&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>ALL THE EXAMPLES FROM THIS POST ARE EXTRACTED FROM <a href="http://code.google.com/p/fwklight/downloads/list">THE FWKLIGHT DEMO</a>. TO FIND OUT HOW TO GET STARTED WITH THE DEMO, READ <a href="http://fwklight.wordpress.com/2010/04/21/getting-started-with-the-fwklight-demo/">THIS</a> POST.</p>
<p>The Repository is the interface through which FwkLight manages the database related tasks for one aggregate (save, load, etc).</p>
<p>It works only with individual aggregates, not with lists of aggregates. When you need to load a list of aggregates, you should always use a Query (instead of a Repository).</p>
<p>When saving aggregates, it automatically uses the current UnitOfWork (you don&#8217;t have to manage that manually).</p>
<p>The repository uses an IMultiCriteriaFetcher&lt;EntityType&gt; (which can be accessed through the MultiCriteriaFetcher property) to do most of its work.</p>
<p>It is automatically instantiated in any BaseLoadTask or BaseSaveTask service, and you don&#8217;t need to use it directly. You will only use the IMultiCriteriaFetcher&lt;EntityType&gt; that it offers, so you can configure the necessary filters.</p>
<p>In the following example, the LoadTask configures its internal Repository by overriding the ConfigureFilters method:</p>
<div style="display:inline;float:none;margin:0;padding:0;" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:ce4ad576-e193-4060-b2f9-2a2ff2a7f07c" class="wlWriterSmartContent">
<pre style="background-color:#ffffff;overflow:auto;"><span style="color:#0000ff;">public</span><span style="color:#000000;"> </span><span style="color:#0000ff;">class</span><span style="color:#000000;"> SomeEntityLoadTask: BaseLoadTask</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">SomeEntity</span><span style="color:#000000;">&gt;</span><span style="color:#000000;">
{
    </span><span style="color:#0000ff;">private</span><span style="color:#000000;"> </span><span style="color:#0000ff;">string</span><span style="color:#000000;"> _filter;

    </span><span style="color:#0000ff;">public</span><span style="color:#000000;"> SomeEntityLoadTask(IBaseRepo</span><span style="color:#000000;">&lt;</span><span style="color:#000000;">SomeEntity</span><span style="color:#000000;">&gt;</span><span style="color:#000000;"> repository, INHUnitOfWorkProvider uowProvider) : </span><span style="color:#0000ff;">base</span><span style="color:#000000;">(repository, uowProvider)
    {
    }

    </span><span style="color:#0000ff;">public</span><span style="color:#000000;"> SomeEntity Execute(</span><span style="color:#0000ff;">string</span><span style="color:#000000;"> filter)
    {
        _filter </span><span style="color:#000000;">=</span><span style="color:#000000;"> filter;
        ExecuteInternal();
        </span><span style="color:#0000ff;">return</span><span style="color:#000000;"> Entity;
    }

    </span><span style="color:#0000ff;">protected</span><span style="color:#000000;"> </span><span style="color:#0000ff;">override</span><span style="color:#000000;"> </span><span style="color:#0000ff;">void</span><span style="color:#000000;"> ConfigureFilters()
    {
        Fetcher.Where(p </span><span style="color:#000000;">=&gt;</span><span style="color:#000000;"> p.SomeEntityContent).Eq(_filter);
    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></p>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/fwklight.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/fwklight.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/fwklight.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/fwklight.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/fwklight.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/fwklight.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/fwklight.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/fwklight.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/fwklight.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/fwklight.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/fwklight.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/fwklight.wordpress.com/97/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/fwklight.wordpress.com/97/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/fwklight.wordpress.com/97/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=fwklight.wordpress.com&amp;blog=13183410&amp;post=97&amp;subd=fwklight&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://fwklight.wordpress.com/2010/04/24/repository/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/8671b84cdbc9d549c082568ebc36f50e?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">abutnaru</media:title>
		</media:content>
	</item>
	</channel>
</rss>
