<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="RSS_xslt_style.asp" version="1.0" ?>
<rss version="2.0" xmlns:WebWizForums="http://syndication.webwiz.co.uk/rss_namespace/">
 <channel>
  <title>DevForce Community Forum : TechTip: Query Strategies</title>
  <link>http://www.ideablade.com/forum/</link>
  <description>This is an XML content feed of; DevForce Community Forum : DevForce Classic : TechTip: Query Strategies</description>
  <pubDate>Fri, 12 Jun 2026 08:40:24 -700</pubDate>
  <lastBuildDate>Mon, 09 Jul 2007 11:57:03 -700</lastBuildDate>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Web Wiz Forums 9.69</generator>
  <ttl>360</ttl>
  <WebWizForums:feedURL>www.ideablade.com/forum/RSS_post_feed.asp?TID=111</WebWizForums:feedURL>
  <image>
   <title>DevForce Community Forum</title>
   <url>http://www.ideablade.com/forum/forum_images/IdeaBlade_logo_tm.png</url>
   <link>http://www.ideablade.com/forum/</link>
  </image>
  <item>
   <title>TechTip: Query Strategies :    Level 100 DevForce Express  View...</title>
   <link>http://www.ideablade.com/forum/forum_posts.asp?TID=111&amp;PID=248#248</link>
   <description>
    <![CDATA[<strong>Author:</strong> <a href="http://www.ideablade.com/forum/member_profile.asp?PF=4" rel="nofollow">GregD</a><br /><strong>Subject:</strong> 111<br /><strong>Posted:</strong> 09-Jul-2007 at 11:57am<br /><br /><TABLE cellSpacing=0 cellPadding=7 width="100%"><T><T><TR vAlign=top ="#ffffff"><TD colSpan=2 height=18><a href="http://rs6.net/tn.jsp?t=q7a55bcab.0.0.zufnlmbab.0&amp;p=http%3A%2F%2Fwww.ideablade.com%2Ftech_tips_definiti&#111;ns.htm" target="_blank"><FONT size=3>Level 100 <BR title=http://rs6.net/tn.jsp?t=q7a55bcab.0.0.zufnlmbab.0&amp;p=http%3A%2F%2Fwww.ideablade.com%2Ftech_tips_definiti&#111;ns.htm>DevForce Express</FONT></A></TD><TD colSpan=2><DIV align=right><a href="http://rs6.net/tn.jsp?t=q7a55bcab.0.0.zufnlmbab.0&amp;p=http%3A%2F%2Fwww.ideablade.com%2Ftechtips_summary.html" target="_blank"><FONT title=http://rs6.net/tn.jsp?t=q7a55bcab.0.0.zufnlmbab.0&amp;p=http%3A%2F%2Fwww.ideablade.com%2Ftechtips_summary. size=-2>View Archived Tech Tips</FONT></A></DIV><DIV align=right></DIV></TD></TR><TR vAlign=center ="#ffffff"><TD width="8%" height=18><strong><FONT color=#000000><a href="http://rs6.net/tn.jsp?t=q7a55bcab.0.0.zufnlmbab.0&amp;p=http%3A%2F%2Fwww.ideablade.com%2Ftechtips_summary.html" target="_blank"><img src="http://www.ideablade.com/images/lightbulb.jpg" height="33" width="33" border="0" alt="View%20all%20Tech%20Tips" /></A></FONT></strong></TD><TD colSpan=3><strong><SPAN style="mso-bookmark: _Toc158203164"><SPAN style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.0pt">Query Strategies</SPAN></SPAN></strong></TD></TR></T></T></TABLE><TABLE cellSpacing=0 cellPadding=7 width="100%"><T><T><TR><TD width="100%"><DIV ="Secti&#111;n1"><P><SPAN style="mso-bookmark: _Toc158203164"><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Verdana"><?:NAMESPACE PREFIX = O /><O:P><P ="Ms&#111;normal"><SPAN style="mso-font-kerning: 16.0pt">Through its <I style="mso-bidi-font-style: normal">QueryStrategy</I> object, DevForce gives you control over both the source of the data returned in a query, and how the data obtained from that source is merged with existing data in the PersistenceManager cache.</SPAN></P><P ="Ms&#111;normal">All forms of the PersistenceManager’s GetEntity(),GetEntities(), and GetEntitiesAsync() methods permit you to specify explicitly what QueryStrategy you want used in the specified data retrieval operation.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>In addition, every PersistenceManager has a <I style="mso-bidi-font-style: normal">DefaultQueryStrategy</I> that is used whenever you do not explicitly specify the query strategy you want.</P><P ="Ms&#111;normal">The <I style="mso-bidi-font-style: normal">QueryStrategy</I> object has three properties: <I style="mso-bidi-font-style: normal">FetchStrategy</I>, <I style="mso-bidi-font-style: normal">MergeStrategy</I>, and <I style="mso-bidi-font-style: normal">TransactionSettings</I>. The <I style="mso-bidi-font-style: normal">FetchStrategy</I> controls where DevForce looks for the requested data: in the cache, in the datasource, or in some combination of the two.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>The <I style="mso-bidi-font-style: normal">MergeStrategy</I> controls how DevForce resolves conflicts between the states of objects which, although already in the cache, are also retrieved from an external source. The <I style="mso-bidi-font-style: normal">TransactionSettings</I> object permits you to control the TimeOut and IsolationLevel associated with a query, and also whether and how to use the Microsoft Distributed Transaction Coordinator.</P><P style="mso-outline-level: 1" ="Ms&#111;normal"><B style="mso-bidi-font-weight: normal">FetchStrategies<O:P></O:P></B></P><P ="Ms&#111;normal">DevForce supports four different FetchStrategies: <I style="mso-bidi-font-style: normal">CacheOnly, CacheThenDataSource, DataSourceOnly, </I>and<I style="mso-bidi-font-style: normal"> DataSourceThenCache</I>. The meaning of these is shown in <SPAN style="mso-field-code: ' REF _Ref171409313 h '">Table <SPAN style="mso-no-proof: yes">1</SPAN></SPAN>.</P><P style="mso-outline-level: 1" ="MsoCapti&#111;n"><A name=_Ref171409313><strong><FONT size=2>Table </FONT></strong></A><strong><SPAN style="mso-bookmark: _Ref171409313"><SPAN style="mso-no-proof: yes">1</SPAN></SPAN> . FetchStrategies</strong><O:P></O:P></P></O:P></SPAN></SPAN><TABLE cellSpacing=0 cellPadding=4 width="100%" border=1><T><T><TR vAlign=top ="#cccccc"><TD width="29%"><strong>Strategy</strong></TD><TD width="71%"><strong>Action</strong></TD></TR><TR vAlign=top><TD>CacheThenDataSource</TD><TD>Check the query cache to see if the code has previously submitted the current query or an identifiable superset of it.<SPAN style="mso-bidi-font-size: 10.0pt"><a href="outbind://17-00000000B61EB4773F151A4B8356748A247480D2070095905C496080A141A5187A268145EB45000000215F7E00003AD3AD810087F94994D5ED7314A4EA95000006CB16600000/#_ftn1" target="_blank"></A></SPAN><SPAN style="mso-bidi-font-size: 10.0pt"><a href="outbind://17-00000000B61EB4773F151A4B8356748A247480D2070095905C496080A141A5187A268145EB45000000215F7E00003AD3AD810087F94994D5ED7314A4EA95000006CB16600000/#_ftn1" target="_blank"><SPAN title=outbind://17-00000000B61EB4773F151A4B8356748A247480D2070095905C496080A141A5187A268145EB45000000215F7E00003AD3AD810087F94994D5ED7314A4EA95000006CB16600000/#_ftn1 ="MsoFootnoteReference"><SPAN title=outbind://17-00000000B61EB4773F151A4B8356748A247480D2070095905C496080A141A5187A268145EB45000000215F7E00003AD3AD810087F94994D5ED7314A4EA95000006CB16600000/#_ftn1 style="mso-special-character: footnote"><FONT size=3> </FONT></SPAN></SPAN></A></SPAN>&#091;1&#093;<SPAN style="mso-bidi-font-size: 10.0pt"><a href="outbind://17-00000000B61EB4773F151A4B8356748A247480D2070095905C496080A141A5187A268145EB45000000215F7E00003AD3AD810087F94994D5ED7314A4EA95000006CB16600000/#_ftn1" target="_blank"><SPAN title=outbind://17-00000000B61EB4773F151A4B8356748A247480D2070095905C496080A141A5187A268145EB45000000215F7E00003AD3AD810087F94994D5ED7314A4EA95000006CB16600000/#_ftn1 ="MsoFootnoteReference"><SPAN title=outbind://17-00000000B61EB4773F151A4B8356748A247480D2070095905C496080A141A5187A268145EB45000000215F7E00003AD3AD810087F94994D5ED7314A4EA95000006CB16600000/#_ftn1 style="mso-special-character: footnote"><FONT size=3> </FONT></SPAN></SPAN></A></SPAN>&nbsp;If so, satisfy the query from the entity cache, and skip the trip to the datasource. If the query cache contains no query matching or encompassing the current query, DevForce will go to the datasource for the requested data.&nbsp;Having retrieved data from the datasource into the entity cache, it will then obtain from the entity cache the final set of entity references it returns to the caller.&nbsp;By so doing, it will return references to new entities that exist only in the cache, as well as those that duplicate rows from the datasource.</TD></TR><TR vAlign=top><TD><SPAN style="mso-bidi-font-size: 10.0pt">CacheOnly</SPAN></TD><TD><SPAN style="mso-bidi-font-size: 10.0pt">Search the cache only. Ignore the data source.</SPAN></TD></TR><TR vAlign=top><TD><SPAN style="mso-bidi-font-size: 10.0pt">DataSourceOnly<O:P></O:P></SPAN></TD><TD><SPAN style="mso-bidi-font-size: 10.0pt">Search only in the data source; return only the entities found in the data source.<O:P></O:P></SPAN></TD></TR><TR vAlign=top><TD><SPAN style="mso-bidi-font-size: 10.0pt">DataSourceThenCache<O:P></O:P></SPAN></TD><TD><SPAN style="mso-bidi-font-size: 10.0pt">First retrieve matching entries from the datasource into the entity cache. Then return references to all matching cached entities.<O:P></O:P></SPAN></TD></TR></T></T></TABLE><SPAN style="mso-bookmark: _Toc158203164"><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Verdana"><O:P><P style="mso-outline-level: 1" ="MsoCapti&#111;n">N<SPAN style="mso-bidi-font-size: 10.0pt">ote that the first-listed FetchStrategy, <I style="mso-bidi-font-style: normal">CacheThenDataSource</I>, behaves exactly like the last-listed one, <I style="mso-bidi-font-style: normal">DataSourceThenCache</I>, except that a preliminary discrimination is made regarding whether to make a</SPAN><SPAN style="FONT-SIZE: 12pt"> </SPAN><SPAN style="mso-bidi-font-size: 10.0pt">trip to the datasource at all.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN><I style="mso-bidi-font-style: normal">If</I> such a trip is made, the operation of the two strategies is identical.<O:P></O:P></SPAN><SPAN style="mso-bookmark: _Toc158203164"><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Verdana"><O:P> </O:P></SPAN></SPAN><SPAN style="mso-bookmark: _Toc158203164"><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Verdana"><O:P></O:P></SPAN></SPAN></P></O:P></SPAN></SPAN><SPAN style="mso-bookmark: _Toc158203164"><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Verdana"><O:P><P style="mso-outline-level: 1" ="Ms&#111;normal"><B style="mso-bidi-font-weight: normal">Operation of FetchStrategies When Disconnected<O:P></O:P></B></P><P style="mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" ="Ms&#111;normal"><SPAN style="mso-bidi-font-size: 10.0pt">If the client is disconnected from the data source, then the last two options, <I style="mso-bidi-font-style: normal">DataSourceOnly</I> and <I style="mso-bidi-font-style: normal">DataSourceThenCache</I>, will throw an </SPAN><SPAN style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.0pt">InvalidOperationException</SPAN><SPAN style="mso-bidi-font-size: 10.0pt">. However, the first option, <I style="mso-bidi-font-style: normal">CacheThenDataSource</I>, will simply return references to whatever is in the cache, regardless of whether it would go to the datasource if it could.<O:P></O:P></SPAN></P><P style="mso-outline-level: 1" ="Ms&#111;normal"><B style="mso-bidi-font-weight: normal">MergeStrategies<O:P></O:P></B></P><P ="Ms&#111;normal">A MergeStrategy comes into play whenever DevForce discovers that an entity retrieved from an external source already exists in the entity cache. (The two versions are recognized as the same entity because of matching type and primary key value.)<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>The MergeStrategy determines how DevForce will resolve any conflict found in the two instances of the entity.</P><P ="Ms&#111;normal">DevForce supports five different MergeStrategies: <I style="mso-bidi-font-style: normal">PreserveChanges, OverwriteChanges, PreserveChangesUnlessOriginalObsolete, PreserveChangesUpdateOriginal</I>, and <I style="mso-bidi-font-style: normal">NotApplicable</I>. Their meanings are shown in <SPAN style="mso-field-code: ' REF _Ref171410812 h '">Table <SPAN style="mso-no-proof: yes">2</SPAN></SPAN>.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN></P><P ="Ms&#111;normal">When reviewing the table, remember that, for every cached DevForce entity, two states are maintained: <I style="mso-bidi-font-style: normal">Original</I> and <I style="mso-bidi-font-style: normal">Current</I>.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>The <I style="mso-bidi-font-style: normal">Original</I> state comprises the set of values for all properties as they existed at the time of the last retrieval from, or save to, the datasource. The <I style="mso-bidi-font-style: normal">Current</I> state comprises the set of values for the object’s properties as the end user sees them. That is, the Current state values reflect any local changes that have been made since the entity was retrieved, or last saved.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>When an entity is persisted, it is the values in its <I style="mso-bidi-font-style: normal">Current</I> state that are saved.</P><P style="mso-outline-level: 1" ="MsoCapti&#111;n"><A name=_Ref171410812><strong><FONT size=2>Table </FONT></strong></A><strong><SPAN style="mso-bookmark: _Ref171410812"><SPAN style="mso-no-proof: yes">2</SPAN></SPAN> . MergeStrategies</strong></P></O:P></SPAN></SPAN><strong></strong><TABLE cellSpacing=0 cellPadding=4 width="100%" border=1><T><T><TR vAlign=top ="#cccccc"><TD width="30%"><strong>Strategy</strong></TD><TD width="70%"><strong>Action when cached entity has pending changes</strong></TD></TR><TR vAlign=top><TD><SPAN style="mso-bidi-font-size: 10.0pt">PreserveChanges<O:P></O:P></SPAN></TD><TD><SPAN style="mso-bidi-font-size: 10.0pt">Preserves the state of the cached entity.<O:P></O:P></SPAN></TD></TR><TR vAlign=top><TD><SPAN style="mso-bidi-font-size: 10.0pt">OverwriteChanges</SPAN></TD><TD><P ="Ms&#111;normal"><SPAN style="mso-bidi-font-size: 10.0pt"><O:P></O:P>Overwrites the cached entity with data from the data source. Sets the RowState of the cached entity to <I style="mso-bidi-font-style: normal">Unchanged</I>.</SPAN></P></TD></TR><TR vAlign=top><TD><SPAN style="mso-bidi-font-size: 10.0pt">PreserveChangesUnless<BR>OriginalObsolete<O:P></O:P></SPAN></TD><TD><P ="Ms&#111;normal"><SPAN style="mso-bidi-font-size: 10.0pt">Preserves the values in the <I style="mso-bidi-font-style: normal">Current</I> state of the cached entity, <I>if</I><SPAN style="mso-bidi-font-style: italic"> its Original state matches the state retrieved from the datasource</SPAN>. <O:P></O:P></SPAN></P><P ="Ms&#111;normal"><SPAN style="mso-bidi-font-size: 10.0pt">If the state as retrieved from the datasource differs from that found locally in the Original set of property values, this indicates that the entity has been changed externally by another user or process.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>In this case (with this MergeStrategy), DevForce overwrites the local entity, setting the values in both its <I style="mso-bidi-font-style: normal">Current</I> and <I style="mso-bidi-font-style: normal">Original</I> states to match that found in the datasource.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>DevForce also sets the RowState of the cached instance to <I style="mso-bidi-font-style: normal">Unchanged</I>.<O:P></O:P></SPAN></P></TD></TR><TR vAlign=top><TD><SPAN style="mso-bidi-font-size: 10.0pt">PreserveChangesUpdate<BR>Original<O:P></O:P></SPAN></TD><TD><SPAN style="mso-bidi-font-size: 10.0pt">Unconditionally preserves the values in the Current version for the cached entity; and <SPAN style="mso-bidi-font-style: italic">also updates the values in its Original version to match the values in the instance retrieved from the datasource.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>This has the effect of rendering the local entity savable, when it might otherwise trigger a concurrency exception. </SPAN><O:P></O:P></SPAN></TD></TR><TR vAlign=top><TD><SPAN style="mso-bidi-font-size: 10.0pt">NotApplicable</SPAN></TD><TD><SPAN style="mso-bidi-font-size: 10.0pt">This merge strategy must be used – and may only be used – with the CacheOnly fetch strategy. No merge action applies because no data is retrieved from any source outside the cache.<O:P></O:P></SPAN></TD></TR></T></T></TABLE><BR><SPAN style="mso-bookmark: _Toc158203164"><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Verdana"><DIV style="PADDING-RIGHT: 4pt; PADDING-LEFT: 4pt; PADDING-BOTTOM: 1pt; PADDING-TOP: 1pt; mso-element: para-border-div; mso-border-alt: solid text .5pt; ccffff: "><P style="BORDER-RIGHT: medium n&#111;ne; PADDING-RIGHT: 0in; BORDER-TOP: medium n&#111;ne; PADDING-LEFT: 0in; PADDING-BOTTOM: 0in; BORDER-LEFT: medium n&#111;ne; PADDING-TOP: 0in; BORDER-BOTTOM: medium n&#111;ne; mso-border-alt: solid text .5pt; mso-padding-alt: 1.0pt 4.0pt 1.0pt 4.0pt; ccffff: " ="Ms&#111;normal"><SPAN style="FONT-SIZE: 2pt"><O:P></O:P></SPAN>DevForce’s support for the <I style="mso-bidi-font-style: normal">PreserveChangesUnlessOriginalObsolete </I>and<I style="mso-bidi-font-style: normal"> PreserveChangesUpdateOriginal</I> MergeStrategies, like its detection of concurrency violations, depends upon the designation of a ConcurrencyColumn in the source table. <SPAN style="mso-spacerun: yes">&nbsp;</SPAN>To determine if the Original state of a local entity instance matches the state of the corresponding entity in the datasource, it compares the values in the designated concurrency column.<SPAN style="FONT-SIZE: 2pt"><O:P>&nbsp;</O:P></SPAN></P></DIV><P style="mso-outline-level: 1" ="Ms&#111;normal"><B style="mso-bidi-font-weight: normal">Pre-Defined QueryStrategies<O:P></O:P></B></P><P style="mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" ="Ms&#111;normal"><SPAN style="mso-bidi-font-size: 10.0pt">As we mentioned previously, every QueryStrategy combines a FetchStrategy with a MergeStrategy. Since there are four of the former and five of the latter, there are potentially 20 versions of QueryStrategy, even keeping the TransactionSettings constant.<O:P></O:P></SPAN></P><P style="mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" ="Ms&#111;normal"><SPAN style="mso-bidi-font-size: 10.0pt">Of course, the <I style="mso-bidi-font-style: normal">NotApplicable</I> MergeStrategy doesn’t apply to the <I style="mso-bidi-font-style: normal">CacheThenDataSource, DataSourceOnly, </I>and <I style="mso-bidi-font-style: normal">DataSourceThenCache</I> FetchStrategies, so that brings us down to 17 possible combinations.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>However, some of the combinations are more generally applicable and useful than others, so DevForce has identified four of the 17 possible combinations as being of particular significance, and has enshrined these four as pre-defined QueryStrategies. These pre-defined QueryStrategies combine Fetch and Merge strategies as shown in <SPAN style="mso-field-code: ' REF _Ref171412250 h '"><SPAN style="mso-bidi-font-size: 12.0pt">Table <SPAN style="mso-no-proof: yes">3</SPAN></SPAN></SPAN>.<O:P></O:P></SPAN></P><P style="mso-outline-level: 1" ="MsoCapti&#111;n"><A name=_Ref171412250><strong><FONT size=2>Table </FONT></strong></A><strong><SPAN style="mso-bookmark: _Ref171412250"><SPAN style="mso-no-proof: yes">3</SPAN></SPAN> . Fetch and merge strategies of the common query strategies</strong></P></SPAN></SPAN><TABLE cellSpacing=0 cellPadding=4 width="100%" border=1><T><T><TR ="#cccccc"><TD><strong>Query Strategy</strong></TD><TD><strong>Fetch Strategy</strong></TD><TD><strong>Merge Strategy</strong></TD></TR><TR><TD><SPAN style="FONT-SIZE: 9pt; mso-bidi-font-size: 12.0pt">Normal</SPAN><SPAN style="FONT-SIZE: 12pt"><O:P></O:P></SPAN></TD><TD><SPAN style="FONT-SIZE: 9pt; mso-bidi-font-size: 12.0pt">CacheThenDataSource</SPAN></TD><TD><SPAN style="FONT-SIZE: 9pt; mso-bidi-font-size: 12.0pt">PreserveChanges</SPAN><SPAN style="FONT-SIZE: 12pt"><O:P></O:P></SPAN></TD></TR><TR><TD><SPAN style="FONT-SIZE: 9pt; mso-bidi-font-size: 12.0pt">CacheOnly</SPAN></TD><TD><SPAN style="FONT-SIZE: 9pt; mso-bidi-font-size: 12.0pt">CacheOnly</SPAN></TD><TD><SPAN style="FONT-SIZE: 9pt; mso-bidi-font-size: 12.0pt">NotApplicable</SPAN></TD></TR><TR><TD><SPAN style="FONT-SIZE: 9pt; mso-bidi-font-size: 12.0pt">DataSourceOnly</SPAN></TD><TD><SPAN style="FONT-SIZE: 9pt; mso-bidi-font-size: 12.0pt">DataSourceOnly</SPAN><SPAN style="FONT-SIZE: 12pt"><O:P></O:P></SPAN></TD><TD><SPAN style="FONT-SIZE: 9pt; mso-bidi-font-size: 12.0pt">OverwriteChanges</SPAN><SPAN style="FONT-SIZE: 12pt"><O:P></O:P></SPAN></TD></TR><TR><TD><SPAN style="FONT-SIZE: 9pt; mso-bidi-font-size: 12.0pt">DataSourceThenCache</SPAN></TD><TD><SPAN style="FONT-SIZE: 9pt; mso-bidi-font-size: 12.0pt">DataSourceThenCache</SPAN><SPAN style="FONT-SIZE: 12pt"><O:P></O:P></SPAN></TD><TD><SPAN style="FONT-SIZE: 9pt; mso-bidi-font-size: 12.0pt">OverwriteChanges</SPAN></TD></TR></T></T></TABLE><SPAN style="mso-bookmark: _Toc158203164"><SPAN style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Verdana"><P style="mso-outline-level: 1" ="MsoCapti&#111;n"><BR>Here’s a usage sample of a pre-defined QueryStrategy: </P><P></P><P></P></SPAN></SPAN><TABLE cellSpacing=0 cellPadding=3 width="100%"><T><T><TR><TD width=19><strong>C#</strong></TD><TD width=636>&nbsp;</TD></TR><TR><TD>&nbsp;</TD><TD><P ="CodeSnippet0"><FONT face="Courier New, Courier, mono"><SPAN style="COLOR: #2b91af; mso-no-proof: yes">EntityQuery</SPAN><SPAN style="mso-no-proof: yes"> anEntityQuery =<BR></SPAN></FONT><FONT face="Courier New, Courier, mono"><SPAN style="mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">EntityQuery</SPAN>(<SPAN style="COLOR: blue">typeof</SPAN>(<SPAN style="COLOR: #2b91af">Employee</SPAN>), <SPAN style="COLOR: #2b91af">Employee</SPAN>.BirthDateEntityColumn,<BR></SPAN></FONT><FONT face="Courier New, Courier, mono"><SPAN style="mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: #2b91af">EntityQueryOp</SPAN>.GT, <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">DateTime</SPAN>(1900, 1, 1));<O:P></O:P></SPAN></FONT></P><P style="MARGIN-LEFT: 2.3pt" ="codeSnippet"><FONT face="Courier New, Courier, mono"><SPAN style="mso-bidi-font-size: 10.0pt; mso-no-proof: yes">mEmployees = mPersMgr.GetEntities&lt;<SPAN style="COLOR: #2b91af">Employee</SPAN>&gt;(<O:P><BR></O:P></SPAN></FONT><FONT face="Courier New, Courier, mono"><SPAN style="mso-bidi-font-size: 10.0pt; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>anEntityQuery, QueryStrategy.DataSourceThenCache);<O:P></O:P></SPAN></FONT></P></TD></TR><TR><TD><strong>VB:</strong></TD><TD>&nbsp;</TD></TR><TR><TD>&nbsp;</TD><TD><P style="MARGIN-TOP: 0in; mso-layout-grid-align: n&#111;ne" ="Ms&#111;normal"><SPAN style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt">Dim</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt"> anEntityQuery <SPAN style="COLOR: blue">As</SPAN> EntityQuery = _<O:P><BR></O:P></SPAN><SPAN style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: blue">New</SPAN> EntityQuery(<SPAN style="COLOR: blue">GetType</SPAN>(Employee), _<O:P><BR></O:P></SPAN><SPAN style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Employee.BirthDateEntityColumn, EntityQueryOp.GT, _<O:P><BR></O:P></SPAN><SPAN style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: blue">New</SPAN> DateTime(1900, 1, 1))<O:P></O:P></SPAN></P><P style="MARGIN-TOP: 0in; mso-layout-grid-align: n&#111;ne" ="Ms&#111;normal"><SPAN style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt">mEmployees = mPersMgr.GetEntities(<SPAN style="COLOR: blue">Of</SPAN> Employee)( _<O:P><BR></O:P></SPAN><SPAN style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>anEntityQuery, QueryStrategy.DataSourceThenCache)<O:P></O:P></SPAN></P></TD></TR></T></T></TABLE><P ="Ms&#111;normal"><SPAN style="FONT-FAMILY: Arial; mso-bidi-font-size: 10.0pt"><O:P><DIV ="Secti&#111;n1"><P ="Ms&#111;normal">Here’s how to define and use a custom QueryStrategy:</P><TABLE cellSpacing=0 cellPadding=3 width="100%"><T><T><TR><TD width=19><strong>C#</strong></TD><TD width=636>&nbsp;</TD></TR><TR><TD>&nbsp;</TD><TD><P ="CodeSnippet0"><FONT face="Courier New, Courier, mono"><SPAN style="COLOR: #2b91af; mso-no-proof: yes">EntityQuery</SPAN><SPAN style="mso-no-proof: yes"> anEntityQuery =<BR></SPAN></FONT><FONT face="Courier New, Courier, mono"><SPAN style="mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">EntityQuery</SPAN>(<SPAN style="COLOR: blue">typeof</SPAN>(<SPAN style="COLOR: #2b91af">Employee</SPAN>), <SPAN style="COLOR: #2b91af">Employee</SPAN>.BirthDateEntityColumn,<BR></SPAN></FONT><FONT face="Courier New, Courier, mono"><SPAN style="mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: #2b91af">EntityQueryOp</SPAN>.GT, <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">DateTime</SPAN>(1900, 1, 1));<O:P><BR></O:P></SPAN></FONT><FONT face="Courier New, Courier, mono"><SPAN style="COLOR: #2b91af; mso-bidi-font-size: 10.0pt; mso-no-proof: yes">QueryStrategy</SPAN><SPAN style="mso-bidi-font-size: 10.0pt; mso-no-proof: yes"> aQueryStrategy =<BR></SPAN></FONT><FONT face="Courier New, Courier, mono"><SPAN style="mso-bidi-font-size: 10.0pt; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: #2b91af">QueryStrategy</SPAN>(<SPAN style="COLOR: #2b91af">FetchStrategy</SPAN>.DataSourceThenCache,<O:P> </O:P></SPAN></FONT><FONT face="Courier New, Courier, mono"><SPAN style="mso-bidi-font-size: 10.0pt; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: #2b91af">MergeStrategy</SPAN>.PreserveChangesUpdateOriginal);</SPAN></FONT><FONT face="Courier New, Courier, mono"><SPAN style="mso-no-proof: yes">mPersMgr.GetEntities&lt;<SPAN style="COLOR: #2b91af">Employee</SPAN>&gt;(anEntityQuery,aQueryStrategy);</SPAN></FONT><FONT face="Courier New, Courier, mono"><O:P>&nbsp;</O:P></FONT></P><P style="MARGIN-LEFT: 2.3pt" ="codeSnippet"><FONT face="Courier New, Courier, mono"><SPAN style="mso-bidi-font-size: 10.0pt; mso-no-proof: yes">mEmployees = mPersMgr.GetEntities&lt;<SPAN style="COLOR: #2b91af">Employee</SPAN>&gt;(<BR></SPAN></FONT><FONT face="Courier New, Courier, mono"><SPAN style="mso-bidi-font-size: 10.0pt; mso-no-proof: yes"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>anEntityQuery, aQueryStrategy);<O:P></O:P></SPAN></FONT><FONT face="Courier New, Courier, mono"><SPAN style="mso-bidi-font-size: 10.0pt; mso-no-proof: yes"><O:P></O:P></SPAN></FONT><FONT face="Courier New, Courier, mono"><SPAN style="mso-bidi-font-size: 10.0pt; mso-no-proof: yes"><O:P></O:P></SPAN></FONT></P></TD></TR><TR><TD><strong>VB:</strong></TD><TD>&nbsp;</TD></TR><TR><TD>&nbsp;</TD><TD><P style="MARGIN-TOP: 0in; mso-layout-grid-align: n&#111;ne" ="Ms&#111;normal"><SPAN style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt">Dim</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt"> anEntityQuery <SPAN style="COLOR: blue">As</SPAN> EntityQuery = _<O:P><BR></O:P></SPAN><SPAN style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: blue">New</SPAN> EntityQuery(<SPAN style="COLOR: blue">GetType</SPAN>(Employee), _<O:P><BR></O:P></SPAN><SPAN style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Employee.BirthDateEntityColumn, EntityQueryOp.GT, _<O:P><BR></O:P></SPAN><SPAN style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: blue">New</SPAN> DateTime(1900, 1, 1))<O:P><BR></O:P></SPAN><SPAN style="COLOR: blue; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt">Dim</SPAN><SPAN style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt"> aQueryStrategy <SPAN style="COLOR: blue">As</SPAN> QueryStrategy = _<O:P><BR></O:P></SPAN><SPAN style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN><SPAN style="COLOR: blue">New</SPAN> QueryStrategy(FetchStrategy.DataSourceThenCache, _<O:P><BR></O:P></SPAN><SPAN style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>MergeStrategy.PreserveChangesUpdateOriginal)<O:P><BR></O:P></SPAN><SPAN style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt">mPersMgr.GetEntities(<SPAN style="COLOR: blue">Of</SPAN> Employee)(anEntityQuery,aQueryStrategy)<O:P></O:P></SPAN><SPAN style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt"><O:P>&nbsp;</O:P></SPAN></P><P style="MARGIN-TOP: 0in; mso-layout-grid-align: n&#111;ne" ="Ms&#111;normal"><SPAN style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt">mEmployees = mPersMgr.GetEntities(<SPAN style="COLOR: blue">Of</SPAN> Employee)( _<O:P><BR></O:P></SPAN><SPAN style="FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt"><SPAN style="mso-spacerun: yes">&nbsp; </SPAN>anEntityQuery, aQueryStrategy)<O:P></O:P></SPAN></P></TD></TR></T></T></TABLE><P ="Ms&#111;normal"><B style="mso-bidi-font-weight: normal">DefaultQueryStrategy<O:P></O:P></B></P><P ="Ms&#111;normal">We mentioned early in this article that the DevForce PersistenceManager has a <I style="mso-bidi-font-style: normal">DefaultQueryStrategy</I> property that can be used to shape the fetch and merge behavior of queries where the QueryStrategy is not explicitly specified. The default setting for the PersistenceManager’s DefaultQueryStrategy is <I style="mso-bidi-font-style: normal">QueryStrategy.Normal</I>. If you leave this setting at its default value, and in an individual query do nothing to countermand the default settings, then the FetchStrategy of <I style="mso-bidi-font-style: normal">CacheThenDataSource</I> will be used in combination with the MergeStrategy of <I style="mso-bidi-font-style: normal">PreserveChanges</I>.</P><P ="Ms&#111;normal">If for some reason you wanted a PersistenceManager where the default QueryStrategy would involve a trip to the datasource, you could assign a different QueryStrategy to the PM’s DefaultQueryStrategy property. For a given query, you could still use any desired QueryStrategy by explicitly specifying a different one.</P><P style="mso-outline-level: 1" ="Ms&#111;normal"><B style="mso-bidi-font-weight: normal">When to Use The Different QueryStrategies<O:P></O:P></B></P><P ="Ms&#111;normal">For most users, most of the time, the DevForce defaults are perfect: satisfy a query from the entity cache whenever possible; and when a trip to the datasource is found necessary, resolve any conflicts that occur between incoming data and data already cache by giving the local version priority.</P><P ="Ms&#111;normal">Your choice of a non-default strategy can be driven by a variety of things. For example, suppose your application supports online concert ticket sales. Your sales clerks need absolutely up-to-date information about what seats are available at the time they make a sale. In that use case, it will be essential to direct your query for available seats <I style="mso-bidi-font-style: normal">against the datasource</I>.</P><P ="Ms&#111;normal">In the <I style="mso-bidi-font-style: normal">Handling Concurrency Conflicts</I> instructional unit, we use the MergeStrategy of <I style="mso-bidi-font-style: normal">PreserveChangesUpdateOriginal</I> in our ConcurrencyHandler class to make an entity in conflict savable.<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>We do this only after calling the conflict to the attention of the end user who attempted the save, and having her confirm for us that, now fully informed about the state of the back-end object, she <I style="mso-bidi-font-style: normal">still</I> wants to save her changes.</P><P ="Ms&#111;normal">You can and will think of your own reasons to use different QueryStrategies. Just ask yourself, for a given data retrieval operation, whether the data in the cache is good enough, or you need absolutely current data from the source. Then ask yourself how you want to resolve conflicts between data already cached and duplicate incoming data, if such conflicts occur. DevForce gives you the flexibility to set the behavior exactly as need it.<O:P>&nbsp;</O:P> <BR clear=all></P></DIV><DIV style="mso-element: footnote-list"><HR align=left width="33%" SIZE=1><DIV id=ftn1 style="mso-element: footnote"><P ="MsoFootnoteText"><a href="outbind://17-00000000B61EB4773F151A4B8356748A247480D2070095905C496080A141A5187A268145EB45000000215F7E00003AD3AD810087F94994D5ED7314A4EA95000006CB16600000/#_ftnref1name=_ftn1" target="_blank"><SPAN title=outbind://17-00000000B61EB4773F151A4B8356748A247480D2070095905C496080A141A5187A268145EB45000000215F7E00003AD3AD810087F94994D5ED7314A4EA95000006CB16600000/#_ftnref1name=_ftn1 ="MsoFootnoteReference"><SPAN title=outbind://17-00000000B61EB4773F151A4B8356748A247480D2070095905C496080A141A5187A268145EB45000000215F7E00003AD3AD810087F94994D5ED7314A4EA95000006CB16600000/#_ftnref1name=_ftn1 style="mso-special-character: footnote"><SPAN title=outbind://17-00000000B61EB4773F151A4B8356748A247480D2070095905C496080A141A5187A268145EB45000000215F7E00003AD3AD810087F94994D5ED7314A4EA95000006CB16600000/#_ftnref1name=_ftn1 ="MsoFootnoteReference"><SPAN title=outbind://17-00000000B61EB4773F151A4B8356748A247480D2070095905C496080A141A5187A268145EB45000000215F7E00003AD3AD810087F94994D5ED7314A4EA95000006CB16600000/#_ftnref1name=_ftn1 style="FONT-SIZE: 9pt; FONT-FAMILY: 'Times New Roman'; mso-bidi-font-size: 10.0pt; mso-fareast-font-family: 'Times New Roman'; mso-ansi-: EN-US; mso-fareast-: EN-US; mso-bidi-: AR-SA">&#091;1&#093;</SPAN></SPAN></SPAN></SPAN></A> For example, if you’re now asking for all Customers from the Eastern region, and you previously retrieved all Customers (without qualification), the all-Customers-without-qualification query will be recognized as a superset of the all-Customers-from-the-Eastern-region query, and the new query will be satisfied from the entity cache.</P></DIV></DIV></SPAN></DIV></TD></TR></T></T></TABLE></O:P><span style="font-size:10px"><br /><br />Edited by GregD - 09-Jul-2007 at 12:08pm</span>]]>
   </description>
   <pubDate>Mon, 09 Jul 2007 11:57:03 -700</pubDate>
   <guid isPermaLink="true">http://www.ideablade.com/forum/forum_posts.asp?TID=111&amp;PID=248#248</guid>
  </item> 
 </channel>
</rss>