<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Robin Hames</title>
        <link>http://geekswithblogs.net/Rhames/Default.aspx</link>
        <description>Hints, tricks and tips relating to MS SQL Server and .NET</description>
        <language>en-GB</language>
        <copyright>Rhames</copyright>
        <managingEditor>rmhames@yahoo.co.uk</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <image>
            <title>Robin Hames</title>
            <url>http://geekswithblogs.net/images/RSS2Image.gif</url>
            <link>http://geekswithblogs.net/Rhames/Default.aspx</link>
            <width>77</width>
            <height>60</height>
        </image>
        <item>
            <title>Adding SQL Cache Dependencies to the Loosely coupled .NET Cache Provider</title>
            <category>C#</category>
            <category>Design Patterns</category>
            <category>ASP.NET</category>
            <category>Data Caching</category>
            <category>MVC 3 Razor</category>
            <link>http://geekswithblogs.net/Rhames/archive/2012/09/13/adding-sql-cache-dependencies-to-the-loosely-coupled-.net-cache.aspx</link>
            <description>&lt;p&gt;Originally posted on: &lt;a href='http://geekswithblogs.net/Rhames/archive/2012/09/13/adding-sql-cache-dependencies-to-the-loosely-coupled-.net-cache.aspx'&gt;http://geekswithblogs.net/Rhames/archive/2012/09/13/adding-sql-cache-dependencies-to-the-loosely-coupled-.net-cache.aspx&lt;/a&gt;&lt;/p&gt;&lt;p&gt;This post adds SQL Cache Dependency support to the loosely coupled .NET Cache Provider that I described in the previous post (&lt;a href="http://geekswithblogs.net/Rhames/archive/2012/09/11/loosely-coupled-.net-cache-provider-using-dependency-injection.aspx"&gt;http://geekswithblogs.net/Rhames/archive/2012/09/11/loosely-coupled-.net-cache-provider-using-dependency-injection.aspx&lt;/a&gt;). The sample code is available on github at &lt;a title="https://github.com/RobinHames/CacheProvider.git" href="https://github.com/RobinHames/CacheProvider.git"&gt;https://github.com/RobinHames/CacheProvider.git&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Each time we want to apply a cache dependency to a call to fetch or cache a data item we need to supply an instance of the relevant dependency implementation. This suggests an Abstract Factory will be useful to create cache dependencies as needed. We can then use Dependency Injection to inject the factory into the relevant consumer.&lt;/p&gt;  &lt;p&gt;Castle Windsor provides a typed factory facility that will be utilised to implement the cache dependency abstract factory (see &lt;a href="http://docs.castleproject.org/Windsor.Typed-Factory-Facility-interface-based-factories.ashx"&gt;http://docs.castleproject.org/Windsor.Typed-Factory-Facility-interface-based-factories.ashx&lt;/a&gt;).&lt;/p&gt;  &lt;h2&gt;Cache Dependency Interfaces&lt;/h2&gt;  &lt;p&gt;First I created a set of cache dependency interfaces in the domain layer, which can be used to pass a cache dependency into the cache provider.&lt;/p&gt;  &lt;p&gt;&lt;u&gt;ICacheDependency&lt;/u&gt;&lt;/p&gt;  &lt;p&gt;The ICacheDependency interface is simply an empty interface that is used as a parent for the specific cache dependency interfaces. This will allow us to place a generic constraint on the Cache Dependency Factory, and will give us a type that can be passed into the relevant Cache Provider methods.&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; CacheDiSample.Domain.CacheInterfaces&lt;/pre&gt;

  &lt;pre&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; ICacheDependency&lt;/pre&gt;

  &lt;pre&gt;    {&lt;/pre&gt;

  &lt;pre class="alt"&gt;    }&lt;/pre&gt;

  &lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;&lt;u&gt;ISqlCacheDependency.cs&lt;/u&gt;&lt;/p&gt;

&lt;p&gt;The ISqlCacheDependency interface provides specific SQL caching details, such as a Sql Command or a database connection and table. It is the concrete implementation of this interface that will be created by the factory in passed into the Cache Provider.&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Linq;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Text;&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; CacheDiSample.Domain.CacheInterfaces&lt;/pre&gt;

  &lt;pre class="alt"&gt;{&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; ISqlCacheDependency : ICacheDependency&lt;/pre&gt;

  &lt;pre class="alt"&gt;    {&lt;/pre&gt;

  &lt;pre&gt;        ISqlCacheDependency Initialise(&lt;span class="kwrd"&gt;string&lt;/span&gt; databaseConnectionName, &lt;span class="kwrd"&gt;string&lt;/span&gt; tableName);&lt;/pre&gt;

  &lt;pre class="alt"&gt;        ISqlCacheDependency Initialise(System.Data.SqlClient.SqlCommand sqlCommand);&lt;/pre&gt;

  &lt;pre&gt;    }&lt;/pre&gt;

  &lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt;If we want other types of cache dependencies, such as by key or file, interfaces may be created to support these (the sample code includes an IKeyCacheDependency interface).&lt;/p&gt;

&lt;h2&gt;Modifying ICacheProvider to accept Cache Dependencies&lt;/h2&gt;

&lt;p&gt;Next I modified the exisitng ICacheProvider&amp;lt;T&amp;gt; interface so that cache dependencies may be passed into a Fetch method call. I did this by adding two overloads to the existing Fetch methods, which take an IEnumerable&amp;lt;ICacheDependency&amp;gt; parameter (the IEnumerable allows more than one cache dependency to be included). I also added a method to create cache dependencies. This means that the implementation of the Cache Provider will require a dependency on the Cache Dependency Factory. It is pretty much down to personal choice as to whether this approach is taken, or whether the Cache Dependency Factory is injected directly into the repository or other consumer of Cache Provider. I think, because the cache dependency cannot be used without the Cache Provider, placing the dependency on the factory into the Cache Provider implementation is cleaner.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;ICacheProvider.cs&lt;/u&gt;&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; CacheDiSample.Domain.CacheInterfaces&lt;/pre&gt;

  &lt;pre class="alt"&gt;{&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; ICacheProvider&amp;lt;T&amp;gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;    {&lt;/pre&gt;

  &lt;pre&gt;        T Fetch(&lt;span class="kwrd"&gt;string&lt;/span&gt; key, Func&amp;lt;T&amp;gt; retrieveData, DateTime? absoluteExpiry, TimeSpan? relativeExpiry);&lt;/pre&gt;

  &lt;pre class="alt"&gt;        T Fetch(&lt;span class="kwrd"&gt;string&lt;/span&gt; key, Func&amp;lt;T&amp;gt; retrieveData, DateTime? absoluteExpiry, TimeSpan? relativeExpiry, &lt;/pre&gt;

  &lt;pre&gt;            IEnumerable&amp;lt;ICacheDependency&amp;gt; cacheDependencies);&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;        IEnumerable&amp;lt;T&amp;gt; Fetch(&lt;span class="kwrd"&gt;string&lt;/span&gt; key, Func&amp;lt;IEnumerable&amp;lt;T&amp;gt;&amp;gt; retrieveData, DateTime? absoluteExpiry, TimeSpan? relativeExpiry);&lt;/pre&gt;

  &lt;pre class="alt"&gt;        IEnumerable&amp;lt;T&amp;gt; Fetch(&lt;span class="kwrd"&gt;string&lt;/span&gt; key, Func&amp;lt;IEnumerable&amp;lt;T&amp;gt;&amp;gt; retrieveData, &lt;/pre&gt;

  &lt;pre&gt;            DateTime? absoluteExpiry, TimeSpan? relativeExpiry, &lt;/pre&gt;

  &lt;pre class="alt"&gt;            IEnumerable&amp;lt;ICacheDependency&amp;gt; cacheDependencies);&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;        U CreateCacheDependency&amp;lt;U&amp;gt;()&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;where&lt;/span&gt; U : ICacheDependency;&lt;/pre&gt;

  &lt;pre class="alt"&gt;    }&lt;/pre&gt;

  &lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;div class="csharpcode"&gt; &lt;/div&gt;

&lt;h2&gt;&lt;/h2&gt;

&lt;h2&gt;Cache Dependency Factory&lt;/h2&gt;

&lt;p&gt;Next I created the interface for the Cache Dependency Factory in the domain layer. &lt;/p&gt;

&lt;p&gt;&lt;u&gt;ICacheDependencyFactory.cs&lt;/u&gt;&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; CacheDiSample.Domain.CacheInterfaces&lt;/pre&gt;

  &lt;pre&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; ICacheDependencyFactory&lt;/pre&gt;

  &lt;pre&gt;    {&lt;/pre&gt;

  &lt;pre class="alt"&gt;        T Create&amp;lt;T&amp;gt;()&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;where&lt;/span&gt; T : ICacheDependency;&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;void&lt;/span&gt; Release&amp;lt;T&amp;gt;(T cacheDependency)&lt;/pre&gt;

  &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;where&lt;/span&gt; T : ICacheDependency;&lt;/pre&gt;

  &lt;pre&gt;    }&lt;/pre&gt;

  &lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;I used the ICacheDependency parent interface as a generic constraint on the create and release methods in the factory interface.&lt;/p&gt;

&lt;p&gt;Now the interfaces are in place, I moved on to the concrete implementations.&lt;/p&gt;

&lt;h2&gt;&lt;/h2&gt;

&lt;h2&gt;ISqlCacheDependency Concrete Implementation&lt;/h2&gt;

&lt;p&gt;The concrete implementation of ISqlCacheDependency will need to provide an instance of System.Web.Caching.SqlCacheDependency to the Cache Provider implementation. Unfortunately this class is sealed, so I cannot simply inherit from this. Instead, I created an interface called IAspNetCacheDependency that will provide a Create method to create an instance of the relevant System.Web.Caching Cache Dependency type. This interface is specific to the ASP.NET implementation of the Cache Provider, so it should be defined in the same layer as the concrete implementation of the Cache Provider (the MVC UI layer in the sample code).&lt;/p&gt;

&lt;p&gt;&lt;u&gt;IAspNetCacheDependency.cs&lt;/u&gt;&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Web.Caching;&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; CacheDiSample.CacheProviders&lt;/pre&gt;

  &lt;pre&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IAspNetCacheDependency&lt;/pre&gt;

  &lt;pre&gt;    {&lt;/pre&gt;

  &lt;pre class="alt"&gt;        CacheDependency CreateAspNetCacheDependency();&lt;/pre&gt;

  &lt;pre&gt;    }&lt;/pre&gt;

  &lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;h2&gt;&lt;/h2&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Next, I created the concrete implementation of the ISqlCacheDependency interface. This class also implements the IAspNetCacheDependency interface. This concrete implementation also is defined in the same layer as the Cache Provider implementation.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;AspNetSqlCacheDependency.cs&lt;/u&gt;&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Web.Caching;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; CacheDiSample.Domain.CacheInterfaces;&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; CacheDiSample.CacheProviders&lt;/pre&gt;

  &lt;pre class="alt"&gt;{&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; AspNetSqlCacheDependency : ISqlCacheDependency, IAspNetCacheDependency&lt;/pre&gt;

  &lt;pre class="alt"&gt;    {&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; databaseConnectionName;&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;string&lt;/span&gt; tableName;&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; System.Data.SqlClient.SqlCommand sqlCommand;&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="preproc"&gt;#region&lt;/span&gt; ISqlCacheDependency Members&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; ISqlCacheDependency Initialise(&lt;span class="kwrd"&gt;string&lt;/span&gt; databaseConnectionName, &lt;span class="kwrd"&gt;string&lt;/span&gt; tableName)&lt;/pre&gt;

  &lt;pre class="alt"&gt;        {&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;this&lt;/span&gt;.databaseConnectionName = databaseConnectionName;&lt;/pre&gt;

  &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;this&lt;/span&gt;.tableName = tableName;&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre class="alt"&gt;        }&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; ISqlCacheDependency Initialise(System.Data.SqlClient.SqlCommand sqlCommand)&lt;/pre&gt;

  &lt;pre&gt;        {&lt;/pre&gt;

  &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;this&lt;/span&gt;.sqlCommand = sqlCommand;&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre class="alt"&gt;        }&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="preproc"&gt;#endregion&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="preproc"&gt;#region&lt;/span&gt; IAspNetCacheDependency Members&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; System.Web.Caching.CacheDependency CreateAspNetCacheDependency()&lt;/pre&gt;

  &lt;pre&gt;        {&lt;/pre&gt;

  &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (sqlCommand != &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;

  &lt;pre&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; SqlCacheDependency(sqlCommand);&lt;/pre&gt;

  &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; SqlCacheDependency(databaseConnectionName, tableName);&lt;/pre&gt;

  &lt;pre class="alt"&gt;        }&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="preproc"&gt;#endregion&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;    }&lt;/pre&gt;

  &lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;ICacheProvider Concrete Implementation&lt;/h2&gt;

&lt;p&gt;The ICacheProvider interface is implemented by the CacheProvider class. This implementation is modified to include the changes to the ICacheProvider interface.&lt;/p&gt;

&lt;p&gt;First I needed to inject the Cache Dependency Factory into the Cache Provider:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt; &lt;span class="kwrd"&gt;private&lt;/span&gt; ICacheDependencyFactory cacheDependencyFactory;&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;span class="kwrd"&gt;public&lt;/span&gt; CacheProvider(ICacheDependencyFactory cacheDependencyFactory)&lt;/pre&gt;

  &lt;pre&gt; {&lt;/pre&gt;

  &lt;pre class="alt"&gt;     &lt;span class="kwrd"&gt;if&lt;/span&gt; (cacheDependencyFactory == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;

  &lt;pre&gt;         &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; ArgumentNullException(&lt;span class="str"&gt;"cacheDependencyFactory"&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="kwrd"&gt;this&lt;/span&gt;.cacheDependencyFactory = cacheDependencyFactory;&lt;/pre&gt;

  &lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Next I implemented the CreateCacheDependency method, which simply passes on the create request to the factory:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; U CreateCacheDependency&amp;lt;U&amp;gt;() &lt;span class="kwrd"&gt;where&lt;/span&gt; U : ICacheDependency&lt;/pre&gt;

  &lt;pre&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;.cacheDependencyFactory.Create&amp;lt;U&amp;gt;();&lt;/pre&gt;

  &lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;The signature of the FetchAndCache helper method was modified to take an additional IEnumerable&amp;lt;ICacheDependency&amp;gt; parameter:&lt;/p&gt;

&lt;div class="csharpcode"&gt; &lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;private&lt;/span&gt; U FetchAndCache&amp;lt;U&amp;gt;(&lt;span class="kwrd"&gt;string&lt;/span&gt; key, Func&amp;lt;U&amp;gt; retrieveData, &lt;/pre&gt;

  &lt;pre&gt;    DateTime? absoluteExpiry, TimeSpan? relativeExpiry, IEnumerable&amp;lt;ICacheDependency&amp;gt; cacheDependencies)&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt;and the following code added to create the relevant System.Web.Caching.CacheDependency object for any dependencies and pass them to the HttpContext Cache:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;CacheDependency aspNetCacheDependencies = &lt;span class="kwrd"&gt;null&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;if&lt;/span&gt; (cacheDependencies != &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;

  &lt;pre&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt; (cacheDependencies.Count() == 1)&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="rem"&gt;// We know that the implementations of ICacheDependency will also implement IAspNetCacheDependency&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="rem"&gt;// so we can use a cast here and call the CreateAspNetCacheDependency() method&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;        aspNetCacheDependencies = ((IAspNetCacheDependency)cacheDependencies.ElementAt(0)).CreateAspNetCacheDependency();&lt;/pre&gt;

  &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (cacheDependencies.Count() &amp;gt; 1)&lt;/pre&gt;

  &lt;pre&gt;    {&lt;/pre&gt;

  &lt;pre class="alt"&gt;        AggregateCacheDependency aggregateCacheDependency = &lt;span class="kwrd"&gt;new&lt;/span&gt; AggregateCacheDependency();&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (ICacheDependency cacheDependency &lt;span class="kwrd"&gt;in&lt;/span&gt; cacheDependencies)&lt;/pre&gt;

  &lt;pre class="alt"&gt;        {&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="rem"&gt;// We know that the implementations of ICacheDependency will also implement IAspNetCacheDependency&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;            &lt;span class="rem"&gt;// so we can use a cast here and call the CreateAspNetCacheDependency() method&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;            aggregateCacheDependency.Add(((IAspNetCacheDependency)cacheDependency).CreateAspNetCacheDependency());&lt;/pre&gt;

  &lt;pre class="alt"&gt;        }&lt;/pre&gt;

  &lt;pre&gt;        aspNetCacheDependencies = aggregateCacheDependency;&lt;/pre&gt;

  &lt;pre class="alt"&gt;    }&lt;/pre&gt;

  &lt;pre&gt;}&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;HttpContext.Current.Cache.Insert(key, &lt;span class="kwrd"&gt;value&lt;/span&gt;, aspNetCacheDependencies, absoluteExpiry.Value, relativeExpiry.Value);&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;The full code listing for the modified CacheProvider class is shown below:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Linq;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Web;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Web.Caching;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; CacheDiSample.Domain.CacheInterfaces;&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; CacheDiSample.CacheProviders&lt;/pre&gt;

  &lt;pre class="alt"&gt;{&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CacheProvider&amp;lt;T&amp;gt; : ICacheProvider&amp;lt;T&amp;gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;    {&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; ICacheDependencyFactory cacheDependencyFactory;&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; CacheProvider(ICacheDependencyFactory cacheDependencyFactory)&lt;/pre&gt;

  &lt;pre class="alt"&gt;        {&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (cacheDependencyFactory == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;

  &lt;pre class="alt"&gt;                &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; ArgumentNullException(&lt;span class="str"&gt;"cacheDependencyFactory"&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;this&lt;/span&gt;.cacheDependencyFactory = cacheDependencyFactory;&lt;/pre&gt;

  &lt;pre&gt;        }&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; T Fetch(&lt;span class="kwrd"&gt;string&lt;/span&gt; key, Func&amp;lt;T&amp;gt; retrieveData, DateTime? absoluteExpiry, TimeSpan? relativeExpiry)&lt;/pre&gt;

  &lt;pre class="alt"&gt;        {&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; FetchAndCache&amp;lt;T&amp;gt;(key, retrieveData, absoluteExpiry, relativeExpiry, &lt;span class="kwrd"&gt;null&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre class="alt"&gt;        }&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; T Fetch(&lt;span class="kwrd"&gt;string&lt;/span&gt; key, Func&amp;lt;T&amp;gt; retrieveData, DateTime? absoluteExpiry, TimeSpan? relativeExpiry, &lt;/pre&gt;

  &lt;pre&gt;            IEnumerable&amp;lt;ICacheDependency&amp;gt; cacheDependencies)&lt;/pre&gt;

  &lt;pre class="alt"&gt;        {&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; FetchAndCache&amp;lt;T&amp;gt;(key, retrieveData, absoluteExpiry, relativeExpiry, cacheDependencies);&lt;/pre&gt;

  &lt;pre class="alt"&gt;        }&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; Fetch(&lt;span class="kwrd"&gt;string&lt;/span&gt; key, Func&amp;lt;IEnumerable&amp;lt;T&amp;gt;&amp;gt; retrieveData, DateTime? absoluteExpiry, TimeSpan? relativeExpiry)&lt;/pre&gt;

  &lt;pre&gt;        {&lt;/pre&gt;

  &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; FetchAndCache&amp;lt;IEnumerable&amp;lt;T&amp;gt;&amp;gt;(key, retrieveData, absoluteExpiry, relativeExpiry, &lt;span class="kwrd"&gt;null&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre&gt;        }&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; Fetch(&lt;span class="kwrd"&gt;string&lt;/span&gt; key, Func&amp;lt;IEnumerable&amp;lt;T&amp;gt;&amp;gt; retrieveData, DateTime? absoluteExpiry, TimeSpan? relativeExpiry, &lt;/pre&gt;

  &lt;pre class="alt"&gt;            IEnumerable&amp;lt;ICacheDependency&amp;gt; cacheDependencies)&lt;/pre&gt;

  &lt;pre&gt;        {&lt;/pre&gt;

  &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; FetchAndCache&amp;lt;IEnumerable&amp;lt;T&amp;gt;&amp;gt;(key, retrieveData, absoluteExpiry, relativeExpiry, cacheDependencies);&lt;/pre&gt;

  &lt;pre&gt;        }&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; U CreateCacheDependency&amp;lt;U&amp;gt;() &lt;span class="kwrd"&gt;where&lt;/span&gt; U : ICacheDependency&lt;/pre&gt;

  &lt;pre class="alt"&gt;        {&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;this&lt;/span&gt;.cacheDependencyFactory.Create&amp;lt;U&amp;gt;();&lt;/pre&gt;

  &lt;pre class="alt"&gt;        }&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="preproc"&gt;#region&lt;/span&gt; Helper Methods&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; U FetchAndCache&amp;lt;U&amp;gt;(&lt;span class="kwrd"&gt;string&lt;/span&gt; key, Func&amp;lt;U&amp;gt; retrieveData, &lt;/pre&gt;

  &lt;pre&gt;            DateTime? absoluteExpiry, TimeSpan? relativeExpiry, IEnumerable&amp;lt;ICacheDependency&amp;gt; cacheDependencies)&lt;/pre&gt;

  &lt;pre class="alt"&gt;        {&lt;/pre&gt;

  &lt;pre&gt;            U &lt;span class="kwrd"&gt;value&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (!TryGetValue&amp;lt;U&amp;gt;(key, &lt;span class="kwrd"&gt;out&lt;/span&gt; &lt;span class="kwrd"&gt;value&lt;/span&gt;))&lt;/pre&gt;

  &lt;pre&gt;            {&lt;/pre&gt;

  &lt;pre class="alt"&gt;                &lt;span class="kwrd"&gt;value&lt;/span&gt; = retrieveData();&lt;/pre&gt;

  &lt;pre&gt;                &lt;span class="kwrd"&gt;if&lt;/span&gt; (!absoluteExpiry.HasValue)&lt;/pre&gt;

  &lt;pre class="alt"&gt;                    absoluteExpiry = Cache.NoAbsoluteExpiration;&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;                &lt;span class="kwrd"&gt;if&lt;/span&gt; (!relativeExpiry.HasValue)&lt;/pre&gt;

  &lt;pre&gt;                    relativeExpiry = Cache.NoSlidingExpiration;&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;                CacheDependency aspNetCacheDependencies = &lt;span class="kwrd"&gt;null&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;                &lt;span class="kwrd"&gt;if&lt;/span&gt; (cacheDependencies != &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;

  &lt;pre class="alt"&gt;                {&lt;/pre&gt;

  &lt;pre&gt;                    &lt;span class="kwrd"&gt;if&lt;/span&gt; (cacheDependencies.Count() == 1)&lt;/pre&gt;

  &lt;pre class="alt"&gt;                        &lt;span class="rem"&gt;// We know that the implementations of ICacheDependency will also implement IAspNetCacheDependency&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;                        &lt;span class="rem"&gt;// so we can use a cast here and call the CreateAspNetCacheDependency() method&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;                        aspNetCacheDependencies = &lt;/pre&gt;

  &lt;pre&gt;                            ((IAspNetCacheDependency)cacheDependencies.ElementAt(0)).CreateAspNetCacheDependency();&lt;/pre&gt;

  &lt;pre class="alt"&gt;                    &lt;span class="kwrd"&gt;else&lt;/span&gt; &lt;span class="kwrd"&gt;if&lt;/span&gt; (cacheDependencies.Count() &amp;gt; 1)&lt;/pre&gt;

  &lt;pre&gt;                    {&lt;/pre&gt;

  &lt;pre class="alt"&gt;                        AggregateCacheDependency aggregateCacheDependency = &lt;span class="kwrd"&gt;new&lt;/span&gt; AggregateCacheDependency();&lt;/pre&gt;

  &lt;pre&gt;                        &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (ICacheDependency cacheDependency &lt;span class="kwrd"&gt;in&lt;/span&gt; cacheDependencies)&lt;/pre&gt;

  &lt;pre class="alt"&gt;                        {&lt;/pre&gt;

  &lt;pre&gt;                            &lt;span class="rem"&gt;// We know that the implementations of ICacheDependency will also implement IAspNetCacheDependency&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;                            &lt;span class="rem"&gt;// so we can use a cast here and call the CreateAspNetCacheDependency() method&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;                            aggregateCacheDependency.Add(&lt;/pre&gt;

  &lt;pre class="alt"&gt;                                ((IAspNetCacheDependency)cacheDependency).CreateAspNetCacheDependency());&lt;/pre&gt;

  &lt;pre&gt;                        }&lt;/pre&gt;

  &lt;pre class="alt"&gt;                        aspNetCacheDependencies = aggregateCacheDependency;&lt;/pre&gt;

  &lt;pre&gt;                    }&lt;/pre&gt;

  &lt;pre class="alt"&gt;                }&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;                HttpContext.Current.Cache.Insert(key, &lt;span class="kwrd"&gt;value&lt;/span&gt;, aspNetCacheDependencies, absoluteExpiry.Value, relativeExpiry.Value);&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;            }&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;value&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre class="alt"&gt;        }&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; TryGetValue&amp;lt;U&amp;gt;(&lt;span class="kwrd"&gt;string&lt;/span&gt; key, &lt;span class="kwrd"&gt;out&lt;/span&gt; U &lt;span class="kwrd"&gt;value&lt;/span&gt;)&lt;/pre&gt;

  &lt;pre&gt;        {&lt;/pre&gt;

  &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;object&lt;/span&gt; cachedValue = HttpContext.Current.Cache.Get(key);&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (cachedValue == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;

  &lt;pre class="alt"&gt;            {&lt;/pre&gt;

  &lt;pre&gt;                &lt;span class="kwrd"&gt;value&lt;/span&gt; = &lt;span class="kwrd"&gt;default&lt;/span&gt;(U);&lt;/pre&gt;

  &lt;pre class="alt"&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;false&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre&gt;            }&lt;/pre&gt;

  &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;            {&lt;/pre&gt;

  &lt;pre class="alt"&gt;                &lt;span class="kwrd"&gt;try&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;                {&lt;/pre&gt;

  &lt;pre class="alt"&gt;                    &lt;span class="kwrd"&gt;value&lt;/span&gt; = (U)cachedValue;&lt;/pre&gt;

  &lt;pre&gt;                    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;true&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre class="alt"&gt;                }&lt;/pre&gt;

  &lt;pre&gt;                &lt;span class="kwrd"&gt;catch&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;                {&lt;/pre&gt;

  &lt;pre&gt;                    &lt;span class="kwrd"&gt;value&lt;/span&gt; = &lt;span class="kwrd"&gt;default&lt;/span&gt;(U);&lt;/pre&gt;

  &lt;pre class="alt"&gt;                    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;false&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre&gt;                }&lt;/pre&gt;

  &lt;pre class="alt"&gt;            }&lt;/pre&gt;

  &lt;pre&gt;        }&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="preproc"&gt;#endregion&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;    }&lt;/pre&gt;

  &lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;&lt;style type="text/css"&gt;&lt;![CDATA[



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;Wiring up the DI Container&lt;/h2&gt;

&lt;p&gt;Now the implementations for the Cache Dependency are in place, I wired them up in the existing Windsor CacheInstaller. First I needed to register the implementation of the ISqlCacheDependency interface:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;container.Register(&lt;/pre&gt;

  &lt;pre&gt;    Component.For&amp;lt;ISqlCacheDependency&amp;gt;()&lt;/pre&gt;

  &lt;pre class="alt"&gt;    .ImplementedBy&amp;lt;AspNetSqlCacheDependency&amp;gt;()&lt;/pre&gt;

  &lt;pre&gt;    .LifestyleTransient());&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Next I registered the Cache Dependency Factory. Notice that I have not implemented the ICacheDependencyFactory interface. Castle Windsor will do this for me by using the Type Factory Facility. I do need to bring the Castle.Facilities.TypedFacility namespace into scope:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; Castle.Facilities.TypedFactory;&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Then I registered the factory:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;container.AddFacility&amp;lt;TypedFactoryFacility&amp;gt;();&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;container.Register(&lt;/pre&gt;

  &lt;pre&gt;    Component.For&amp;lt;ICacheDependencyFactory&amp;gt;()&lt;/pre&gt;

  &lt;pre class="alt"&gt;    .AsFactory());&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;&lt;style type="text/css"&gt;&lt;![CDATA[



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt;The full code for the CacheInstaller class is:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; Castle.MicroKernel.Registration;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; Castle.MicroKernel.SubSystems.Configuration;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; Castle.Windsor;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; Castle.Facilities.TypedFactory;&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; CacheDiSample.Domain.CacheInterfaces;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; CacheDiSample.CacheProviders;&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; CacheDiSample.WindsorInstallers&lt;/pre&gt;

  &lt;pre&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CacheInstaller : IWindsorInstaller&lt;/pre&gt;

  &lt;pre&gt;    {&lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Install(IWindsorContainer container, IConfigurationStore store)&lt;/pre&gt;

  &lt;pre&gt;        {&lt;/pre&gt;

  &lt;pre class="alt"&gt;            container.Register(&lt;/pre&gt;

  &lt;pre&gt;                Component.For(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(ICacheProvider&amp;lt;&amp;gt;))&lt;/pre&gt;

  &lt;pre class="alt"&gt;                .ImplementedBy(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(CacheProvider&amp;lt;&amp;gt;))&lt;/pre&gt;

  &lt;pre&gt;                .LifestyleTransient());&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;            container.Register(&lt;/pre&gt;

  &lt;pre class="alt"&gt;                Component.For&amp;lt;ISqlCacheDependency&amp;gt;()&lt;/pre&gt;

  &lt;pre&gt;                .ImplementedBy&amp;lt;AspNetSqlCacheDependency&amp;gt;()&lt;/pre&gt;

  &lt;pre class="alt"&gt;                .LifestyleTransient());&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;            container.AddFacility&amp;lt;TypedFactoryFacility&amp;gt;();&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;            container.Register(&lt;/pre&gt;

  &lt;pre&gt;                Component.For&amp;lt;ICacheDependencyFactory&amp;gt;()&lt;/pre&gt;

  &lt;pre class="alt"&gt;                .AsFactory());&lt;/pre&gt;

  &lt;pre&gt;        }&lt;/pre&gt;

  &lt;pre class="alt"&gt;    }&lt;/pre&gt;

  &lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;Configuring the ASP.NET SQL Cache Dependency&lt;/h2&gt;

&lt;p&gt;There are a couple of configuration steps required to enable SQL Cache Dependency for the application and database. From the Visual Studio Command Prompt, the following commands should be used to enable the Cache Polling of the relevant database tables:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;aspnet_regsql -S &amp;lt;servername&amp;gt; -E -d &amp;lt;databasename&amp;gt; –ed&lt;/pre&gt;

  &lt;pre&gt;aspnet_regsql -S &amp;lt;servername&amp;gt; -E -d CacheSample –et –t &amp;lt;tablename&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;(The –t option should be repeated for each table that is to be made available for cache dependencies).&lt;/p&gt;

&lt;p&gt;Finally the SQL Cache Polling needs to be enabled by adding the following configuration to the &amp;lt;system.web&amp;gt; section of web.config:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&amp;lt;caching&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;    &amp;lt;sqlCacheDependency pollTime=&lt;span class="str"&gt;"10000"&lt;/span&gt; enabled=&lt;span class="str"&gt;"true"&lt;/span&gt;&amp;gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;        &amp;lt;databases&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;            &amp;lt;add name=&lt;span class="str"&gt;"BloggingContext"&lt;/span&gt; connectionStringName=&lt;span class="str"&gt;"BloggingContext"&lt;/span&gt;/&amp;gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;        &amp;lt;/databases&amp;gt;&lt;/pre&gt;

  &lt;pre&gt;    &amp;lt;/sqlCacheDependency&amp;gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&amp;lt;/caching&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;(obviously the name and connection string name should be altered as required).&lt;/p&gt;

&lt;h2&gt;Using a SQL Cache Dependency&lt;/h2&gt;

&lt;p&gt;Now all the coding is complete. To specify a SQL Cache Dependency, I can modify my BlogRepositoryWithCaching decorator class (see the earlier post) as follows:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; IList&amp;lt;Blog&amp;gt; GetAll()&lt;/pre&gt;

  &lt;pre&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;    var sqlCacheDependency = cacheProvider.CreateCacheDependency&amp;lt;ISqlCacheDependency&amp;gt;()&lt;/pre&gt;

  &lt;pre&gt;        .Initialise(&lt;span class="str"&gt;"BloggingContext"&lt;/span&gt;, &lt;span class="str"&gt;"Blogs"&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;    ICacheDependency[] cacheDependencies = &lt;span class="kwrd"&gt;new&lt;/span&gt; ICacheDependency[] { sqlCacheDependency };&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="kwrd"&gt;string&lt;/span&gt; key = &lt;span class="kwrd"&gt;string&lt;/span&gt;.Format(&lt;span class="str"&gt;"CacheDiSample.DataAccess.GetAll"&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="kwrd"&gt;return&lt;/span&gt; cacheProvider.Fetch(key, () =&amp;gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;    {&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;return&lt;/span&gt; parentBlogRepository.GetAll();&lt;/pre&gt;

  &lt;pre class="alt"&gt;    },&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;null&lt;/span&gt;, &lt;span class="kwrd"&gt;null&lt;/span&gt;, cacheDependencies)&lt;/pre&gt;

  &lt;pre class="alt"&gt;    .ToList();&lt;/pre&gt;

  &lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;This will add a dependency of the “Blogs” table in the database. The data will remain in the cache until the contents of this table change, then the cache item will be invalidated, and the next call to the GetAll() repository method will be routed to the parent repository to refresh the data from the database.&lt;/p&gt; &lt;img src="http://geekswithblogs.net/Rhames/aggbug/150695.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rhames</dc:creator>
            <guid>http://geekswithblogs.net/Rhames/archive/2012/09/13/adding-sql-cache-dependencies-to-the-loosely-coupled-.net-cache.aspx</guid>
            <pubDate>Thu, 13 Sep 2012 11:24:45 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Rhames/comments/150695.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Rhames/archive/2012/09/13/adding-sql-cache-dependencies-to-the-loosely-coupled-.net-cache.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/Rhames/comments/commentRss/150695.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Rhames/services/trackbacks/150695.aspx</trackback:ping>
        </item>
        <item>
            <title>Loosely coupled .NET Cache Provider using Dependency Injection</title>
            <category>MVC 3 Razor</category>
            <category>Data Caching</category>
            <category>Design Patterns</category>
            <category>ASP.NET</category>
            <category>C#</category>
            <link>http://geekswithblogs.net/Rhames/archive/2012/09/11/loosely-coupled-.net-cache-provider-using-dependency-injection.aspx</link>
            <description>&lt;p&gt;Originally posted on: &lt;a href='http://geekswithblogs.net/Rhames/archive/2012/09/11/loosely-coupled-.net-cache-provider-using-dependency-injection.aspx'&gt;http://geekswithblogs.net/Rhames/archive/2012/09/11/loosely-coupled-.net-cache-provider-using-dependency-injection.aspx&lt;/a&gt;&lt;/p&gt;&lt;p&gt;I have recently been reading the excellent book “Dependency Injection in .NET”, written by Mark Seemann, which I strongly recommend. &lt;/p&gt;  &lt;p&gt;Reading the ideas around Dependency Injection made me realise that the Cache Provider code I wrote about earlier (see &lt;a href="http://geekswithblogs.net/Rhames/archive/2011/01/10/using-the-asp.net-cache-to-cache-data-in-a-model.aspx"&gt;http://geekswithblogs.net/Rhames/archive/2011/01/10/using-the-asp.net-cache-to-cache-data-in-a-model.aspx&lt;/a&gt;) could be refactored to use Dependency Injection, which should produce cleaner code.&lt;/p&gt;  &lt;p&gt;The goals are to:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Separate the cache provider implementation (using the ASP.NET data cache) from the consumers (loose coupling). This will also mean that the dependency on System.Web for the cache provider does not ripple down into the layers where it is being consumed (such as the domain layer). &lt;/li&gt;    &lt;li&gt;Provide a decorator pattern to allow a consumer of the cache provider to be implemented separately from the base consumer (i.e. if we have a base repository, we can decorate this with a caching version). Although I used the term repository, in reality the cache consumer could be just about anything. &lt;/li&gt;    &lt;li&gt;Use constructor injection to provide the Dependency Injection, with a suitable DI container (I use Castle Windsor). &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The sample code for this post is available on github, &lt;a title="https://github.com/RobinHames/CacheProvider.git" href="https://github.com/RobinHames/CacheProvider.git"&gt;https://github.com/RobinHames/CacheProvider.git&lt;/a&gt;&lt;/p&gt;  &lt;h2&gt;ICacheProvider&lt;/h2&gt;  &lt;p&gt;In the sample code, the key interface is ICacheProvider, which is in the domain layer.&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; CacheDiSample.Domain&lt;/pre&gt;

  &lt;pre class="alt"&gt;{&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; ICacheProvider&amp;lt;T&amp;gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;    {&lt;/pre&gt;

  &lt;pre&gt;        T Fetch(&lt;span class="kwrd"&gt;string&lt;/span&gt; key, Func&amp;lt;T&amp;gt; retrieveData, DateTime? absoluteExpiry, TimeSpan? relativeExpiry);&lt;/pre&gt;

  &lt;pre class="alt"&gt;        IEnumerable&amp;lt;T&amp;gt; Fetch(&lt;span class="kwrd"&gt;string&lt;/span&gt; key, Func&amp;lt;IEnumerable&amp;lt;T&amp;gt;&amp;gt; retrieveData, DateTime? absoluteExpiry, TimeSpan? relativeExpiry);&lt;/pre&gt;

  &lt;pre&gt;    }&lt;/pre&gt;

  &lt;pre class="alt"&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;div class="csharpcode"&gt; &lt;/div&gt;

&lt;p&gt;This interface contains two methods to retrieve data from the cache, either as a single instance or as an IEnumerable. the second paramerter is of type Func&amp;lt;T&amp;gt;. This is the method used to retrieve data if nothing is found in the cache.&lt;/p&gt;

&lt;p&gt;The ASP.NET implementation of the ICacheProvider interface needs to live in a project that has a reference to system.web, typically this will be the root UI project, or it could be a separate project. The key thing is that the domain or data access layers do not need system.web references adding to them.&lt;/p&gt;

&lt;p&gt;In my sample MVC application, the CacheProvider is implemented in the UI project, in a folder called “CacheProviders”:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Linq;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Web;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Web.Caching;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; CacheDiSample.Domain;&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; CacheDiSample.CacheProvider&lt;/pre&gt;

  &lt;pre class="alt"&gt;{&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CacheProvider&amp;lt;T&amp;gt; : ICacheProvider&amp;lt;T&amp;gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;    {&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; T Fetch(&lt;span class="kwrd"&gt;string&lt;/span&gt; key, Func&amp;lt;T&amp;gt; retrieveData, DateTime? absoluteExpiry, TimeSpan? relativeExpiry)&lt;/pre&gt;

  &lt;pre class="alt"&gt;        {&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; FetchAndCache&amp;lt;T&amp;gt;(key, retrieveData, absoluteExpiry, relativeExpiry);&lt;/pre&gt;

  &lt;pre class="alt"&gt;        }&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; IEnumerable&amp;lt;T&amp;gt; Fetch(&lt;span class="kwrd"&gt;string&lt;/span&gt; key, Func&amp;lt;IEnumerable&amp;lt;T&amp;gt;&amp;gt; retrieveData, DateTime? absoluteExpiry, TimeSpan? relativeExpiry)&lt;/pre&gt;

  &lt;pre&gt;        {&lt;/pre&gt;

  &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; FetchAndCache&amp;lt;IEnumerable&amp;lt;T&amp;gt;&amp;gt;(key, retrieveData, absoluteExpiry, relativeExpiry);&lt;/pre&gt;

  &lt;pre&gt;        }&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="preproc"&gt;#region&lt;/span&gt; Helper Methods&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; U FetchAndCache&amp;lt;U&amp;gt;(&lt;span class="kwrd"&gt;string&lt;/span&gt; key, Func&amp;lt;U&amp;gt; retrieveData, DateTime? absoluteExpiry, TimeSpan? relativeExpiry)&lt;/pre&gt;

  &lt;pre class="alt"&gt;        {&lt;/pre&gt;

  &lt;pre&gt;            U &lt;span class="kwrd"&gt;value&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (!TryGetValue&amp;lt;U&amp;gt;(key, &lt;span class="kwrd"&gt;out&lt;/span&gt; &lt;span class="kwrd"&gt;value&lt;/span&gt;))&lt;/pre&gt;

  &lt;pre&gt;            {&lt;/pre&gt;

  &lt;pre class="alt"&gt;                &lt;span class="kwrd"&gt;value&lt;/span&gt; = retrieveData();&lt;/pre&gt;

  &lt;pre&gt;                &lt;span class="kwrd"&gt;if&lt;/span&gt; (!absoluteExpiry.HasValue)&lt;/pre&gt;

  &lt;pre class="alt"&gt;                    absoluteExpiry = Cache.NoAbsoluteExpiration;&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;                &lt;span class="kwrd"&gt;if&lt;/span&gt; (!relativeExpiry.HasValue)&lt;/pre&gt;

  &lt;pre&gt;                    relativeExpiry = Cache.NoSlidingExpiration;&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;                HttpContext.Current.Cache.Insert(key, &lt;span class="kwrd"&gt;value&lt;/span&gt;, &lt;span class="kwrd"&gt;null&lt;/span&gt;, absoluteExpiry.Value, relativeExpiry.Value);&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;            }&lt;/pre&gt;

  &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;value&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre&gt;        }&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;bool&lt;/span&gt; TryGetValue&amp;lt;U&amp;gt;(&lt;span class="kwrd"&gt;string&lt;/span&gt; key, &lt;span class="kwrd"&gt;out&lt;/span&gt; U &lt;span class="kwrd"&gt;value&lt;/span&gt;)&lt;/pre&gt;

  &lt;pre class="alt"&gt;        {&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;object&lt;/span&gt; cachedValue = HttpContext.Current.Cache.Get(key);&lt;/pre&gt;

  &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (cachedValue == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;

  &lt;pre&gt;            {&lt;/pre&gt;

  &lt;pre class="alt"&gt;                &lt;span class="kwrd"&gt;value&lt;/span&gt; = &lt;span class="kwrd"&gt;default&lt;/span&gt;(U);&lt;/pre&gt;

  &lt;pre&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;false&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre class="alt"&gt;            }&lt;/pre&gt;

  &lt;pre&gt;            &lt;span class="kwrd"&gt;else&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;            {&lt;/pre&gt;

  &lt;pre&gt;                &lt;span class="kwrd"&gt;try&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;                {&lt;/pre&gt;

  &lt;pre&gt;                    &lt;span class="kwrd"&gt;value&lt;/span&gt; = (U)cachedValue;&lt;/pre&gt;

  &lt;pre class="alt"&gt;                    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;true&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre&gt;                }&lt;/pre&gt;

  &lt;pre class="alt"&gt;                &lt;span class="kwrd"&gt;catch&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;                {&lt;/pre&gt;

  &lt;pre class="alt"&gt;                    &lt;span class="kwrd"&gt;value&lt;/span&gt; = &lt;span class="kwrd"&gt;default&lt;/span&gt;(U);&lt;/pre&gt;

  &lt;pre&gt;                    &lt;span class="kwrd"&gt;return&lt;/span&gt; &lt;span class="kwrd"&gt;false&lt;/span&gt;;&lt;/pre&gt;

  &lt;pre class="alt"&gt;                }&lt;/pre&gt;

  &lt;pre&gt;            }&lt;/pre&gt;

  &lt;pre class="alt"&gt;        }&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="preproc"&gt;#endregion&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;    }&lt;/pre&gt;

  &lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;&lt;style type="text/css"&gt;&lt;![CDATA[



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;The FetchAndCache helper method checks if the specified cache key exists, if it does not, the Func&amp;lt;U&amp;gt; retrieveData method is called, and the results are added to the cache.&lt;/p&gt;

&lt;h2&gt;Using Castle Windsor to register the cache provider&lt;/h2&gt;

&lt;p&gt;In the MVC UI project (my application root), Castle Windsor is used to register the CacheProvider implementation, using a Windsor Installer:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; Castle.MicroKernel.Registration;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; Castle.MicroKernel.SubSystems.Configuration;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; Castle.Windsor;&lt;/pre&gt;

  &lt;pre&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; CacheDiSample.Domain;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; CacheDiSample.CacheProvider;&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; CacheDiSample.WindsorInstallers&lt;/pre&gt;

  &lt;pre class="alt"&gt;{&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; CacheInstaller : IWindsorInstaller&lt;/pre&gt;

  &lt;pre class="alt"&gt;    {&lt;/pre&gt;

  &lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Install(IWindsorContainer container, IConfigurationStore store)&lt;/pre&gt;

  &lt;pre class="alt"&gt;        {&lt;/pre&gt;

  &lt;pre&gt;            container.Register(&lt;/pre&gt;

  &lt;pre class="alt"&gt;                Component.For(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(ICacheProvider&amp;lt;&amp;gt;))&lt;/pre&gt;

  &lt;pre&gt;                .ImplementedBy(&lt;span class="kwrd"&gt;typeof&lt;/span&gt;(CacheProvider&amp;lt;&amp;gt;))&lt;/pre&gt;

  &lt;pre class="alt"&gt;                .LifestyleTransient());&lt;/pre&gt;

  &lt;pre&gt;        }&lt;/pre&gt;

  &lt;pre class="alt"&gt;    }&lt;/pre&gt;

  &lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;div class="csharpcode"&gt; &lt;/div&gt;

&lt;p&gt;Note that the cache provider is registered as a open generic type.&lt;/p&gt;

&lt;h2&gt;Consuming a Repository&lt;/h2&gt;

&lt;p&gt;I have an existing couple of repository interfaces defined in my domain layer:&lt;/p&gt;

&lt;p&gt;&lt;u&gt;IRepository.cs&lt;/u&gt;&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; CacheDiSample.Domain.Model;&lt;/pre&gt;

  &lt;pre class="alt"&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; CacheDiSample.Domain.Repositories&lt;/pre&gt;

  &lt;pre class="alt"&gt;{&lt;/pre&gt;

  &lt;pre&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IRepository&amp;lt;T&amp;gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;where&lt;/span&gt; T : EntityBase&lt;/pre&gt;

  &lt;pre&gt;    {&lt;/pre&gt;

  &lt;pre class="alt"&gt;        T GetById(&lt;span class="kwrd"&gt;int&lt;/span&gt; id);&lt;/pre&gt;

  &lt;pre&gt;        IList&amp;lt;T&amp;gt; GetAll();&lt;/pre&gt;

  &lt;pre class="alt"&gt;    }&lt;/pre&gt;

  &lt;pre&gt;}&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre&gt; &lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;u&gt;IBlogRepository.cs&lt;/u&gt;&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;

    &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; CacheDiSample.Domain.Model;&lt;/pre&gt;

    &lt;pre class="alt"&gt; &lt;/pre&gt;

    &lt;pre&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; CacheDiSample.Domain.Repositories&lt;/pre&gt;

    &lt;pre class="alt"&gt;{&lt;/pre&gt;

    &lt;pre&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;interface&lt;/span&gt; IBlogRepository : IRepository&amp;lt;Blog&amp;gt;&lt;/pre&gt;

    &lt;pre class="alt"&gt;    {&lt;/pre&gt;

    &lt;pre&gt;        Blog GetByName(&lt;span class="kwrd"&gt;string&lt;/span&gt; name);&lt;/pre&gt;

    &lt;pre class="alt"&gt;    }&lt;/pre&gt;

    &lt;pre&gt;}&lt;/pre&gt;
  &lt;/div&gt;
  &lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;These two repositories are implemented in the DataAccess layer, using Entity Framework to retrieve data (this is not important though). One important point is that in the BaseRepository implementation of IRepository, the methods are virtual. This will allow the decorator to override them.&lt;/p&gt;

&lt;p&gt;The BlogRepository is registered in a RepositoriesInstaller, again in the MVC UI project.&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; Castle.MicroKernel.Registration;&lt;/pre&gt;

    &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; Castle.MicroKernel.SubSystems.Configuration;&lt;/pre&gt;

    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; Castle.Windsor;&lt;/pre&gt;

    &lt;pre&gt; &lt;/pre&gt;

    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; CacheDiSample.Domain.CacheDecorators;&lt;/pre&gt;

    &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; CacheDiSample.Domain.Repositories;&lt;/pre&gt;

    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; CacheDiSample.DataAccess;&lt;/pre&gt;

    &lt;pre&gt; &lt;/pre&gt;

    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; CacheDiSample.WindsorInstallers&lt;/pre&gt;

    &lt;pre&gt;{&lt;/pre&gt;

    &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; RepositoriesInstaller : IWindsorInstaller&lt;/pre&gt;

    &lt;pre&gt;    {&lt;/pre&gt;

    &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Install(IWindsorContainer container, IConfigurationStore store)&lt;/pre&gt;

    &lt;pre&gt;        {&lt;/pre&gt;

    &lt;pre class="alt"&gt;            container.Register(Component.For&amp;lt;IBlogRepository&amp;gt;()&lt;/pre&gt;

    &lt;pre&gt;                .ImplementedBy&amp;lt;BlogRepository&amp;gt;()&lt;/pre&gt;

    &lt;pre class="alt"&gt;                .LifestyleTransient()&lt;/pre&gt;

    &lt;pre&gt;                .DependsOn(&lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;/pre&gt;

    &lt;pre class="alt"&gt;                                {&lt;/pre&gt;

    &lt;pre&gt;                                    nameOrConnectionString = &lt;span class="str"&gt;"BloggingContext"&lt;/span&gt;&lt;/pre&gt;

    &lt;pre class="alt"&gt;                                }));&lt;/pre&gt;

    &lt;pre&gt;        }&lt;/pre&gt;

    &lt;pre class="alt"&gt;    }&lt;/pre&gt;

    &lt;pre&gt;}&lt;/pre&gt;
  &lt;/div&gt;
  &lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

  &lt;pre&gt; &lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Now I can inject a dependency on the IBlogRepository into a consumer, such as a controller in my sample code:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;

    &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;

    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Linq;&lt;/pre&gt;

    &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Web;&lt;/pre&gt;

    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Web.Mvc;&lt;/pre&gt;

    &lt;pre&gt; &lt;/pre&gt;

    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; CacheDiSample.Domain.Repositories;&lt;/pre&gt;

    &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; CacheDiSample.Domain.Model;&lt;/pre&gt;

    &lt;pre class="alt"&gt; &lt;/pre&gt;

    &lt;pre&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; CacheDiSample.Controllers&lt;/pre&gt;

    &lt;pre class="alt"&gt;{&lt;/pre&gt;

    &lt;pre&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; HomeController : Controller&lt;/pre&gt;

    &lt;pre class="alt"&gt;    {&lt;/pre&gt;

    &lt;pre&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; &lt;span class="kwrd"&gt;readonly&lt;/span&gt; IBlogRepository blogRepository;&lt;/pre&gt;

    &lt;pre class="alt"&gt; &lt;/pre&gt;

    &lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; HomeController(IBlogRepository blogRepository)&lt;/pre&gt;

    &lt;pre class="alt"&gt;        {&lt;/pre&gt;

    &lt;pre&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (blogRepository == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;

    &lt;pre class="alt"&gt;                &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; ArgumentNullException(&lt;span class="str"&gt;"blogRepository"&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre&gt; &lt;/pre&gt;

    &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;this&lt;/span&gt;.blogRepository = blogRepository;&lt;/pre&gt;

    &lt;pre&gt;        }&lt;/pre&gt;

    &lt;pre class="alt"&gt; &lt;/pre&gt;

    &lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; ActionResult Index()&lt;/pre&gt;

    &lt;pre class="alt"&gt;        {&lt;/pre&gt;

    &lt;pre&gt;            ViewBag.Message = &lt;span class="str"&gt;"Welcome to ASP.NET MVC!"&lt;/span&gt;;&lt;/pre&gt;

    &lt;pre class="alt"&gt; &lt;/pre&gt;

    &lt;pre&gt;            var blogs = blogRepository.GetAll();&lt;/pre&gt;

    &lt;pre class="alt"&gt; &lt;/pre&gt;

    &lt;pre&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; View(&lt;span class="kwrd"&gt;new&lt;/span&gt; Models.HomeModel { Blogs = blogs });&lt;/pre&gt;

    &lt;pre class="alt"&gt;        }&lt;/pre&gt;

    &lt;pre&gt; &lt;/pre&gt;

    &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; ActionResult About()&lt;/pre&gt;

    &lt;pre&gt;        {&lt;/pre&gt;

    &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; View();&lt;/pre&gt;

    &lt;pre&gt;        }&lt;/pre&gt;

    &lt;pre class="alt"&gt;    }&lt;/pre&gt;

    &lt;pre&gt;}&lt;/pre&gt;
  &lt;/div&gt;
  &lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

  &lt;pre&gt; &lt;/pre&gt;
&lt;/div&gt;

&lt;h2&gt;Consuming the Cache Provider via a Decorator&lt;/h2&gt;

&lt;p&gt;I used a Decorator pattern to consume the cache provider, this means my repositories follow the open/closed principle, as they do not require any modifications to implement the caching. It also means that my controllers do not have any knowledge of the caching taking place, as the DI container will simply inject the decorator instead of the root implementation of the repository.&lt;/p&gt;

&lt;p&gt;The first step is to implement a BlogRepository decorator, with the caching logic in it. Note that this can reside in the domain layer, as it does not require any knowledge of the data access methods.&lt;/p&gt;

&lt;p&gt;&lt;u&gt;BlogRepositoryWithCaching.cs&lt;/u&gt;&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System;&lt;/pre&gt;

    &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Collections.Generic;&lt;/pre&gt;

    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Linq;&lt;/pre&gt;

    &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; System.Text;&lt;/pre&gt;

    &lt;pre class="alt"&gt; &lt;/pre&gt;

    &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; CacheDiSample.Domain.Model;&lt;/pre&gt;

    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; CacheDiSample.Domain;&lt;/pre&gt;

    &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; CacheDiSample.Domain.Repositories;&lt;/pre&gt;

    &lt;pre class="alt"&gt; &lt;/pre&gt;

    &lt;pre&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; CacheDiSample.Domain.CacheDecorators&lt;/pre&gt;

    &lt;pre class="alt"&gt;{&lt;/pre&gt;

    &lt;pre&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; BlogRepositoryWithCaching : IBlogRepository&lt;/pre&gt;

    &lt;pre class="alt"&gt;    {&lt;/pre&gt;

    &lt;pre&gt;        &lt;span class="rem"&gt;// The generic cache provider, injected by DI&lt;/span&gt;&lt;/pre&gt;

    &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; ICacheProvider&amp;lt;Blog&amp;gt; cacheProvider;&lt;/pre&gt;

    &lt;pre&gt;        &lt;span class="rem"&gt;// The decorated blog repository, injected by DI&lt;/span&gt;&lt;/pre&gt;

    &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;private&lt;/span&gt; IBlogRepository parentBlogRepository;&lt;/pre&gt;

    &lt;pre&gt; &lt;/pre&gt;

    &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; BlogRepositoryWithCaching(IBlogRepository parentBlogRepository, ICacheProvider&amp;lt;Blog&amp;gt; cacheProvider)&lt;/pre&gt;

    &lt;pre&gt;        {&lt;/pre&gt;

    &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (parentBlogRepository == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;

    &lt;pre&gt;                &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; ArgumentNullException(&lt;span class="str"&gt;"parentBlogRepository"&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre class="alt"&gt; &lt;/pre&gt;

    &lt;pre&gt;            &lt;span class="kwrd"&gt;this&lt;/span&gt;.parentBlogRepository = parentBlogRepository;&lt;/pre&gt;

    &lt;pre class="alt"&gt; &lt;/pre&gt;

    &lt;pre&gt;            &lt;span class="kwrd"&gt;if&lt;/span&gt; (cacheProvider == &lt;span class="kwrd"&gt;null&lt;/span&gt;)&lt;/pre&gt;

    &lt;pre class="alt"&gt;                &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; ArgumentNullException(&lt;span class="str"&gt;"cacheProvider"&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre&gt; &lt;/pre&gt;

    &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;this&lt;/span&gt;.cacheProvider = cacheProvider;&lt;/pre&gt;

    &lt;pre&gt;        }&lt;/pre&gt;

    &lt;pre class="alt"&gt; &lt;/pre&gt;

    &lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; Blog GetByName(&lt;span class="kwrd"&gt;string&lt;/span&gt; name)&lt;/pre&gt;

    &lt;pre class="alt"&gt;        {&lt;/pre&gt;

    &lt;pre&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; key = &lt;span class="kwrd"&gt;string&lt;/span&gt;.Format(&lt;span class="str"&gt;"CacheDiSample.DataAccess.GetByName.{0}"&lt;/span&gt;, name);&lt;/pre&gt;

    &lt;pre class="alt"&gt;            &lt;span class="rem"&gt;// hard code 5 minute expiry!&lt;/span&gt;&lt;/pre&gt;

    &lt;pre&gt;            TimeSpan relativeCacheExpiry = &lt;span class="kwrd"&gt;new&lt;/span&gt; TimeSpan(0, 5, 0);&lt;/pre&gt;

    &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; cacheProvider.Fetch(key, () =&amp;gt;&lt;/pre&gt;

    &lt;pre&gt;            {&lt;/pre&gt;

    &lt;pre class="alt"&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; parentBlogRepository.GetByName(name);&lt;/pre&gt;

    &lt;pre&gt;            },&lt;/pre&gt;

    &lt;pre class="alt"&gt;                &lt;span class="kwrd"&gt;null&lt;/span&gt;, relativeCacheExpiry);&lt;/pre&gt;

    &lt;pre&gt;        }&lt;/pre&gt;

    &lt;pre class="alt"&gt; &lt;/pre&gt;

    &lt;pre&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; Blog GetById(&lt;span class="kwrd"&gt;int&lt;/span&gt; id)&lt;/pre&gt;

    &lt;pre class="alt"&gt;        {&lt;/pre&gt;

    &lt;pre&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; key = &lt;span class="kwrd"&gt;string&lt;/span&gt;.Format(&lt;span class="str"&gt;"CacheDiSample.DataAccess.GetById.{0}"&lt;/span&gt;, id);&lt;/pre&gt;

    &lt;pre class="alt"&gt; &lt;/pre&gt;

    &lt;pre&gt;            &lt;span class="rem"&gt;// hard code 5 minute expiry!&lt;/span&gt;&lt;/pre&gt;

    &lt;pre class="alt"&gt;            TimeSpan relativeCacheExpiry = &lt;span class="kwrd"&gt;new&lt;/span&gt; TimeSpan(0, 5, 0);&lt;/pre&gt;

    &lt;pre&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; cacheProvider.Fetch(key, () =&amp;gt;&lt;/pre&gt;

    &lt;pre class="alt"&gt;            {&lt;/pre&gt;

    &lt;pre&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; parentBlogRepository.GetById(id);&lt;/pre&gt;

    &lt;pre class="alt"&gt;            },&lt;/pre&gt;

    &lt;pre&gt;                &lt;span class="kwrd"&gt;null&lt;/span&gt;, relativeCacheExpiry);&lt;/pre&gt;

    &lt;pre class="alt"&gt;        }&lt;/pre&gt;

    &lt;pre&gt; &lt;/pre&gt;

    &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; IList&amp;lt;Blog&amp;gt; GetAll()&lt;/pre&gt;

    &lt;pre&gt;        {&lt;/pre&gt;

    &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;string&lt;/span&gt; key = &lt;span class="kwrd"&gt;string&lt;/span&gt;.Format(&lt;span class="str"&gt;"CacheDiSample.DataAccess.GetAll"&lt;/span&gt;);&lt;/pre&gt;

    &lt;pre&gt; &lt;/pre&gt;

    &lt;pre class="alt"&gt;            &lt;span class="rem"&gt;// hard code 5 minute expiry!&lt;/span&gt;&lt;/pre&gt;

    &lt;pre&gt;            TimeSpan relativeCacheExpiry = &lt;span class="kwrd"&gt;new&lt;/span&gt; TimeSpan(0, 5, 0);&lt;/pre&gt;

    &lt;pre class="alt"&gt;            &lt;span class="kwrd"&gt;return&lt;/span&gt; cacheProvider.Fetch(key, () =&amp;gt;&lt;/pre&gt;

    &lt;pre&gt;            {&lt;/pre&gt;

    &lt;pre class="alt"&gt;                &lt;span class="kwrd"&gt;return&lt;/span&gt; parentBlogRepository.GetAll();&lt;/pre&gt;

    &lt;pre&gt;            },&lt;/pre&gt;

    &lt;pre class="alt"&gt;                &lt;span class="kwrd"&gt;null&lt;/span&gt;, relativeCacheExpiry)&lt;/pre&gt;

    &lt;pre&gt;            .ToList();&lt;/pre&gt;

    &lt;pre class="alt"&gt;        }&lt;/pre&gt;

    &lt;pre&gt;    }&lt;/pre&gt;

    &lt;pre class="alt"&gt;}&lt;/pre&gt;
  &lt;/div&gt;
  &lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;The key things in this caching repository are:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;I inject into the repository the ICacheProvider&amp;lt;Blog&amp;gt; implementation, via the constructor. This will make the cache provider functionality available to the repository. &lt;/li&gt;

  &lt;li&gt;I inject the parent IBlogRepository implementation (which has the actual data access code), via the constructor. This will allow the methods implemented in the parent to be called if nothing is found in the cache. &lt;/li&gt;

  &lt;li&gt;I override each of the methods implemented in the repository, including those implemented in the generic BaseRepository. Each override of these methods follows the same pattern. It makes a call to the CacheProvider.Fetch method, and passes in the parentBlogRepository implementation of the method as the retrieval method, to be used if nothing is present in the cache. &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;Configuring the Caching Repository in the DI Container&lt;/h2&gt;

&lt;p&gt;The final piece of the jigsaw is to tell Castle Windsor to use the BlogRepositoryWithCaching implementation of IBlogRepository, but to inject the actual Data Access implementation into this decorator. This is easily achieved by modifying the RepositoriesInstaller to use Windsor’s implicit decorator wiring:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;div class="csharpcode"&gt;
    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; Castle.MicroKernel.Registration;&lt;/pre&gt;

    &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; Castle.MicroKernel.SubSystems.Configuration;&lt;/pre&gt;

    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; Castle.Windsor;&lt;/pre&gt;

    &lt;pre&gt; &lt;/pre&gt;

    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; CacheDiSample.Domain.CacheDecorators;&lt;/pre&gt;

    &lt;pre&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; CacheDiSample.Domain.Repositories;&lt;/pre&gt;

    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; CacheDiSample.DataAccess;&lt;/pre&gt;

    &lt;pre&gt; &lt;/pre&gt;

    &lt;pre class="alt"&gt;&lt;span class="kwrd"&gt;namespace&lt;/span&gt; CacheDiSample.WindsorInstallers&lt;/pre&gt;

    &lt;pre&gt;{&lt;/pre&gt;

    &lt;pre class="alt"&gt;    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; RepositoriesInstaller : IWindsorInstaller&lt;/pre&gt;

    &lt;pre&gt;    {&lt;/pre&gt;

    &lt;pre class="alt"&gt;        &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Install(IWindsorContainer container, IConfigurationStore store)&lt;/pre&gt;

    &lt;pre&gt;        {&lt;/pre&gt;

    &lt;pre class="alt"&gt; &lt;/pre&gt;

    &lt;pre&gt;            &lt;span class="rem"&gt;// Use Castle Windsor implicit wiring for the blog repository decorator&lt;/span&gt;&lt;/pre&gt;

    &lt;pre class="alt"&gt;            &lt;span class="rem"&gt;// Register the outermost decorator first&lt;/span&gt;&lt;/pre&gt;

    &lt;pre&gt;            container.Register(Component.For&amp;lt;IBlogRepository&amp;gt;()&lt;/pre&gt;

    &lt;pre class="alt"&gt;                .ImplementedBy&amp;lt;BlogRepositoryWithCaching&amp;gt;()&lt;/pre&gt;

    &lt;pre&gt;                .LifestyleTransient());&lt;/pre&gt;

    &lt;pre class="alt"&gt;            &lt;span class="rem"&gt;// Next register the IBlogRepository implementation to inject into the outer decorator&lt;/span&gt;&lt;/pre&gt;

    &lt;pre&gt;            container.Register(Component.For&amp;lt;IBlogRepository&amp;gt;()&lt;/pre&gt;

    &lt;pre class="alt"&gt;                .ImplementedBy&amp;lt;BlogRepository&amp;gt;()&lt;/pre&gt;

    &lt;pre&gt;                .LifestyleTransient()&lt;/pre&gt;

    &lt;pre class="alt"&gt;                .DependsOn(&lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;/pre&gt;

    &lt;pre&gt;                                {&lt;/pre&gt;

    &lt;pre class="alt"&gt;                                    nameOrConnectionString = &lt;span class="str"&gt;"BloggingContext"&lt;/span&gt;&lt;/pre&gt;

    &lt;pre&gt;                                }));&lt;/pre&gt;

    &lt;pre class="alt"&gt;        }&lt;/pre&gt;

    &lt;pre&gt;    }&lt;/pre&gt;

    &lt;pre class="alt"&gt;}&lt;/pre&gt;
  &lt;/div&gt;
  &lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[



.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;This is all that is needed. Now if the consumer of the repository makes a call to the repositories method, it will be routed via the caching mechanism. You can test this by stepping through the code, and seeing that the DataAccess.BlogRepository code is only called if there is no data in the cache, or this has expired.&lt;/p&gt;

&lt;p&gt;The next step is to add the SQL Cache Dependency support into this pattern, this will be a future post.&lt;/p&gt; &lt;img src="http://geekswithblogs.net/Rhames/aggbug/150672.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rhames</dc:creator>
            <guid>http://geekswithblogs.net/Rhames/archive/2012/09/11/loosely-coupled-.net-cache-provider-using-dependency-injection.aspx</guid>
            <pubDate>Tue, 11 Sep 2012 13:11:10 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Rhames/comments/150672.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Rhames/archive/2012/09/11/loosely-coupled-.net-cache-provider-using-dependency-injection.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/Rhames/comments/commentRss/150672.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Rhames/services/trackbacks/150672.aspx</trackback:ping>
        </item>
        <item>
            <title>Using JQuery Ajax method to pass Json to a MVC3 Action and return a partial view</title>
            <category>ASP.NET</category>
            <category>C#</category>
            <category>MVC 3 Razor</category>
            <category>JQuery</category>
            <link>http://geekswithblogs.net/Rhames/archive/2012/08/06/using-jquery-ajax-method-to-pass-json-to-a-mvc3.aspx</link>
            <description>&lt;p&gt;Originally posted on: &lt;a href='http://geekswithblogs.net/Rhames/archive/2012/08/06/using-jquery-ajax-method-to-pass-json-to-a-mvc3.aspx'&gt;http://geekswithblogs.net/Rhames/archive/2012/08/06/using-jquery-ajax-method-to-pass-json-to-a-mvc3.aspx&lt;/a&gt;&lt;/p&gt;&lt;p&gt; &lt;/p&gt;  &lt;p&gt;ASP.NET MVC3 provides out of the box support for binding a Json data object into a Model on postback. This maybe used with a JQuery Ajax function call to post selected data back to an action. It is also possible to return a partial view from the same action, and refresh this from the ajax success function. For example:&lt;/p&gt;  &lt;p&gt;In my MVC3 Razor project, I create two model classes:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;public class NameResponseModel      &lt;br /&gt;{       &lt;br /&gt;    public string Name { get; set; }       &lt;br /&gt;    public DateTime CurrentDateTime { get; set; }       &lt;br /&gt;    public IList&amp;lt;int&amp;gt; Numbers { get; set; }       &lt;br /&gt;}&lt;/p&gt;    &lt;p&gt;public class UpdateNameModel      &lt;br /&gt;{       &lt;br /&gt;    public string Name { get; set; }       &lt;br /&gt;    public IEnumerable&amp;lt;int&amp;gt; Numbers { get; set; }       &lt;br /&gt;}       &lt;br /&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;The UpdateNameModel will be used to retrieve data submitted by the Ajax method call. It is this model that the Json object will bind to. The NameResponseModel class is used to pass information back to the UI via a template view.&lt;/p&gt;  &lt;p&gt;In the Views/Shared/DisplayTemplates folder, I created a template that is strongly typed to the NameResponseModel class, called NameResponseModel.cshtml:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;@model MvcJQuery.Models.NameResponseModel&lt;/p&gt;    &lt;p&gt;&amp;lt;div&amp;gt;      &lt;br /&gt;&amp;lt;div class="display-label"&amp;gt;       &lt;br /&gt;    Name: @Html.DisplayFor(m =&amp;gt; m.Name)       &lt;br /&gt;&amp;lt;/div&amp;gt;       &lt;br /&gt;&amp;lt;div class="display-label"&amp;gt;       &lt;br /&gt;    Current Date Time: @Html.DisplayFor(m =&amp;gt; m.CurrentDateTime)       &lt;br /&gt;&amp;lt;/div&amp;gt;       &lt;br /&gt;@{       &lt;br /&gt;    var numbersCount = Model.Numbers.Count;       &lt;br /&gt;    &lt;br /&gt;    for(int numberIndex = 0; numberIndex &amp;lt; numbersCount; numberIndex++)       &lt;br /&gt;    {       &lt;br /&gt;        &amp;lt;div class="display-label"&amp;gt;       &lt;br /&gt;            Number: @numberIndex = @Html.DisplayFor(m =&amp;gt; m.Numbers[numberIndex])       &lt;br /&gt;        &amp;lt;/div&amp;gt;       &lt;br /&gt;    }       &lt;br /&gt;}       &lt;br /&gt;&amp;lt;/div&amp;gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;This template simply displays the contents of the associated NameResponseModel object.&lt;/p&gt;  &lt;p&gt;Next, in the Home Controller, I added the following action:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;public ActionResult UpdateName(UpdateNameModel updateNameModel)      &lt;br /&gt;{       &lt;br /&gt;    return PartialView("DisplayTemplates/NameResponseModel", new NameResponseModel       &lt;br /&gt;    {       &lt;br /&gt;        Name = updateNameModel.Name,       &lt;br /&gt;        CurrentDateTime = DateTime.Now,       &lt;br /&gt;        Numbers = updateNameModel.Numbers.ToList()       &lt;br /&gt;    });       &lt;br /&gt;}       &lt;br /&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;This action takes a UpdateNameModel object as a parameter and simply copies this into a new instance of the NameResponseModel. It then returns the display template as a partial view.&lt;/p&gt;  &lt;p&gt;Finally, my Home/Index.cshtml view looks like this:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;@{      &lt;br /&gt;    ViewBag.Title = "Home Page";       &lt;br /&gt;}&lt;/p&gt;    &lt;p&gt;&amp;lt;script type="text/javascript"&amp;gt;      &lt;br /&gt;    $(function () {       &lt;br /&gt;        $('#UpdateName').click(function () {       &lt;br /&gt;            var inputName = $('#Name').val();       &lt;br /&gt;            var dataResponse = { Name: inputName, Numbers: [1, 45, 67, 89] };       &lt;br /&gt;            $.ajax({       &lt;br /&gt;                type: 'POST',       &lt;br /&gt;                contentType: 'application/json; charset=utf-8',       &lt;br /&gt;                data: JSON.stringify(dataResponse),       &lt;br /&gt;                dataType: 'html',       &lt;br /&gt;                url: '@Url.Action("UpdateName")',       &lt;br /&gt;                success: function(result) {       &lt;br /&gt;                    $('#Response').html(result);       &lt;br /&gt;                }       &lt;br /&gt;            });       &lt;br /&gt;        });       &lt;br /&gt;    });       &lt;br /&gt;&amp;lt;/script&amp;gt;&lt;/p&gt;    &lt;p&gt;     &lt;br /&gt;&amp;lt;h2&amp;gt;@ViewBag.Message&amp;lt;/h2&amp;gt;       &lt;br /&gt;&amp;lt;p&amp;gt;       &lt;br /&gt;    To learn more about ASP.NET MVC visit &amp;lt;a href="&lt;a href="http://asp.net/mvc&amp;quot;"&gt;http://asp.net/mvc"&lt;/a&gt; title="ASP.NET MVC Website"&amp;gt;&lt;a href="http://asp.net/mvc"&gt;http://asp.net/mvc&lt;/a&gt;&amp;lt;/a&amp;gt;.       &lt;br /&gt;&amp;lt;/p&amp;gt;&lt;/p&gt;    &lt;p&gt;&amp;lt;div id="Response"&amp;gt;&amp;lt;/div&amp;gt;&lt;/p&gt;    &lt;p&gt;&amp;lt;input type="text" id="Name" /&amp;gt;      &lt;br /&gt;&amp;lt;button type="button" id="UpdateName"&amp;gt;Update&amp;lt;/button&amp;gt;       &lt;br /&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;I’ve added a div with the Id=”Response”. This will be used to display the partial view following an Ajax update. When the “Update” button is clicked, the click event creates a Json object with the contents of the “Name” text input and an array of integers. Note that the names of the Json items must match exactly the names within the data model that the Json will be bound to. MVC3 will quite happily handle complex data binding, so the array of integers will be bound successfully to the Numbers IEnumerable&amp;lt;int&amp;gt; parameter in the UpdateNameModel class.&lt;/p&gt;  &lt;p&gt;Other points to note are the contentType parameter of the Ajax call is set to 'application/json; charset=utf-8'. This indicates that the parameter passed by the Ajax call will be a Json object. The Json object itself needs to be converted to a string, JSON.stringify will perform this function for us. The dataType parameter in the Ajax call is set to ‘html’. This indicates that the data returned by the server is expected to be html (the rendered partial view in this case). The success callback then simply loads this returned html into the “Response” div.&lt;/p&gt;  &lt;p&gt;Hope this helps somebody! It took me a while, and a lot of googling, to figure it out!&lt;/p&gt; &lt;img src="http://geekswithblogs.net/Rhames/aggbug/150371.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rhames</dc:creator>
            <guid>http://geekswithblogs.net/Rhames/archive/2012/08/06/using-jquery-ajax-method-to-pass-json-to-a-mvc3.aspx</guid>
            <pubDate>Mon, 06 Aug 2012 21:23:52 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Rhames/comments/150371.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Rhames/archive/2012/08/06/using-jquery-ajax-method-to-pass-json-to-a-mvc3.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/Rhames/comments/commentRss/150371.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Rhames/services/trackbacks/150371.aspx</trackback:ping>
        </item>
        <item>
            <title>Data Caching Architecture: Using the ASP.NET Cache to cache data in a Model or Business Object layer, without a dependency on System.Web in the layer - Part Two. Adding Cache Dependency Support</title>
            <category>Design Patterns</category>
            <category>Data Caching</category>
            <category>C#</category>
            <category>ASP.NET</category>
            <link>http://geekswithblogs.net/Rhames/archive/2011/01/26/data-caching-architecture-using-the-asp.net-cache-to-cache-data.aspx</link>
            <description>&lt;p&gt;Originally posted on: &lt;a href='http://geekswithblogs.net/Rhames/archive/2011/01/26/data-caching-architecture-using-the-asp.net-cache-to-cache-data.aspx'&gt;http://geekswithblogs.net/Rhames/archive/2011/01/26/data-caching-architecture-using-the-asp.net-cache-to-cache-data.aspx&lt;/a&gt;&lt;/p&gt;&lt;div style="margin: 10pt 0cm 0pt"&gt;&lt;b&gt;&lt;font size="5"&gt;&lt;font size="4"&gt;&lt;font color="#4f81bd"&gt;Adding Cache Dependency Support&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;This second part of my article on adding cache support to applications will extend the sample application developed in part one to add support for cache dependencies such as the SqlCacheDependency.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;Part One of this article is at:&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;&lt;a href="http://geekswithblogs.net/Rhames/archive/2011/01/10/using-the-asp.net-cache-to-cache-data-in-a-model.aspx"&gt;http://geekswithblogs.net/Rhames/archive/2011/01/10/using-the-asp.net-cache-to-cache-data-in-a-model.aspx&lt;/a&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;&lt;em&gt;&lt;font color="#4f81bd"&gt;ICacheDependency Interface&lt;/font&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;We will need to pass cache dependencies to the Cache Provider implementation, so the first step is to create an empty interface called ICacheDependency in the CacheSample.Caching project:&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;namespace&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; CacheSample.Caching&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;interface&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ICacheDependency&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;We can then add overloaded definitions for the Fetch methods in the ICacheProvider&amp;lt;T&amp;gt; interface that will take instances of ICacheDependency. As there could be more than one dependency, the overloads will have a parameter of type IEnumerable&amp;lt;ICacheDependency&amp;gt;, as shown below:&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;public&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; &lt;span style="color: blue"&gt;interface&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ICacheProvider&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="color: #2b91af"&gt;ICacheProvider&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    T Fetch(&lt;span style="color: blue"&gt;string&lt;/span&gt; key, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T&amp;gt; retrieveData, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt"&gt;&lt;span style="color: #2b91af; font-size: 9.5pt"&gt;DateTime&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;? absoluteExpiry, &lt;span style="color: #2b91af"&gt;TimeSpan&lt;/span&gt;? relativeExpiry);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    T Fetch(&lt;span style="color: blue"&gt;string&lt;/span&gt; key, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T&amp;gt; retrieveData, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt"&gt;&lt;span style="color: #2b91af; font-size: 9.5pt"&gt;DateTime&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;? absoluteExpiry, &lt;span style="color: #2b91af"&gt;TimeSpan&lt;/span&gt;? relativeExpiry, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt"&gt;&lt;span style="color: #2b91af; font-size: 9.5pt"&gt;IEnumerable&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ICacheDependency&lt;/span&gt;&amp;gt; cacheDependencies);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; Fetch(&lt;span style="color: blue"&gt;string&lt;/span&gt; key, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt; retrieveData, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt"&gt;&lt;span style="color: #2b91af; font-size: 9.5pt"&gt;DateTime&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;? absoluteExpiry, &lt;span style="color: #2b91af"&gt;TimeSpan&lt;/span&gt;? relativeExpiry);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; Fetch(&lt;span style="color: blue"&gt;string&lt;/span&gt; key, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt; retrieveData, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt"&gt;&lt;span style="color: #2b91af; font-size: 9.5pt"&gt;DateTime&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;? absoluteExpiry, &lt;span style="color: #2b91af"&gt;TimeSpan&lt;/span&gt;? relativeExpiry, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt"&gt;&lt;span style="color: #2b91af; font-size: 9.5pt"&gt;IEnumerable&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ICacheDependency&lt;/span&gt;&amp;gt; cacheDependencies);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;Each type of cache dependency will have its own set of parameters, and therefore will require its own interface, which will then be implemented by the cache provider. For this sample I will implement a SQL cache dependency and a Key cache dependency (which gives a dependency on other cache items, identified by a set of cache keys). For these two types of cache dependency, there will be two interfaces, both of which will inherit from ICacheDependency:&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;namespace&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; CacheSample.Caching&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;interface&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ISqlCacheDependency&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;ICacheDependency&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;string&lt;/span&gt; DatabaseConnectionName { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;string&lt;/span&gt; TableName { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        System.Data.SqlClient.&lt;span style="color: #2b91af"&gt;SqlCommand&lt;/span&gt; SqlCommand { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;namespace&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; CacheSample.Caching&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;interface&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IKeyCacheDependency&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;ICacheDependency&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;string&lt;/span&gt;[] Keys { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="margin: 10pt 0cm 0pt"&gt;&lt;b&gt;&lt;font size="4"&gt;&lt;font color="#4f81bd"&gt;&lt;font size="3"&gt;Dependency Injection of Cache Dependency Implementations&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;We will need to configure the implementations of each cache dependency interface in the application configuration file so that dependency injection may be used to create instances of the dependencies at runtime. For this we will need an additional custom configuration element with properties for the interface being implemented and the type of the class that implements the interface. As there may be several cache dependency implementations there will also be an associated configuration collection.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;&lt;em&gt;&lt;font color="#4f81bd"&gt;CacheDependency Custom Configurations&lt;/font&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;In the CacheSample.Caching project, create a class called CacheDependencyConfigurationElement, in the Configuration folder. The C# code for this class is shown below:&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; System;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; System.Configuration;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;namespace&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; CacheSample.Caching.Configuration&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;CacheDependencyConfigurationElement&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;ConfigurationElement&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        [&lt;span style="color: #2b91af"&gt;ConfigurationProperty&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"interface"&lt;/span&gt;, IsRequired = &lt;span style="color: blue"&gt;true&lt;/span&gt;)]&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; CacheDependencyInterface&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;get&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                &lt;span style="color: blue"&gt;return&lt;/span&gt; (&lt;span style="color: blue"&gt;string&lt;/span&gt;)&lt;span style="color: blue"&gt;this&lt;/span&gt;[&lt;span style="color: #a31515"&gt;"interface"&lt;/span&gt;];&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        [&lt;span style="color: #2b91af"&gt;ConfigurationProperty&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"type"&lt;/span&gt;, IsRequired = &lt;span style="color: blue"&gt;true&lt;/span&gt;)]&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; CacheDependencyType&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;get&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                &lt;span style="color: blue"&gt;return&lt;/span&gt; (&lt;span style="color: blue"&gt;string&lt;/span&gt;)&lt;span style="color: blue"&gt;this&lt;/span&gt;[&lt;span style="color: #a31515"&gt;"type"&lt;/span&gt;];&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;Create a second class called CacheDependencyConfigurationElementCollection, with the following C# code:&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; System;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; System.Configuration;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;namespace&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; CacheSample.Caching.Configuration&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;CacheDependencyConfigurationElementCollection&lt;/span&gt; : &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt"&gt;&lt;span style="color: #2b91af; font-size: 9.5pt"&gt;ConfigurationElementCollection&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;protected&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; ElementName&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;get&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: #a31515"&gt;"cacheDependency"&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;protected&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ConfigurationElement&lt;/span&gt; CreateNewElement()&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;CacheDependencyConfigurationElement&lt;/span&gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ConfigurationElementCollectionType&lt;/span&gt; CollectionType&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;get&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ConfigurationElementCollectionType&lt;/span&gt;.BasicMap;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;protected&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: blue"&gt;object&lt;/span&gt; GetElementKey(&lt;span style="color: #2b91af"&gt;ConfigurationElement&lt;/span&gt; element)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;return&lt;/span&gt; ((&lt;span style="color: #2b91af"&gt;CacheDependencyConfigurationElement&lt;/span&gt;)element).&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 36pt"&gt;&lt;span style="font-size: 9.5pt"&gt;CacheDependencyInterface;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;CacheDependencyConfigurationElement&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;[&lt;span style="color: blue"&gt;int&lt;/span&gt; index]&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;get&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                &lt;span style="color: blue"&gt;return&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;CacheDependencyConfigurationElement&lt;/span&gt;)BaseGet(index);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;CacheDependencyConfigurationElement&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;[&lt;span style="color: blue"&gt;string&lt;/span&gt; Interface]&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;get&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                &lt;span style="color: blue"&gt;return&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;CacheDependencyConfigurationElement&lt;/span&gt;)BaseGet(Interface);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;Finally to complete the custom configuration code, add the following property to the CacheProviderConfigurationSection class:&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;[&lt;span style="color: #2b91af"&gt;ConfigurationProperty&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"cacheDependencies"&lt;/span&gt;)]&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;public&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; &lt;span style="color: #2b91af"&gt;CacheDependencyConfigurationElementCollection&lt;/span&gt; CacheDependencies&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: blue"&gt;get&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;return&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;CacheDependencyConfigurationElementCollection&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 36pt"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;base&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;[&lt;span style="color: #a31515"&gt;"cacheDependencies"&lt;/span&gt;];&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="margin: 10pt 0cm 0pt"&gt;&lt;b&gt;&lt;font size="4"&gt;&lt;font color="#4f81bd"&gt;&lt;font size="3"&gt;Cache Dependency Factory&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;Instances of the cache dependency implementations will be retrieved using a factory class, which will function in a similar manner to the Cache Provider Factory. However, there is one key difference between the Cache Dependency Factory and the Cache Provider Factory. Whereas instances of Cache Providers can be reused, we will have to create a new Cache Dependency instance each time the dependency is required, as it will be passed directly into the cache system. We do not want to have to make an expensive call to Activator.CreateInstance() every time, so we need an efficient method of creating our cache dependency instances at runtime. My solution is to provide a CacheDependencyCreator generic class, which has a Create() method that will create a new instance of the relevant ICacheDependency generic type by called new T(). The first time a particular cache dependency is requested, the factory class will create the relevant creator class, and store this in a static dictionary for use in future requests.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;&lt;em&gt;&lt;font color="#4f81bd"&gt;Cache Dependency Creator class&lt;/font&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;Create a class called CacheDependencyCreator in the CacheSample.Caching project, under the CacheDependency folder. The C# code for the CacheDependencyCreator is shown below:&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; System;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;namespace&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; CacheSample.Caching&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; Provide an efficient mechanism for creating new instances &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; of ICacheDependency implementations at runtime&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;CacheDependencyCreator&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; Create a new instance of the relevant ICacheDependency implementation&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;virtual&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ICacheDependency&lt;/span&gt; Create()&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;NotImplementedException&lt;/span&gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; Provide an efficient mechanism for creating new instances &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; of ICacheDependency implementations at runtime&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;typeparam name="T"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The ICacheDependency type&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/typeparam&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: blue"&gt;internal&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;CacheDependencyCreator&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="color: #2b91af"&gt;CacheDependencyCreator&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;where&lt;/span&gt; T : &lt;span style="color: #2b91af"&gt;ICacheDependency&lt;/span&gt;, &lt;span style="color: blue"&gt;new&lt;/span&gt;()&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; Create a new instance of the relevant ICacheDependency implementation&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ICacheDependency&lt;/span&gt; Create()&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; T();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;Note that there is a non-generic parent class for CacheDependencyCreator that will be used as a type for the static dictionary in the Cache Dependency Facotry.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;&lt;em&gt;&lt;font color="#4f81bd"&gt;Cache Dependency Factory class&lt;/font&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;When a request for a cache dependency instance is made, the factory will first check for the relevant CacheDependencyCreator&amp;lt;&amp;gt; object in the dictionary. If it is present, it will call the Create() method on the relevant creator object. If the dictionary does not contain an instance of the relevant creator, one will be created using Activator.CreateInstance().&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;The C# code for the Cache Dependency Factory class is shown below:&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; System;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; System.Collections.Generic;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; System.Linq;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; CacheSample.Caching.Configuration;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;namespace&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; CacheSample.Caching&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;CacheDependencyFactory&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;CacheDependencyCreator&lt;/span&gt;&amp;gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 36pt"&gt;&lt;span style="font-size: 9.5pt"&gt;cacheDependencyCreators = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 108pt"&gt;&lt;span style="color: #2b91af; font-size: 9.5pt"&gt;CacheDependencyCreator&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;&amp;gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;object&lt;/span&gt; syncRoot = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: blue"&gt;object&lt;/span&gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; Create a new cache dependency instance for the cache dependency &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: green"&gt; interface specified by &lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;typeparamref name="T"/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;typeparam name="T"&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;The ICacheDependency interface being &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt"&gt;&lt;span style="color: green; font-size: 9.5pt"&gt; &lt;/span&gt;&lt;span style="color: gray; font-size: 9.5pt"&gt;///&lt;/span&gt;&lt;span style="color: green; font-size: 9.5pt"&gt; requested&lt;/span&gt;&lt;span style="color: gray; font-size: 9.5pt"&gt;&amp;lt;/typeparam&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: gray"&gt;///&lt;/span&gt;&lt;span style="color: gray"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: green"&gt;An instance of the implementation of the cache dependency &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt"&gt;&lt;span style="color: green; font-size: 9.5pt"&gt; &lt;/span&gt;&lt;span style="color: gray; font-size: 9.5pt"&gt;///&lt;/span&gt;&lt;span style="color: green; font-size: 9.5pt"&gt; interface&lt;/span&gt;&lt;span style="color: gray; font-size: 9.5pt"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; T CreateCacheDependency&amp;lt;T&amp;gt;()&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;where&lt;/span&gt; T : &lt;span style="color: #2b91af"&gt;ICacheDependency&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: #2b91af"&gt;ICacheDependency&lt;/span&gt; cacheDependency = &lt;span style="color: blue"&gt;null&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: #2b91af"&gt;Type&lt;/span&gt; typeOfT = &lt;span style="color: blue"&gt;typeof&lt;/span&gt;(T);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;lock&lt;/span&gt; (syncRoot)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                &lt;span style="color: blue"&gt;if&lt;/span&gt; (cacheDependencyCreators.ContainsKey(typeOfT))&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                    &lt;span style="color: green"&gt;// The cacheDependencyCreators dictionary already&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                    &lt;span style="color: green"&gt;// has a creator for this type of cache dependency, &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                    &lt;span style="color: green"&gt;// so create the instance by calling its Create() method&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                    cacheDependency = ((&lt;span style="color: #2b91af"&gt;CacheDependencyCreator&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 108pt"&gt;&lt;span style="font-size: 9.5pt"&gt;cacheDependencyCreators[typeOfT]).Create();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                &lt;span style="color: blue"&gt;else&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                    &lt;span style="color: green"&gt;// Get the cache dependeny configuration for the cache dependency &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; margin: 0cm 0cm 0pt 72pt"&gt;&lt;span style="color: green; font-size: 9.5pt"&gt;      // interface type&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                    &lt;span style="color: blue"&gt;var&lt;/span&gt; cacheDependencyConfiguration = &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 108pt"&gt;&lt;span style="color: #2b91af; font-size: 9.5pt"&gt;CacheProviderConfigurationSection&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;.Current.&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 108pt"&gt;&lt;span style="font-size: 9.5pt"&gt;CacheDependencies[typeOfT.Name];&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                    &lt;span style="color: blue"&gt;if&lt;/span&gt; (cacheDependencyConfiguration != &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                        &lt;span style="color: green"&gt;// Get the type for the implementation of the specified cache &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 72pt"&gt;&lt;span style="color: green; font-size: 9.5pt"&gt;   // dependency interface&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                        &lt;span style="color: blue"&gt;string&lt;/span&gt; strCacheDependencyImplementationType = &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 108pt"&gt;&lt;span style="font-size: 9.5pt"&gt;cacheDependencyConfiguration.CacheDependencyType;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                        &lt;span style="color: #2b91af"&gt;Type&lt;/span&gt; typeOfCacheDependencyImplementation = &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 108pt"&gt;&lt;span style="color: #2b91af; font-size: 9.5pt"&gt;Type&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;.GetType(strCacheDependencyImplementationType);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                        &lt;span style="color: green"&gt;// Get the Type reference for the CacheDependencyCreator &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; margin: 0cm 0cm 0pt 108pt"&gt;&lt;span style="color: green; font-size: 9.5pt"&gt;   // generic class&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                        &lt;span style="color: #2b91af"&gt;Type&lt;/span&gt; typeOfCacheDependencyCreator = &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 108pt"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;typeof&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;(&lt;span style="color: #2b91af"&gt;CacheDependencyCreator&lt;/span&gt;&amp;lt;&amp;gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                        &lt;span style="color: blue"&gt;if&lt;/span&gt; (typeOfCacheDependencyImplementation != &lt;span style="color: blue"&gt;null&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 108pt"&gt;&lt;span style="font-size: 9.5pt"&gt;&amp;amp;&amp;amp; typeOfCacheDependencyCreator != &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                            &lt;span style="color: green"&gt;// Get the type reference for &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 108pt"&gt;&lt;span style="color: green; font-size: 9.5pt"&gt;// CacheDependencyCreator&amp;lt;cache interface implementation&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                            &lt;span style="color: #2b91af"&gt;Type&lt;/span&gt; typeOfCacheDependencyCreatorForImplementationOfT = &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 144pt"&gt;&lt;span style="font-size: 9.5pt"&gt;typeOfCacheDependencyCreator.MakeGenericType(&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 144pt"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;new&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; &lt;span style="color: #2b91af"&gt;Type&lt;/span&gt;[] &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 144pt"&gt;&lt;span style="font-size: 9.5pt"&gt;{ typeOfCacheDependencyImplementation });&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                            &lt;span style="color: blue"&gt;if&lt;/span&gt; (typeOfCacheDependencyCreatorForImplementationOfT &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 144pt"&gt;&lt;span style="font-size: 9.5pt"&gt;!= &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                            {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                                &lt;span style="color: green"&gt;// Create the CacheDependencyCreator&amp;lt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; margin: 0cm 0cm 0pt 144pt"&gt;&lt;span style="color: green; font-size: 9.5pt"&gt;    // cache interface implementation&amp;gt; instance&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                                &lt;span style="color: blue"&gt;var&lt;/span&gt; cacheDependencyCreator = &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 144pt"&gt;&lt;span style="font-size: 9.5pt"&gt;(&lt;span style="color: #2b91af"&gt;CacheDependencyCreator&lt;/span&gt;)&lt;span style="color: #2b91af"&gt;Activator&lt;/span&gt;.CreateInstance(&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 144pt"&gt;&lt;span style="font-size: 9.5pt"&gt;typeOfCacheDependencyCreatorForImplementationOfT);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                                &lt;span style="color: green"&gt;// Add the CacheDependencyCreator&amp;lt;cache interface &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; margin: 0cm 0cm 0pt 144pt"&gt;&lt;span style="color: green; font-size: 9.5pt"&gt;    // implementation&amp;gt; instance to the dictionary&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                                cacheDependencyCreators.Add(&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 144pt"&gt;&lt;span style="font-size: 9.5pt"&gt;typeOfT, cacheDependencyCreator);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                                &lt;span style="color: green"&gt;// Create the Cache Dependency instance&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                                cacheDependency = cacheDependencyCreator.Create();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                            }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;return&lt;/span&gt; (T)cacheDependency;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="margin: 10pt 0cm 0pt"&gt;&lt;b&gt;&lt;font size="5"&gt;&lt;font size="4"&gt;&lt;font color="#4f81bd"&gt;Implementing ICacheDependency interfaces&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;My original intention when implementing the ICacheDependency interfaces was to create classes within the CacheSample.CacheProvider that extend the System.Web.Caching.CacheDependency and System.Web.Caching.SqlCacheDependency classes. However the System.Web.Caching.SqlCacheDependency class is sealed, so my implementation cannot inherit from this.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;My alternative was to create an interface in the CacheSample.CacheProvider project called IAspNetCacheDependency. This interface contains a single method that returns an instance of System.Web.Caching.CacheDependency. My implementations of the ICacheDependency interfaces will also implement this Asp.Net specific interface, so that the Cache Provider implementation can use this method to create the relevant System.Web.Caching.CacheDependency object.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;The C# code for the IAspNetCacheDependency interface is shown below:&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span&gt; &lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; System.Web.Caching;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;namespace&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; CacheSample.CacheProvider&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;interface&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IAspNetCacheDependency&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: #2b91af"&gt;CacheDependency&lt;/span&gt; CreateAspNetCacheDependency();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;&lt;em&gt;&lt;font color="#4f81bd"&gt;AspNetSqlCacheDependency implementation of ISqlCacheDependency&lt;/font&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;Add a class called AspNetSqlCacheDependency to the CacheSample.CacheProvider project to implement both the ISqlCacheDependency and IAspNetCacheDependency interfaces. For ISqlCacheDependency it is simply a matter of implementing the relevant properties. For the IAspNetCacheDependency interface, the CreateAspNetCacheDependency() method is implemented to return an instance of System.Web.Caching.SqlCacheDependency with the appropriate properties set. The C# code for AspNetCacheDependency is shown below:&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; System.Web.Caching;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; CacheSample.Caching;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;namespace&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; CacheSample.CacheProvider&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;AspNetSqlCacheDependency&lt;/span&gt; : &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt"&gt;&lt;span style="color: #2b91af; font-size: 9.5pt"&gt;ISqlCacheDependency&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;, &lt;span style="color: #2b91af"&gt;IAspNetCacheDependency&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;        #region&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; ISqlCacheDependency Members&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; DatabaseConnectionName&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;get&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;set&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; TableName&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;get&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;set&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;public&lt;/span&gt; System.Data.SqlClient.&lt;span style="color: #2b91af"&gt;SqlCommand&lt;/span&gt; SqlCommand&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;get&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;set&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;        #endregion&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;        #region&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; IAspNetCacheDependency Members&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;public&lt;/span&gt; System.Web.Caching.&lt;span style="color: #2b91af"&gt;CacheDependency&lt;/span&gt; CreateAspNetCacheDependency()&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;if&lt;/span&gt; (SqlCommand != &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;SqlCacheDependency&lt;/span&gt;(SqlCommand);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;else&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;SqlCacheDependency&lt;/span&gt;(DatabaseConnectionName, TableName);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;        #endregion&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;&lt;em&gt;&lt;font color="#4f81bd"&gt;AspNetKeyCacheDependency implementation of IKeyCacheDependency&lt;/font&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;A second class called AspNetKeyCacheDependency is created in the CacheSample.CacheProvider project to implement the IKeyCacheDependency interface, using the C# code shown below:&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; System.Web.Caching;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; CacheSample.Caching;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;namespace&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; CacheSample.CacheProvider&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;AspNetKeyCacheDependency&lt;/span&gt; : &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt"&gt;&lt;span style="color: #2b91af; font-size: 9.5pt"&gt;IKeyCacheDependency&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;, &lt;span style="color: #2b91af"&gt;IAspNetCacheDependency&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;        #region&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; IKeyCacheDependency Members&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt;[] Keys&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;get&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;set&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;        #endregion&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;        #region&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; IAspNetCacheDependency Members&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;CacheDependency&lt;/span&gt; CreateAspNetCacheDependency()&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;CacheDependency&lt;/span&gt;(&lt;span style="color: blue"&gt;null&lt;/span&gt;, Keys);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;        #endregion&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="margin: 10pt 0cm 0pt"&gt;&lt;b&gt;&lt;font size="5"&gt;&lt;font size="4"&gt;&lt;font color="#4f81bd"&gt;Implementing the ICacheProvider.Fetch() method overloads&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;The helper method FetchAndCache&amp;lt;U&amp;gt;() in the AspNetCacheProvider class is extended to take an additional IEnumerable&amp;lt;ICacheDependency&amp;gt; parameter. The Fetch method implementations then call this helper method, as shown below:&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; System;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; System.Collections.Generic;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; System.Linq;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; System.Web;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; System.Web.Caching;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; CacheSample.Caching;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;namespace&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; CacheSample.CacheProvider&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;AspNetCacheProvider&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="color: #2b91af"&gt;ICacheProvider&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;        #region&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; ICacheProvider&amp;lt;T&amp;gt; Members&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;public&lt;/span&gt; T Fetch(&lt;span style="color: blue"&gt;string&lt;/span&gt; key, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T&amp;gt; retrieveData, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 36pt"&gt;&lt;span style="color: #2b91af; font-size: 9.5pt"&gt;DateTime&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;? absoluteExpiry, &lt;span style="color: #2b91af"&gt;TimeSpan&lt;/span&gt;? relativeExpiry)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;return&lt;/span&gt; FetchAndCache&amp;lt;T&amp;gt;(key, retrieveData, absoluteExpiry, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 72pt"&gt;&lt;span style="font-size: 9.5pt"&gt;relativeExpiry, &lt;span style="color: blue"&gt;null&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;public&lt;/span&gt; T Fetch(&lt;span style="color: blue"&gt;string&lt;/span&gt; key, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;T&amp;gt; retrieveData, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 36pt"&gt;&lt;span style="color: #2b91af; font-size: 9.5pt"&gt;DateTime&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;? absoluteExpiry, &lt;span style="color: #2b91af"&gt;TimeSpan&lt;/span&gt;? relativeExpiry, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 36pt"&gt;&lt;span style="color: #2b91af; font-size: 9.5pt"&gt;IEnumerable&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ICacheDependency&lt;/span&gt;&amp;gt; cacheDependencies)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;return&lt;/span&gt; FetchAndCache&amp;lt;T&amp;gt;(key, retrieveData, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 72pt"&gt;&lt;span style="font-size: 9.5pt"&gt;absoluteExpiry, relativeExpiry, cacheDependencies);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; Fetch(&lt;span style="color: blue"&gt;string&lt;/span&gt; key, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt; retrieveData, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 36pt"&gt;&lt;span style="color: #2b91af; font-size: 9.5pt"&gt;DateTime&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;? absoluteExpiry, &lt;span style="color: #2b91af"&gt;TimeSpan&lt;/span&gt;? relativeExpiry)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;return&lt;/span&gt; FetchAndCache&amp;lt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt;(key, retrieveData, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 72pt"&gt;&lt;span style="font-size: 9.5pt"&gt;absoluteExpiry, relativeExpiry, &lt;span style="color: blue"&gt;null&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; Fetch(&lt;span style="color: blue"&gt;string&lt;/span&gt; key, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt; retrieveData, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 36pt"&gt;&lt;span style="color: #2b91af; font-size: 9.5pt"&gt;DateTime&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;? absoluteExpiry, &lt;span style="color: #2b91af"&gt;TimeSpan&lt;/span&gt;? relativeExpiry, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 36pt"&gt;&lt;span style="color: #2b91af; font-size: 9.5pt"&gt;IEnumerable&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ICacheDependency&lt;/span&gt;&amp;gt; cacheDependencies)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;return&lt;/span&gt; FetchAndCache&amp;lt;&lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt;(key, retrieveData, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 72pt"&gt;&lt;span style="font-size: 9.5pt"&gt;absoluteExpiry, relativeExpiry, cacheDependencies);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;        #endregion&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;        #region&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; Helper Methods&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;private&lt;/span&gt; U FetchAndCache&amp;lt;U&amp;gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt; key, &lt;span style="color: #2b91af"&gt;Func&lt;/span&gt;&amp;lt;U&amp;gt; retrieveData, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 36pt"&gt;&lt;span style="color: #2b91af; font-size: 9.5pt"&gt;DateTime&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;? absoluteExpiry, &lt;span style="color: #2b91af"&gt;TimeSpan&lt;/span&gt;? relativeExpiry, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 36pt"&gt;&lt;span style="color: #2b91af; font-size: 9.5pt"&gt;IEnumerable&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ICacheDependency&lt;/span&gt;&amp;gt; cacheDependencies)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            U value;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;if&lt;/span&gt; (!TryGetValue&amp;lt;U&amp;gt;(key, &lt;span style="color: blue"&gt;out&lt;/span&gt; value))&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                value = retrieveData();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                &lt;span style="color: blue"&gt;if&lt;/span&gt; (!absoluteExpiry.HasValue)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                    absoluteExpiry = &lt;span style="color: #2b91af"&gt;Cache&lt;/span&gt;.NoAbsoluteExpiration;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                &lt;span style="color: blue"&gt;if&lt;/span&gt; (!relativeExpiry.HasValue)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                    relativeExpiry = &lt;span style="color: #2b91af"&gt;Cache&lt;/span&gt;.NoSlidingExpiration;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                &lt;span style="color: #2b91af"&gt;CacheDependency&lt;/span&gt; aspNetCacheDependencies = &lt;span style="color: blue"&gt;null&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                &lt;span style="color: blue"&gt;if&lt;/span&gt; (cacheDependencies != &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                    &lt;span style="color: blue"&gt;if&lt;/span&gt; (cacheDependencies.Count() == 1)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                        &lt;span style="color: green"&gt;// We know that the implementations of ICacheDependency &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 9.5pt"&gt;                        // will also implement IAspNetCacheDependency&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                        &lt;span style="color: green"&gt;// so we can use a cast here and call the &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 9.5pt"&gt;                        // CreateAspNetCacheDependency() method&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                        aspNetCacheDependencies = &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 108pt"&gt;&lt;span style="font-size: 9.5pt"&gt;((&lt;span style="color: #2b91af"&gt;IAspNetCacheDependency&lt;/span&gt;)cacheDependencies&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 108pt"&gt;&lt;span style="font-size: 9.5pt"&gt;.ElementAt(0)).CreateAspNetCacheDependency();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                    &lt;span style="color: blue"&gt;else&lt;/span&gt; &lt;span style="color: blue"&gt;if&lt;/span&gt;(cacheDependencies.Count() &amp;gt; 1)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                        &lt;span style="color: #2b91af"&gt;AggregateCacheDependency&lt;/span&gt; aggregateCacheDependency = &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 108pt"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;new&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; &lt;span style="color: #2b91af"&gt;AggregateCacheDependency&lt;/span&gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                        &lt;span style="color: blue"&gt;foreach&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;ICacheDependency&lt;/span&gt; cacheDependency &lt;span style="color: blue"&gt;in&lt;/span&gt; cacheDependencies)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                            &lt;span style="color: green"&gt;// We know that the implementations of ICacheDependency &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 9.5pt"&gt;                            // will also implement IAspNetCacheDependency&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                            &lt;span style="color: green"&gt;// so we can use a cast here and call the &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 9.5pt"&gt;                            // CreateAspNetCacheDependency() method&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                            aggregateCacheDependency&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 144pt"&gt;&lt;span style="font-size: 9.5pt"&gt;.Add(((&lt;span style="color: #2b91af"&gt;IAspNetCacheDependency&lt;/span&gt;)cacheDependency)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 144pt"&gt;&lt;span style="font-size: 9.5pt"&gt;.CreateAspNetCacheDependency());&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                        aspNetCacheDependencies = aggregateCacheDependency;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                &lt;span style="color: #2b91af"&gt;HttpContext&lt;/span&gt;.Current.Cache.Insert(key, value, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 72pt"&gt;&lt;span style="font-size: 9.5pt"&gt;aspNetCacheDependencies, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 72pt"&gt;&lt;span style="font-size: 9.5pt"&gt;absoluteExpiry.Value, relativeExpiry.Value);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;return&lt;/span&gt; value;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;bool&lt;/span&gt; TryGetValue&amp;lt;U&amp;gt;(&lt;span style="color: blue"&gt;string&lt;/span&gt; key, &lt;span style="color: blue"&gt;out&lt;/span&gt; U value)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;object&lt;/span&gt; cachedValue = &lt;span style="color: #2b91af"&gt;HttpContext&lt;/span&gt;.Current.Cache.Get(key);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;if&lt;/span&gt; (cachedValue == &lt;span style="color: blue"&gt;null&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                value = &lt;span style="color: blue"&gt;default&lt;/span&gt;(U);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;false&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;else&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                &lt;span style="color: blue"&gt;try&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                    value = (U)cachedValue;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                    &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;true&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                &lt;span style="color: blue"&gt;catch&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                    value = &lt;span style="color: blue"&gt;default&lt;/span&gt;(U);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                    &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;false&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;        #endregion&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;If any cache dependencies are passed to the FetchAndCache&amp;lt;U&amp;gt; method, they are cast to the IAspNetCacheDependency interface, then the CreateAspNetCacheDependency() method is called to create an instance of the relevant System.Web.Caching.CacheDependency class to include in the HttpContext.Current.Cache.Insert() method call.&lt;/div&gt;
&lt;div style="margin: 10pt 0cm 0pt"&gt;&lt;b&gt;&lt;font size="5"&gt;&lt;font size="4"&gt;&lt;font color="#4f81bd"&gt;Specifying the ICacheDependency implementations in Web.Config&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;We need to add a &amp;lt;cacheDependencies&amp;gt; collection to the &amp;lt;cacheProvider&amp;gt; element in the application configuration file, and then add individual &amp;lt;cacheDependency&amp;gt; elements to this collection for each of the ICacheDependency implementations, as show below:&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515; font-size: 9.5pt"&gt;cacheProvider&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt"&gt;&lt;span style="color: red; font-size: 9.5pt"&gt;type&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;=&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;"&lt;span style="color: blue"&gt;CacheSample.CacheProvider.AspNetCacheProvider`1, &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;CacheSample.CacheProvider, Version=1.0.0.0, Culture=neutral, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;PublicKeyToken=null&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;"&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515; font-size: 9.5pt"&gt;cacheDependencies&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;      &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515; font-size: 9.5pt"&gt;cacheDependency&lt;/span&gt;&lt;span style="color: red; font-size: 9.5pt"&gt;interface&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;=&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;"&lt;span style="color: blue"&gt;IKeyCacheDependency&lt;/span&gt;"&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;                       &lt;/span&gt;&lt;span style="color: red; font-size: 9.5pt"&gt;type&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;=&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;"&lt;span style="color: blue"&gt;CacheSample.CacheProvider.AspNetKeyCacheDependency, &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 108pt"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;CacheSample.CacheProvider, Version=1.0.0.0, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 108pt"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;Culture=neutral, PublicKeyToken=null&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;"&lt;span style="color: blue"&gt; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;      &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515; font-size: 9.5pt"&gt;cacheDependency&lt;/span&gt;&lt;span style="color: red; font-size: 9.5pt"&gt;interface&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;=&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;"&lt;span style="color: blue"&gt;ISqlCacheDependency&lt;/span&gt;"&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;                       &lt;/span&gt;&lt;span style="color: red; font-size: 9.5pt"&gt;type&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;=&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;"&lt;span style="color: blue"&gt;CacheSample.CacheProvider.AspNetSqlCacheDependency, &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 108pt"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;CacheSample.CacheProvider, Version=1.0.0.0, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 108pt"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;Culture=neutral, PublicKeyToken=null&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;"&lt;span style="color: blue"&gt; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515; font-size: 9.5pt"&gt;cacheDependencies&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;&amp;gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515; font-size: 9.5pt"&gt;cacheProvider&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;This will allow the CacheDependencyFactory class to use dependency injection to get the implementations of the relevant interfaces at runtime.&lt;/div&gt;
&lt;div style="margin: 10pt 0cm 0pt"&gt;&lt;b&gt;&lt;font size="5"&gt;&lt;font size="4"&gt;&lt;font color="#4f81bd"&gt;Using the ISqlCacheDependency in the Sale.GetSales() method&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;We can now modify the Sale.GetSales() method to include a cache dependency on the Sale table in the database.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;First we create a cache dependency instance of the ISqlCacheDependency interface, using the factory, and set its properties, as shown below:&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;var&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; sqlCacheDependency = CacheSample.Caching.&lt;span style="color: #2b91af"&gt;CacheDependencyFactory&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt"&gt;&lt;span style="font-size: 9.5pt"&gt;.CreateCacheDependency&amp;lt;CacheSample.Caching.&lt;span style="color: #2b91af"&gt;ISqlCacheDependency&lt;/span&gt;&amp;gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;sqlCacheDependency.DatabaseConnectionName = &lt;span style="color: #a31515"&gt;"CacheSample"&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;sqlCacheDependency.TableName = &lt;span style="color: #a31515"&gt;"Sale"&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;We can then pass this cache dependency instance in the call to the CacheProvider Fetch method. The full code for GetSales is shown below:&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;public&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Sale&lt;/span&gt;&amp;gt; GetSales(&lt;span style="color: blue"&gt;int&lt;/span&gt;? highestDayCount)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: blue"&gt;string&lt;/span&gt; cacheKey = &lt;span style="color: blue"&gt;string&lt;/span&gt;.Format(&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt"&gt;&lt;span style="color: #a31515; font-size: 9.5pt"&gt;"CacheSample.BusinessObjects.GetSalesWithCache_SqlCommandDependency({0})"&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt"&gt;&lt;span style="font-size: 9.5pt"&gt;highestDayCount);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: blue"&gt;string&lt;/span&gt; connectionStr = System.Configuration.&lt;span style="color: #2b91af"&gt;ConfigurationManager&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt"&gt;&lt;span style="font-size: 9.5pt"&gt;.ConnectionStrings[&lt;span style="color: #a31515"&gt;"CacheSample"&lt;/span&gt;].ConnectionString;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: blue"&gt;var&lt;/span&gt; sqlCacheDependency = CacheSample.Caching.&lt;span style="color: #2b91af"&gt;CacheDependencyFactory&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt"&gt;&lt;span style="font-size: 9.5pt"&gt;.CreateCacheDependency&amp;lt;CacheSample.Caching.&lt;span style="color: #2b91af"&gt;ISqlCacheDependency&lt;/span&gt;&amp;gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    sqlCacheDependency.DatabaseConnectionName = &lt;span style="color: #a31515"&gt;"CacheSample"&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    sqlCacheDependency.TableName = &lt;span style="color: #a31515"&gt;"Sale"&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    CacheSample.Caching.&lt;span style="color: #2b91af"&gt;ICacheDependency&lt;/span&gt;[] cacheDependencies = &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;new&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; Caching.&lt;span style="color: #2b91af"&gt;ICacheDependency&lt;/span&gt;[] { sqlCacheDependency };&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;    &lt;span style="color: blue"&gt;return&lt;/span&gt; CacheSample.Caching.&lt;span style="color: #2b91af"&gt;CacheProviderFactory&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt"&gt;&lt;span style="font-size: 9.5pt"&gt;.GetCacheProvider&amp;lt;&lt;span style="color: #2b91af"&gt;Sale&lt;/span&gt;&amp;gt;().Fetch(cacheKey,&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;delegate&lt;/span&gt;()&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Sale&lt;/span&gt;&amp;gt; sales = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Sale&lt;/span&gt;&amp;gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: #2b91af"&gt;SqlParameter&lt;/span&gt; highestDayCountParameter = &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt; margin: 0cm 0cm 0pt 36pt"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;new&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt; &lt;span style="color: #2b91af"&gt;SqlParameter&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"@HighestDayCount"&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;SqlDbType&lt;/span&gt;.SmallInt);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;if&lt;/span&gt; (highestDayCount.HasValue)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                highestDayCountParameter.Value = highestDayCount;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;else&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                highestDayCountParameter.Value = &lt;span style="color: #2b91af"&gt;DBNull&lt;/span&gt;.Value;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;SqlConnection&lt;/span&gt; sqlConn = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;SqlConnection&lt;/span&gt;(connectionStr))&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;SqlCommand&lt;/span&gt; sqlCmd = sqlConn.CreateCommand())&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                sqlCmd.CommandText = &lt;span style="color: #a31515"&gt;"spGetRunningTotals"&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                sqlCmd.CommandType = &lt;span style="color: #2b91af"&gt;CommandType&lt;/span&gt;.StoredProcedure;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                sqlCmd.Parameters.Add(highestDayCountParameter);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                sqlConn.Open();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                &lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;SqlDataReader&lt;/span&gt; dr = sqlCmd.ExecuteReader())&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                    &lt;span style="color: blue"&gt;while&lt;/span&gt; (dr.Read())&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                        &lt;span style="color: #2b91af"&gt;Sale&lt;/span&gt; newSale = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Sale&lt;/span&gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                        newSale.DayCount = dr.GetInt16(0);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                        newSale.Sales = dr.GetDecimal(1);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                        newSale.RunningTotal = dr.GetDecimal(2);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                        sales.Add(newSale);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;                }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;            &lt;span style="color: blue"&gt;return&lt;/span&gt; sales;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        },&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;null&lt;/span&gt;,&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        &lt;span style="color: blue"&gt;null&lt;/span&gt;,&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        cacheDependencies&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;        );&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 9.5pt"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;As we only provided a Fetch method in ICacheProvider that takes IEnumerable&amp;lt;ICacheDependency&amp;gt;, we create an array of ICacheDependency and add our SqlCacheDependency object to this. However, it would be simple to provide a Fetch overload that only takes a single ICacheDependency instead.&lt;/div&gt;
&lt;div style="margin: 10pt 0cm 0pt"&gt;&lt;b&gt;&lt;font size="5"&gt;&lt;font size="4"&gt;&lt;font color="#4f81bd"&gt;Configuring the Sql Server and ASP.NET application for SQL Cache Dependency&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;As the code will use the ASP.NET Sql cache dependency polling method (as supported by SQL Server 2000 onwards) we need to carry out some additional configuration.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;Open the Visual Studio command prompt and run the following command to enable caching support in the CacheSample database:&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;aspnet_regsql -S &amp;lt;servername&amp;gt; -E -d CacheSample –ed&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;Next enable the Sale table for caching support by running the following command:&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;aspnet_regsql -S &amp;lt;servername&amp;gt; -E -d CacheSample –et –t Sale&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;We also have to add the following to the web.config file, inside the &amp;lt;system.web&amp;gt; element:&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515; font-size: 9.5pt"&gt;caching&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;      &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515; font-size: 9.5pt"&gt;sqlCacheDependency&lt;/span&gt;&lt;span style="color: red; font-size: 9.5pt"&gt;pollTime&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;=&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;"&lt;span style="color: blue"&gt;10000&lt;/span&gt;"&lt;span style="color: red"&gt;enabled&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;true&lt;/span&gt;"&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515; font-size: 9.5pt"&gt;databases&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;          &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515; font-size: 9.5pt"&gt;add&lt;/span&gt;&lt;span style="color: red; font-size: 9.5pt"&gt;name&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;=&lt;/span&gt;&lt;span style="font-size: 9.5pt"&gt;"&lt;span style="color: blue"&gt;CacheSample&lt;/span&gt;"&lt;span style="color: red"&gt;connectionStringName&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;"&lt;span style="color: blue"&gt;CacheSample&lt;/span&gt;"&lt;span style="color: blue"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;        &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515; font-size: 9.5pt"&gt;databases&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;      &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515; font-size: 9.5pt"&gt;sqlCacheDependency&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515; font-size: 9.5pt"&gt;caching&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt; &lt;/div&gt;
&lt;div style="margin: 10pt 0cm 0pt"&gt;&lt;b&gt;&lt;font size="5"&gt;&lt;font size="4"&gt;&lt;font color="#4f81bd"&gt;Testing the Cache Dependency&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;Using the web page developed in part one, the first time the GetSales() method is called, I get the following result:&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 12pt"&gt;Count of Sales: 4999, Last DayCount: 4999, Total Sales: 187138020.0000. Query took 979 ms&lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;Subsequent calls to GetSales() give the following results. Note that the cache is now beings used, so the time recorded by the StopWatch is 0ms:&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 12pt"&gt;Count of Sales: 4999, Last DayCount: 4999, Total Sales: 187138020.0000. Query took 0 ms &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;Next add a new row to the Sale table:&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;INSERT&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: blue"&gt;INTO&lt;/span&gt; [CacheSample]&lt;span style="color: gray"&gt;.&lt;/span&gt;[dbo]&lt;span style="color: gray"&gt;.&lt;/span&gt;[Sale]&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;           &lt;/span&gt;&lt;span style="color: gray; font-size: 10pt"&gt;(&lt;/span&gt;&lt;span style="font-size: 10pt"&gt;[DayCount]&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;           &lt;span style="color: gray"&gt;,&lt;/span&gt;[Sales]&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;     &lt;span style="color: blue"&gt;VALUES&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;           &lt;/span&gt;&lt;span style="color: gray; font-size: 10pt"&gt;(&lt;/span&gt;&lt;span style="font-size: 10pt"&gt;5000&lt;span style="color: gray"&gt;,&lt;/span&gt; 100&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt;This should invalidate the cache, making the next call to GetSales() get the data from the database again:&lt;/div&gt;
&lt;div style="line-height: normal"&gt;&lt;span style="font-size: 12pt"&gt;Count of Sales: 5000, Last DayCount: 5000, Total Sales: 187138120.0000. Query took 499 ms &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt"&gt; &lt;/div&gt; &lt;img src="http://geekswithblogs.net/Rhames/aggbug/143635.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rhames</dc:creator>
            <guid>http://geekswithblogs.net/Rhames/archive/2011/01/26/data-caching-architecture-using-the-asp.net-cache-to-cache-data.aspx</guid>
            <pubDate>Wed, 26 Jan 2011 10:56:02 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Rhames/comments/143635.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Rhames/archive/2011/01/26/data-caching-architecture-using-the-asp.net-cache-to-cache-data.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/Rhames/comments/commentRss/143635.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Rhames/services/trackbacks/143635.aspx</trackback:ping>
        </item>
        <item>
            <title>Data Caching Architecture: Using the ASP.NET Cache to cache data in a Model or Business Object layer, without a dependency on System.Web in the layer - Part One.</title>
            <category>C#</category>
            <category>ASP.NET</category>
            <category>Design Patterns</category>
            <category>Data Caching</category>
            <link>http://geekswithblogs.net/Rhames/archive/2011/01/10/using-the-asp.net-cache-to-cache-data-in-a-model.aspx</link>
            <description>&lt;p&gt;Originally posted on: &lt;a href='http://geekswithblogs.net/Rhames/archive/2011/01/10/using-the-asp.net-cache-to-cache-data-in-a-model.aspx'&gt;http://geekswithblogs.net/Rhames/archive/2011/01/10/using-the-asp.net-cache-to-cache-data-in-a-model.aspx&lt;/a&gt;&lt;/p&gt;&lt;div style="margin: 0cm 0cm 10pt; line-height: normal;"&gt;ASP.NET applications can make use of the System.Web.Caching.Cache object to cache data and prevent repeated expensive calls to a database or other store. However, ideally an application should make use of caching at the point where data is retrieved from the database, which typically is inside a Business Objects or Model layer. One of the key features of using a UI pattern such as Model-View-Presenter (MVP) or Model-View-Controller (MVC) is that the Model and Presenter (or Controller) layers are developed without any knowledge of the UI layer. Introducing a dependency on System.Web into the Model layer would break this independence of the Model from the View.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt; line-height: normal;"&gt;This article gives a solution to this problem, using dependency injection to inject the caching implementation into the Model layer at runtime. This allows caching to be used within the Model layer, without any knowledge of the actual caching mechanism that will be used.&lt;/div&gt;
&lt;div style="margin: 10pt 0cm 0pt;"&gt;&lt;b&gt;&lt;font size="5"&gt;&lt;font size="4"&gt;&lt;font color="#4f81bd"&gt;Create a sample application to use the caching solution&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;&lt;em&gt;&lt;font color="#4f81bd"&gt;Create a test SQL Server database&lt;/font&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;This solution uses a SQL Server database with the same Sales data used in my previous post on calculating running totals. The advantage of using this data is that it gives nice slow queries that will exaggerate the effect of using caching!&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;To create the data, first create a new SQL database called CacheSample. Next run the following script to create the Sale table and populate it:&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 10pt;"&gt;USE&lt;/span&gt;&lt;span style="font-size: 10pt;"&gt; CacheSample&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 10pt;"&gt;GO&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 10pt;"&gt;CREATE&lt;/span&gt;&lt;span style="font-size: 10pt;"&gt; &lt;span style="color: blue;"&gt;TABLE&lt;/span&gt; Sale&lt;span style="color: gray;"&gt;(&lt;/span&gt;DayCount &lt;span style="color: blue;"&gt;smallint&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt; Sales &lt;span style="color: blue;"&gt;money&lt;/span&gt;&lt;span style="color: gray;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 10pt;"&gt;CREATE&lt;/span&gt;&lt;span style="font-size: 10pt;"&gt; &lt;span style="color: blue;"&gt;CLUSTERED&lt;/span&gt; &lt;span style="color: blue;"&gt;INDEX&lt;/span&gt; ndx_DayCount &lt;span style="color: blue;"&gt;ON&lt;/span&gt; Sale&lt;span style="color: gray;"&gt;(&lt;/span&gt;DayCount&lt;span style="color: gray;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 10pt;"&gt;go&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 10pt;"&gt;INSERT&lt;/span&gt;&lt;span style="font-size: 10pt;"&gt; Sale &lt;span style="color: blue;"&gt;VALUES &lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;1&lt;span style="color: gray;"&gt;,&lt;/span&gt;120&lt;span style="color: gray;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 10pt;"&gt;INSERT&lt;/span&gt;&lt;span style="font-size: 10pt;"&gt; Sale &lt;span style="color: blue;"&gt;VALUES &lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;2&lt;span style="color: gray;"&gt;,&lt;/span&gt;60&lt;span style="color: gray;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 10pt;"&gt;INSERT&lt;/span&gt;&lt;span style="font-size: 10pt;"&gt; Sale &lt;span style="color: blue;"&gt;VALUES &lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;3&lt;span style="color: gray;"&gt;,&lt;/span&gt;125&lt;span style="color: gray;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 10pt;"&gt;INSERT&lt;/span&gt;&lt;span style="font-size: 10pt;"&gt; Sale &lt;span style="color: blue;"&gt;VALUES &lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;4&lt;span style="color: gray;"&gt;,&lt;/span&gt;40&lt;span style="color: gray;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 10pt;"&gt;DECLARE&lt;/span&gt;&lt;span style="font-size: 10pt;"&gt; @DayCount &lt;span style="color: blue;"&gt;smallint&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt; @Sales &lt;span style="color: blue;"&gt;money&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 10pt;"&gt;SET&lt;/span&gt;&lt;span style="font-size: 10pt;"&gt; @DayCount &lt;span style="color: gray;"&gt;=&lt;/span&gt; 5&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 10pt;"&gt;SET&lt;/span&gt;&lt;span style="font-size: 10pt;"&gt; @Sales &lt;span style="color: gray;"&gt;=&lt;/span&gt; 10&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 10pt;"&gt;WHILE&lt;/span&gt;&lt;span style="font-size: 10pt;"&gt; @DayCount &lt;span style="color: gray;"&gt;&amp;lt;&lt;/span&gt; 5000&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt; &lt;span style="color: blue;"&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt; &lt;span style="color: blue;"&gt;INSERT&lt;/span&gt; Sale &lt;span style="color: blue;"&gt;VALUES &lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;@DayCount&lt;span style="color: gray;"&gt;,&lt;/span&gt;@Sales&lt;span style="color: gray;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt; &lt;span style="color: blue;"&gt;SET&lt;/span&gt; @DayCount &lt;span style="color: gray;"&gt;=&lt;/span&gt; @DayCount &lt;span style="color: gray;"&gt;+&lt;/span&gt; 1&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt; &lt;span style="color: blue;"&gt;SET&lt;/span&gt; @Sales &lt;span style="color: gray;"&gt;=&lt;/span&gt; @Sales &lt;span style="color: gray;"&gt;+&lt;/span&gt; 15&lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;&lt;span style="line-height: 115%; font-size: 10pt;"&gt; &lt;span style="color: blue;"&gt;END&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;Next create a stored procedure to calculate the running total, and return a specified number of rows from the Sale table, using the following script:&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 10pt;"&gt;USE&lt;/span&gt;&lt;span style="font-size: 10pt;"&gt; [CacheSample]&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 10pt;"&gt;GO&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 10pt;"&gt;SET&lt;/span&gt;&lt;span style="font-size: 10pt;"&gt; &lt;span style="color: blue;"&gt;ANSI_NULLS&lt;/span&gt; &lt;span style="color: blue;"&gt;ON&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 10pt;"&gt;GO&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 10pt;"&gt;SET&lt;/span&gt;&lt;span style="font-size: 10pt;"&gt; &lt;span style="color: blue;"&gt;QUOTED_IDENTIFIER&lt;/span&gt; &lt;span style="color: blue;"&gt;ON&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 10pt;"&gt;GO&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: green; font-size: 10pt;"&gt;-- =============================================&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: green; font-size: 10pt;"&gt;-- Author:        Robin&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: green; font-size: 10pt;"&gt;-- Create date: &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: green; font-size: 10pt;"&gt;-- Description:   &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: green; font-size: 10pt;"&gt;-- =============================================&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 10pt;"&gt;CREATE&lt;/span&gt;&lt;span style="font-size: 10pt;"&gt; &lt;span style="color: blue;"&gt;PROCEDURE&lt;/span&gt; [dbo]&lt;span style="color: gray;"&gt;.&lt;/span&gt;[spGetRunningTotals] &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;      &lt;span style="color: green;"&gt;-- Add the parameters for the stored procedure here&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;      @HighestDayCount &lt;span style="color: blue;"&gt;smallint&lt;/span&gt; &lt;span style="color: gray;"&gt;=&lt;/span&gt; &lt;span style="color: gray;"&gt;null&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 10pt;"&gt;AS&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 10pt;"&gt;BEGIN&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;      &lt;span style="color: green;"&gt;-- SET NOCOUNT ON added to prevent extra result sets from&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;      &lt;span style="color: green;"&gt;-- interfering with SELECT statements.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;      &lt;span style="color: blue;"&gt;SET&lt;/span&gt; &lt;span style="color: blue;"&gt;NOCOUNT&lt;/span&gt; &lt;span style="color: blue;"&gt;ON&lt;/span&gt;&lt;span style="color: gray;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;      &lt;span style="color: blue;"&gt;IF&lt;/span&gt; @HighestDayCount &lt;span style="color: gray;"&gt;IS&lt;/span&gt; &lt;span style="color: gray;"&gt;NULL&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;            &lt;span style="color: blue;"&gt;SELECT&lt;/span&gt; @HighestDayCount &lt;span style="color: gray;"&gt;=&lt;/span&gt; &lt;span style="color: fuchsia;"&gt;MAX&lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;DayCount&lt;span style="color: gray;"&gt;)&lt;/span&gt; &lt;span style="color: blue;"&gt;FROM&lt;/span&gt; dbo&lt;span style="color: gray;"&gt;.&lt;/span&gt;Sale&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;            &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;      &lt;span style="color: blue;"&gt;DECLARE&lt;/span&gt; @SaleTbl &lt;span style="color: blue;"&gt;TABLE &lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;DayCount &lt;span style="color: blue;"&gt;smallint&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt; Sales &lt;span style="color: blue;"&gt;money&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt; RunningTotal &lt;span style="color: blue;"&gt;money&lt;/span&gt;&lt;span style="color: gray;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;      &lt;span style="color: blue;"&gt;DECLARE&lt;/span&gt; @DayCount &lt;span style="color: blue;"&gt;smallint&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;                  @Sales &lt;span style="color: blue;"&gt;money&lt;/span&gt;&lt;span style="color: gray;"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;                  @RunningTotal &lt;span style="color: blue;"&gt;money&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;      &lt;span style="color: blue;"&gt;SET&lt;/span&gt; @RunningTotal &lt;span style="color: gray;"&gt;=&lt;/span&gt; 0&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;      &lt;span style="color: blue;"&gt;SET&lt;/span&gt; @DayCount &lt;span style="color: gray;"&gt;=&lt;/span&gt; 0&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;      &lt;span style="color: blue;"&gt;DECLARE&lt;/span&gt; rt_cursor &lt;span style="color: blue;"&gt;CURSOR&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;      &lt;span style="color: blue;"&gt;FOR&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;      &lt;span style="color: blue;"&gt;SELECT&lt;/span&gt; DayCount&lt;span style="color: gray;"&gt;,&lt;/span&gt; Sales&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;      &lt;span style="color: blue;"&gt;FROM&lt;/span&gt; Sale&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;      &lt;span style="color: blue;"&gt;ORDER&lt;/span&gt; &lt;span style="color: blue;"&gt;BY&lt;/span&gt; DayCount&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;      &lt;span style="color: blue;"&gt;OPEN&lt;/span&gt; rt_cursor&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;      &lt;span style="color: blue;"&gt;FETCH&lt;/span&gt; &lt;span style="color: blue;"&gt;NEXT&lt;/span&gt; &lt;span style="color: blue;"&gt;FROM&lt;/span&gt; rt_cursor &lt;span style="color: blue;"&gt;INTO&lt;/span&gt; @DayCount&lt;span style="color: gray;"&gt;,&lt;/span&gt;@Sales&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;      &lt;span style="color: blue;"&gt;WHILE&lt;/span&gt; &lt;span style="color: fuchsia;"&gt;@@FETCH_STATUS&lt;/span&gt; &lt;span style="color: gray;"&gt;=&lt;/span&gt; 0 &lt;span style="color: gray;"&gt;AND&lt;/span&gt; @DayCount &lt;span style="color: gray;"&gt;&amp;lt;=&lt;/span&gt; @HighestDayCount&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;       &lt;span style="color: blue;"&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;       &lt;span style="color: blue;"&gt;SET&lt;/span&gt; @RunningTotal &lt;span style="color: gray;"&gt;=&lt;/span&gt; @RunningTotal &lt;span style="color: gray;"&gt;+&lt;/span&gt; @Sales&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;       &lt;span style="color: blue;"&gt;INSERT&lt;/span&gt; @SaleTbl &lt;span style="color: blue;"&gt;VALUES &lt;/span&gt;&lt;span style="color: gray;"&gt;(&lt;/span&gt;@DayCount&lt;span style="color: gray;"&gt;,&lt;/span&gt;@Sales&lt;span style="color: gray;"&gt;,&lt;/span&gt;@RunningTotal&lt;span style="color: gray;"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;       &lt;span style="color: blue;"&gt;FETCH&lt;/span&gt; &lt;span style="color: blue;"&gt;NEXT&lt;/span&gt; &lt;span style="color: blue;"&gt;FROM&lt;/span&gt; rt_cursor &lt;span style="color: blue;"&gt;INTO&lt;/span&gt; @DayCount&lt;span style="color: gray;"&gt;,&lt;/span&gt;@Sales&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;       &lt;span style="color: blue;"&gt;END&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;      &lt;span style="color: blue;"&gt;CLOSE&lt;/span&gt; rt_cursor&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;      &lt;span style="color: blue;"&gt;DEALLOCATE&lt;/span&gt; rt_cursor&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;      &lt;span style="color: blue;"&gt;SELECT&lt;/span&gt; DayCount&lt;span style="color: gray;"&gt;,&lt;/span&gt; Sales&lt;span style="color: gray;"&gt;,&lt;/span&gt; RunningTotal &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 10pt;"&gt;      &lt;span style="color: blue;"&gt;FROM&lt;/span&gt; @SaleTbl &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 10pt;"&gt;END&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 10pt;"&gt;GO&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;&lt;em&gt;&lt;font color="#4f81bd"&gt;Create the Sample ASP.NET application&lt;/font&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;In Visual Studio create a new solution and add a class library project called CacheSample.BusinessObjects and an ASP.NET web application called CacheSample.UI.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;The CacheSample.BusinessObjects project will contain a single class to represent a Sale data item, with all the code to retrieve the sales from the database included in it for simplicity (normally I would at least have a separate Repository or other object that is responsible for retrieving data, and probably a data access layer as well, but for this sample I want to keep it simple).&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;The C# code for the Sale class is shown below:&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; System;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; System.Collections.Generic;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; System.Data;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; System.Data.SqlClient;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;namespace&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; CacheSample.BusinessObjects&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Sale&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Int16&lt;/span&gt; DayCount { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;decimal&lt;/span&gt; Sales { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;decimal&lt;/span&gt; RunningTotal { &lt;span style="color: blue;"&gt;get&lt;/span&gt;; &lt;span style="color: blue;"&gt;set&lt;/span&gt;; }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Sale&lt;/span&gt;&amp;gt; GetSales(&lt;span style="color: blue;"&gt;int&lt;/span&gt;? highestDayCount)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Sale&lt;/span&gt;&amp;gt; sales = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Sale&lt;/span&gt;&amp;gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;SqlParameter&lt;/span&gt; highestDayCountParameter = &lt;span style="color: blue;"&gt;new&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 72pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: rgb(43, 145, 175); font-size: 9.5pt;"&gt;SqlParameter&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"@HighestDayCount"&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;SqlDbType&lt;/span&gt;.SmallInt);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;if&lt;/span&gt; (highestDayCount.HasValue)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                highestDayCountParameter.Value = highestDayCount;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;else&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                highestDayCountParameter.Value = &lt;span style="color: rgb(43, 145, 175);"&gt;DBNull&lt;/span&gt;.Value;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;string&lt;/span&gt; connectionStr = &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 72pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="font-size: 9.5pt;"&gt;System.Configuration.&lt;span style="color: rgb(43, 145, 175);"&gt;ConfigurationManager&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 72pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="font-size: 9.5pt;"&gt;.ConnectionStrings[&lt;span style="color: rgb(163, 21, 21);"&gt;"CacheSample"&lt;/span&gt;].ConnectionString;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;using&lt;/span&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;SqlConnection&lt;/span&gt; sqlConn = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;SqlConnection&lt;/span&gt;(connectionStr))&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;using&lt;/span&gt; (&lt;span style="color: rgb(43, 145, 175);"&gt;SqlCommand&lt;/span&gt; sqlCmd = sqlConn.CreateCommand())&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                sqlCmd.CommandText = &lt;span style="color: rgb(163, 21, 21);"&gt;"spGetRunningTotals"&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                sqlCmd.CommandType = &lt;span style="color: rgb(43, 145, 175);"&gt;CommandType&lt;/span&gt;.StoredProcedure;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                sqlCmd.Parameters.Add(highestDayCountParameter);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                sqlConn.Open();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                &lt;span style="color: blue;"&gt;using&lt;/span&gt; (&lt;span style="color: rgb(43, 145, 175);"&gt;SqlDataReader&lt;/span&gt; dr = sqlCmd.ExecuteReader())&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                    &lt;span style="color: blue;"&gt;while&lt;/span&gt; (dr.Read())&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                        &lt;span style="color: rgb(43, 145, 175);"&gt;Sale&lt;/span&gt; newSale = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Sale&lt;/span&gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                        newSale.DayCount = dr.GetInt16(0);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                        newSale.Sales = dr.GetDecimal(1);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                        newSale.RunningTotal = dr.GetDecimal(2);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                        sales.Add(newSale);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;return&lt;/span&gt; sales;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;The static GetSale() method makes a call to the spGetRunningTotals stored procedure and then reads each row from the returned SqlDataReader into an instance of the Sale class, it then returns a List of the Sale objects, as IEnnumerable&amp;lt;Sale&amp;gt;. A reference to System.Configuration needs to be added to the CacheSample.BusinessObjects project so that the connection string can be read from the web.config file.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;In the CacheSample.UI ASP.NET project, create a single web page called ShowSales.aspx, and make this the default start up page. This page will contain a single button to call the GetSales() method and a label to display the results. The html mark up and the C# code behind are shown below:&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;ShowSales.aspx&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="background: yellow; font-size: 9.5pt;"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;@&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; &lt;span style="color: maroon;"&gt;Page&lt;/span&gt; &lt;span style="color: red;"&gt;Language&lt;/span&gt;&lt;span style="color: blue;"&gt;="C#"&lt;/span&gt; &lt;span style="color: red;"&gt;AutoEventWireup&lt;/span&gt;&lt;span style="color: blue;"&gt;="true"&lt;/span&gt; &lt;span style="color: red;"&gt;CodeBehind&lt;/span&gt;&lt;span style="color: blue;"&gt;="ShowSales.aspx.cs"&lt;/span&gt; &lt;span style="color: red;"&gt;Inherits&lt;/span&gt;&lt;span style="color: blue;"&gt;="CacheSample.UI.ShowSales"&lt;/span&gt; &lt;span style="background: yellow;"&gt;%&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;lt;!&lt;/span&gt;&lt;span style="color: maroon; font-size: 9.5pt;"&gt;DOCTYPE&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; &lt;span style="color: red;"&gt;html&lt;/span&gt; &lt;span style="color: red;"&gt;PUBLIC&lt;/span&gt; &lt;span style="color: blue;"&gt;"-//W3C//DTD XHTML 1.0 Transitional//EN"&lt;/span&gt; &lt;span style="color: blue;"&gt;"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon; font-size: 9.5pt;"&gt;html&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; &lt;span style="color: red;"&gt;xmlns&lt;/span&gt;&lt;span style="color: blue;"&gt;="http://www.w3.org/1999/xhtml"&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon; font-size: 9.5pt;"&gt;head&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; &lt;span style="color: red;"&gt;runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;title&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;Cache Sample - Show All Sales&lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;title&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon; font-size: 9.5pt;"&gt;head&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon; font-size: 9.5pt;"&gt;body&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;form&lt;/span&gt; &lt;span style="color: red;"&gt;id&lt;/span&gt;&lt;span style="color: blue;"&gt;="form1"&lt;/span&gt; &lt;span style="color: red;"&gt;runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;div&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;Button&lt;/span&gt; &lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: blue;"&gt;="btnTest1"&lt;/span&gt; &lt;span style="color: red;"&gt;runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&lt;/span&gt; &lt;span style="color: red;"&gt;onclick&lt;/span&gt;&lt;span style="color: blue;"&gt;="btnTest1_Click"&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: red;"&gt;Text&lt;/span&gt;&lt;span style="color: blue;"&gt;="Get All Sales"&lt;/span&gt; &lt;span style="color: blue;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: red;"&gt;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;Label&lt;/span&gt; &lt;span style="color: red;"&gt;ID&lt;/span&gt;&lt;span style="color: blue;"&gt;="lblResults"&lt;/span&gt; &lt;span style="color: red;"&gt;runat&lt;/span&gt;&lt;span style="color: blue;"&gt;="server"&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;asp&lt;/span&gt;&lt;span style="color: blue;"&gt;:&lt;/span&gt;&lt;span style="color: maroon;"&gt;Label&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    &lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;div&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    &lt;span style="color: blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon;"&gt;form&lt;/span&gt;&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon; font-size: 9.5pt;"&gt;body&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: maroon; font-size: 9.5pt;"&gt;html&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;ShowSales.aspx.cs&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; System;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; System.Collections.Generic;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; System.Linq;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; System.Web;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; System.Web.UI;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; System.Web.UI.WebControls;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; CacheSample.BusinessObjects;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;namespace&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; CacheSample.UI&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;partial&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ShowSales&lt;/span&gt; : System.Web.UI.&lt;span style="color: rgb(43, 145, 175);"&gt;Page&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: blue;"&gt;protected&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; Page_Load(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color: rgb(43, 145, 175);"&gt;EventArgs&lt;/span&gt; e)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: blue;"&gt;protected&lt;/span&gt; &lt;span style="color: blue;"&gt;void&lt;/span&gt; btnTest1_Click(&lt;span style="color: blue;"&gt;object&lt;/span&gt; sender, &lt;span style="color: rgb(43, 145, 175);"&gt;EventArgs&lt;/span&gt; e)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            System.Diagnostics.&lt;span style="color: rgb(43, 145, 175);"&gt;Stopwatch&lt;/span&gt; stopWatch = &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 72pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;new&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; System.Diagnostics.&lt;span style="color: rgb(43, 145, 175);"&gt;Stopwatch&lt;/span&gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            stopWatch.Start();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;var&lt;/span&gt; sales = &lt;span style="color: rgb(43, 145, 175);"&gt;Sale&lt;/span&gt;.GetSales(&lt;span style="color: blue;"&gt;null&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;var&lt;/span&gt; lastSales = sales.Last();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            stopWatch.Stop();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            lblResults.Text = &lt;span style="color: blue;"&gt;string&lt;/span&gt;.Format(&lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: rgb(163, 21, 21); font-size: 9.5pt;"&gt;"Count of Sales: {0}, Last DayCount: {1}, Total Sales: {2}. Query took {3} ms"&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;, &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="font-size: 9.5pt;"&gt;sales.Count(), &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="font-size: 9.5pt;"&gt;lastSales.DayCount, &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="font-size: 9.5pt;"&gt;lastSales.RunningTotal, &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="font-size: 9.5pt;"&gt;stopWatch.ElapsedMilliseconds);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;Finally we need to add a connection string to the CacheSample SQL Server database, called CacheSample, to the web.config file:&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21); font-size: 9.5pt;"&gt;xml&lt;/span&gt;&lt;span style="color: red; font-size: 9.5pt;"&gt;version&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;=&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;"&lt;span style="color: blue;"&gt;1.0&lt;/span&gt;"&lt;span style="color: blue;"&gt;?&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21); font-size: 9.5pt;"&gt;configuration&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt; &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21); font-size: 9.5pt;"&gt;connectionStrings&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;    &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21); font-size: 9.5pt;"&gt;add &lt;/span&gt;&lt;span style="color: red; font-size: 9.5pt;"&gt;name&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;=&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;"&lt;span style="color: blue;"&gt;CacheSample&lt;/span&gt;"&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;         &lt;/span&gt;&lt;span style="color: red; font-size: 9.5pt;"&gt;connectionString&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;=&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;"&lt;span style="color: blue;"&gt;data source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=CacheSample&lt;/span&gt;"&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;         &lt;/span&gt;&lt;span style="color: red; font-size: 9.5pt;"&gt;providerName&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;=&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;"&lt;span style="color: blue;"&gt;System.Data.SqlClient&lt;/span&gt;"&lt;span style="color: blue;"&gt; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt; &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21); font-size: 9.5pt;"&gt;connectionStrings&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt; &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21); font-size: 9.5pt;"&gt;system.web&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;    &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21); font-size: 9.5pt;"&gt;compilation &lt;/span&gt;&lt;span style="color: red; font-size: 9.5pt;"&gt;debug&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;=&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;"&lt;span style="color: blue;"&gt;true&lt;/span&gt;"&lt;span style="color: red;"&gt;targetFramework&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;4.0&lt;/span&gt;"&lt;span style="color: blue;"&gt; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt; &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21); font-size: 9.5pt;"&gt;system.web&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21); font-size: 9.5pt;"&gt;configuration&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;Run the application and click the button a few times to see how long each call to the database takes. On my system, each query takes about 450ms.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;Next I shall look at a solution to use the ASP.NET caching to cache the data returned by the query, so that subsequent requests to the GetSales() method are much faster.&lt;/div&gt;
&lt;div style="margin: 10pt 0cm 0pt;"&gt;&lt;b&gt;&lt;font size="5"&gt;&lt;font size="4"&gt;&lt;font color="#4f81bd"&gt;Adding Data Caching Support&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;I am going to create my caching support in a separate project called CacheSample.Caching, so the next step is to add a class library to the solution. We shall be using the application configuration to define the implementation of our caching system, so we need a reference to System.Configuration adding to the project.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;&lt;em&gt;&lt;font color="#4f81bd"&gt;ICacheProvider&amp;lt;T&amp;gt; Interface&lt;/font&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;The first step in adding caching to our application is to define an interface, called ICacheProvider, in the CacheSample.Caching project, with methods to retrieve any data from the cache or to retrieve the data from the data source if it is not present in the cache. Dependency Injection will then be used to inject an implementation of this interface at runtime, allowing the users of the interface (i.e. the CacheSample.BusinessObjects project) to be completely unaware of how the caching is actually implemented. As data of any type maybe retrieved from the data source, it makes sense to use generics in the interface, with a generic type parameter defining the data type associated with a particular instance of the cache interface implementation. The C# code for the ICacheProvider interface is shown below:&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; System;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; System.Collections.Generic;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;namespace&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; CacheSample.Caching&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ICacheProvider&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;interface&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ICacheProvider&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="color: rgb(43, 145, 175);"&gt;ICacheProvider&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        T Fetch(&lt;span style="color: blue;"&gt;string&lt;/span&gt; key, &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: rgb(43, 145, 175); font-size: 9.5pt;"&gt;Func&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;&amp;lt;T&amp;gt; retrieveData, &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: rgb(43, 145, 175); font-size: 9.5pt;"&gt;DateTime&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;? absoluteExpiry, &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: rgb(43, 145, 175); font-size: 9.5pt;"&gt;TimeSpan&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;? relativeExpiry);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; Fetch(&lt;span style="color: blue;"&gt;string&lt;/span&gt; key, &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: rgb(43, 145, 175); font-size: 9.5pt;"&gt;Func&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt; retrieveData, &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: rgb(43, 145, 175); font-size: 9.5pt;"&gt;DateTime&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;? absoluteExpiry, &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: rgb(43, 145, 175); font-size: 9.5pt;"&gt;TimeSpan&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;? relativeExpiry);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;The empty non-generic interface will be used as a type in a Dictionary generic collection later to store instances of the ICacheProvider&amp;lt;T&amp;gt; implementation for reuse, I prefer to use a base interface when doing this, as I think the alternative of using object makes for less clear code.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;The ICacheProvider&amp;lt;T&amp;gt; interface defines two overloaded Fetch methods, the difference between these is that one will return a single instance of the type T and the other will return an IEnumerable&amp;lt;T&amp;gt;, providing support for easy caching of collections of data items.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;Both methods will take a key parameter, which will uniquely identify the cached data, a delegate of type Func&amp;lt;T&amp;gt; or Func&amp;lt;IEnumerable&amp;lt;T&amp;gt;&amp;gt; which will provide the code to retrieve the data from the store if it is not present in the cache, and absolute or relative expiry policies to define when a cached item should expire. Note that at present there is no support for cache dependencies, but I shall be showing a method of adding this in part two of this article.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;&lt;em&gt;&lt;font color="#4f81bd"&gt;CacheProviderFactory Class&lt;/font&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;We need a mechanism of creating instances of our ICacheProvider&amp;lt;T&amp;gt; interface, using Dependency Injection to get the implementation of the interface. To do this we shall create a CacheProviderFactory static class in the CacheSample.Caching project. This factory will provide a generic static method called GetCacheProvider&amp;lt;T&amp;gt;(), which shall return instances of ICacheProvider&amp;lt;T&amp;gt;. We can then call this factory method with the relevant data type (for example the Sale class in the CacheSample.BusinessObject project) to get a instance of ICacheProvider for that type (e.g. call CacheProviderFactory.GetCacheProvider&amp;lt;Sale&amp;gt;() to get the ICacheProvider&amp;lt;Sale&amp;gt; implementation).&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;The C# code for the CacheProviderFactory is shown below:&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; System;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; System.Collections.Generic;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; CacheSample.Caching.Configuration;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;namespace&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; CacheSample.Caching&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;CacheProviderFactory&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Type&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;ICacheProvider&lt;/span&gt;&amp;gt; cacheProviders &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="font-size: 9.5pt;"&gt;= &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Dictionary&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Type&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;ICacheProvider&lt;/span&gt;&amp;gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: blue;"&gt;object&lt;/span&gt; syncRoot = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: blue;"&gt;object&lt;/span&gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;summary&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: green;"&gt; Factory method to create or retrieve an implementation of the &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: green; font-size: 9.5pt;"&gt; &lt;/span&gt;&lt;span style="color: gray; font-size: 9.5pt;"&gt;///&lt;/span&gt;&lt;span style="color: green; font-size: 9.5pt;"&gt; ICacheProvider interface for type &lt;/span&gt;&lt;span style="color: gray; font-size: 9.5pt;"&gt;&amp;lt;typeparamref name="T"/&amp;gt;&lt;/span&gt;&lt;span style="color: green; font-size: 9.5pt;"&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;typeparam name="T"&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: gray; font-size: 9.5pt;"&gt; ///&lt;/span&gt;&lt;span style="color: green; font-size: 9.5pt;"&gt; The type that this cache provider instance will work with&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: green; font-size: 9.5pt;"&gt; &lt;/span&gt;&lt;span style="color: gray; font-size: 9.5pt;"&gt;///&lt;/span&gt;&lt;span style="color: gray; font-size: 9.5pt;"&gt;&amp;lt;/typeparam&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: gray;"&gt;///&lt;/span&gt;&lt;span style="color: gray;"&gt;&amp;lt;returns&amp;gt;&lt;/span&gt;&lt;span style="color: green;"&gt;An instance of the implementation of ICacheProvider for type &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: green; font-size: 9.5pt;"&gt; &lt;/span&gt;&lt;span style="color: gray; font-size: 9.5pt;"&gt;///&lt;/span&gt;&lt;span style="color: gray; font-size: 9.5pt;"&gt;&amp;lt;typeparamref name="T"/&amp;gt;&lt;/span&gt;&lt;span style="color: green; font-size: 9.5pt;"&gt;, as specified by the application &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: green; font-size: 9.5pt;"&gt; &lt;/span&gt;&lt;span style="color: gray; font-size: 9.5pt;"&gt;///&lt;/span&gt;&lt;span style="color: green; font-size: 9.5pt;"&gt; configuration&lt;/span&gt;&lt;span style="color: gray; font-size: 9.5pt;"&gt;&amp;lt;/returns&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;ICacheProvider&lt;/span&gt;&amp;lt;T&amp;gt; GetCacheProvider&amp;lt;T&amp;gt;()&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;ICacheProvider&lt;/span&gt;&amp;lt;T&amp;gt; cacheProvider = &lt;span style="color: blue;"&gt;null&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: green;"&gt;// Get the Type reference for the type parameter T&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;Type&lt;/span&gt; typeOfT = &lt;span style="color: blue;"&gt;typeof&lt;/span&gt;(T);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: green;"&gt;// Lock the access to the cacheProviders dictionary &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: green; font-size: 9.5pt;"&gt;            // so multiple threads can work with it&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;lock&lt;/span&gt; (syncRoot)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                &lt;span style="color: green;"&gt;// First check if an instance of the ICacheProvider implementation &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: green; font-size: 9.5pt;"&gt; // already exists in the cacheProviders dictionary for the type T&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                &lt;span style="color: blue;"&gt;if&lt;/span&gt; (cacheProviders.ContainsKey(typeOfT))&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                    cacheProvider = (&lt;span style="color: rgb(43, 145, 175);"&gt;ICacheProvider&lt;/span&gt;&amp;lt;T&amp;gt;)cacheProviders[typeOfT];&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                &lt;span style="color: blue;"&gt;else&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                    &lt;span style="color: green;"&gt;// There is not already an instance of the ICacheProvider in &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: green; font-size: 9.5pt;"&gt;      // cacheProviders for the type T&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                    &lt;span style="color: green;"&gt;// so we need to create one&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                    &lt;span style="color: green;"&gt;// Get the Type reference for the application's implementation of &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 72pt; line-height: normal;"&gt;&lt;span style="color: green; font-size: 9.5pt;"&gt;      // ICacheProvider from the configuration&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                    &lt;span style="color: rgb(43, 145, 175);"&gt;Type&lt;/span&gt; cacheProviderType = &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 108pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: rgb(43, 145, 175); font-size: 9.5pt;"&gt;Type&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;.GetType(&lt;span style="color: rgb(43, 145, 175);"&gt;CacheProviderConfigurationSection&lt;/span&gt;.Current.&lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 180pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="font-size: 9.5pt;"&gt;CacheProviderType);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                    &lt;span style="color: blue;"&gt;if&lt;/span&gt; (cacheProviderType != &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                        &lt;span style="color: green;"&gt;// Now get a Type reference for the Cache Provider with the &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: green; font-size: 9.5pt;"&gt;                        // type T generic parameter&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                        &lt;span style="color: rgb(43, 145, 175);"&gt;Type&lt;/span&gt; typeOfCacheProviderTypeForT = &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 108pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="font-size: 9.5pt;"&gt;cacheProviderType.MakeGenericType(&lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Type&lt;/span&gt;[] { typeOfT });&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                        &lt;span style="color: blue;"&gt;if&lt;/span&gt; (typeOfCacheProviderTypeForT != &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                            &lt;span style="color: green;"&gt;// Create the instance of the Cache Provider and add it to &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 108pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: green; font-size: 9.5pt;"&gt;// the cacheProviders dictionary for future use&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                            cacheProvider = &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 144pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="font-size: 9.5pt;"&gt;(&lt;span style="color: rgb(43, 145, 175);"&gt;ICacheProvider&lt;/span&gt;&amp;lt;T&amp;gt;)&lt;span style="color: rgb(43, 145, 175);"&gt;Activator&lt;/span&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 144pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="font-size: 9.5pt;"&gt;CreateInstance(typeOfCacheProviderTypeForT);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                            cacheProviders.Add(typeOfT, cacheProvider);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;return&lt;/span&gt; cacheProvider;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;As this code uses Activator.CreateInstance() to create instances of the ICacheProvider&amp;lt;T&amp;gt; implementation, which is a slow process, the factory class maintains a Dictionary of the previously created instances so that a cache provider needs to be created only once for each type.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;The type of the implementation of ICacheProvider&amp;lt;T&amp;gt; is read from a custom configuration section in the application configuration file, via the CacheProviderConfigurationSection class, which is described below.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;&lt;em&gt;&lt;font color="#4f81bd"&gt;CacheProviderConfigurationSection Class&lt;/font&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;The implementation of ICacheProvider&amp;lt;T&amp;gt; will be specified in a custom configuration section in the application’s configuration. To handle this create a folder in the CacheSample.Caching project called Configuration, and add a class called CacheProviderConfigurationSection to this folder. This class will extend the System.Configuration.ConfigurationSection class, and will contain a single string property called CacheProviderType. The C# code for this class is shown below:&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; System;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; System.Configuration;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;namespace&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; CacheSample.Caching.Configuration&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    &lt;span style="color: blue;"&gt;internal&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;CacheProviderConfigurationSection&lt;/span&gt; : &lt;span style="color: rgb(43, 145, 175);"&gt;ConfigurationSection&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;CacheProviderConfigurationSection&lt;/span&gt; Current&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                &lt;span style="color: blue;"&gt;return&lt;/span&gt; (&lt;span style="color: rgb(43, 145, 175);"&gt;CacheProviderConfigurationSection&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 72pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: rgb(43, 145, 175); font-size: 9.5pt;"&gt;ConfigurationManager&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;.GetSection(&lt;span style="color: rgb(163, 21, 21);"&gt;"cacheProvider"&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        [&lt;span style="color: rgb(43, 145, 175);"&gt;ConfigurationProperty&lt;/span&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"type"&lt;/span&gt;, IsRequired=&lt;span style="color: blue;"&gt;true&lt;/span&gt;)]&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;string&lt;/span&gt; CacheProviderType&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;get&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                &lt;span style="color: blue;"&gt;return&lt;/span&gt; (&lt;span style="color: blue;"&gt;string&lt;/span&gt;)&lt;span style="color: blue;"&gt;this&lt;/span&gt;[&lt;span style="color: rgb(163, 21, 21);"&gt;"type"&lt;/span&gt;];&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="margin: 10pt 0cm 0pt;"&gt;&lt;b&gt;&lt;font size="5"&gt;&lt;font size="4"&gt;&lt;font color="#4f81bd"&gt;Adding Data Caching to the Sales Class&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;We now have enough code in place to add caching to the GetSales() method in the CacheSample.BusinessObjects.Sale class, even though we do not yet have an implementation of the ICacheProvider&amp;lt;T&amp;gt; interface.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;We need to add a reference to the CacheSample.Caching project to CacheSample.BusinessObjects so that we can use the ICacheProvider&amp;lt;T&amp;gt; interface within the GetSales() method.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;Once the reference is added, we can first create a unique string key based on the method name and the parameter value, so that the same cache key is used for repeated calls to the method with the same parameter values. Then we get an instance of the cache provider for the Sales type, using the CacheProviderFactory, and pass the existing code to retrieve the data from the database as the retrievalMethod delegate in a call to the Cache Provider Fetch() method. The C# code for the modified GetSales() method is shown below:&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;public&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; &lt;span style="color: blue;"&gt;static&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Sale&lt;/span&gt;&amp;gt; GetSales(&lt;span style="color: blue;"&gt;int&lt;/span&gt;? highestDayCount)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    &lt;span style="color: blue;"&gt;string&lt;/span&gt; cacheKey = &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;string&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;.Format(&lt;span style="color: rgb(163, 21, 21);"&gt;"CacheSample.BusinessObjects.GetSalesWithCache({0})"&lt;/span&gt;, &lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt;"&gt;&lt;span style="font-size: 9.5pt;"&gt;highestDayCount);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    &lt;span style="color: blue;"&gt;return&lt;/span&gt; CacheSample.Caching.&lt;span style="color: rgb(43, 145, 175);"&gt;CacheProviderFactory&lt;/span&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt;"&gt;&lt;span style="font-size: 9.5pt;"&gt;GetCacheProvider&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Sale&lt;/span&gt;&amp;gt;().Fetch(cacheKey,&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: blue;"&gt;delegate&lt;/span&gt;()&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Sale&lt;/span&gt;&amp;gt; sales = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;Sale&lt;/span&gt;&amp;gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: rgb(43, 145, 175);"&gt;SqlParameter&lt;/span&gt; highestDayCountParameter = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: rgb(43, 145, 175); font-size: 9.5pt;"&gt;SqlParameter&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;(&lt;span style="color: rgb(163, 21, 21);"&gt;"@HighestDayCount"&lt;/span&gt;, &lt;span style="color: rgb(43, 145, 175);"&gt;SqlDbType&lt;/span&gt;.SmallInt);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;if&lt;/span&gt; (highestDayCount.HasValue)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                highestDayCountParameter.Value = highestDayCount;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;else&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                highestDayCountParameter.Value = &lt;span style="color: rgb(43, 145, 175);"&gt;DBNull&lt;/span&gt;.Value;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;string&lt;/span&gt; connectionStr = &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="font-size: 9.5pt;"&gt;System.Configuration.&lt;span style="color: rgb(43, 145, 175);"&gt;ConfigurationManager&lt;/span&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="font-size: 9.5pt;"&gt;ConnectionStrings[&lt;span style="color: rgb(163, 21, 21);"&gt;"CacheSample"&lt;/span&gt;].ConnectionString;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;using&lt;/span&gt; (&lt;span style="color: rgb(43, 145, 175);"&gt;SqlConnection&lt;/span&gt; sqlConn = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;SqlConnection&lt;/span&gt;(connectionStr))&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;using&lt;/span&gt; (&lt;span style="color: rgb(43, 145, 175);"&gt;SqlCommand&lt;/span&gt; sqlCmd = sqlConn.CreateCommand())&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                sqlCmd.CommandText = &lt;span style="color: rgb(163, 21, 21);"&gt;"spGetRunningTotals"&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                sqlCmd.CommandType = &lt;span style="color: rgb(43, 145, 175);"&gt;CommandType&lt;/span&gt;.StoredProcedure;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                sqlCmd.Parameters.Add(highestDayCountParameter);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                sqlConn.Open();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                &lt;span style="color: blue;"&gt;using&lt;/span&gt; (&lt;span style="color: rgb(43, 145, 175);"&gt;SqlDataReader&lt;/span&gt; dr = sqlCmd.ExecuteReader())&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                    &lt;span style="color: blue;"&gt;while&lt;/span&gt; (dr.Read())&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                        &lt;span style="color: rgb(43, 145, 175);"&gt;Sale&lt;/span&gt; newSale = &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;Sale&lt;/span&gt;();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                        newSale.DayCount = dr.GetInt16(0);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                        newSale.Sales = dr.GetDecimal(1);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                        newSale.RunningTotal = dr.GetDecimal(2);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                        sales.Add(newSale);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;return&lt;/span&gt; sales;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        },&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: blue;"&gt;null&lt;/span&gt;,&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: blue;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;TimeSpan&lt;/span&gt;(0, 10, 0));&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;This example passes the code to retrieve the Sales data from the database to the Cache Provider as an anonymous method, however it could also be written as a lambda. The main advantage of using an anonymous function (method or lambda) is that the code inside the anonymous function can access the parameters passed to the GetSales() method. Finally the absolute expiry is set to null, and the relative expiry set to 10 minutes, to indicate that the cache entry should be removed 10 minutes after the last request for the data.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;As the ICacheProvider&amp;lt;T&amp;gt; has a Fetch() method that returns IEnumerable&amp;lt;T&amp;gt;, we can simply return the results of the Fetch() method to the caller of the GetSales() method.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;This should be all that is needed for the GetSales() method to now retrieve data from a cache after the first time the data has be retrieved from the database.&lt;/div&gt;
&lt;div style="margin: 10pt 0cm 0pt;"&gt;&lt;b&gt;&lt;font size="5"&gt;&lt;font size="4"&gt;&lt;font color="#4f81bd"&gt;Implementing a ASP.NET Cache Provider&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;The final step is to actually implement the ICacheProvider&amp;lt;T&amp;gt; interface, and add the implementation details to the web.config file for the dependency injection.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;The cache provider implementation needs to have access to System.Web. Therefore it could be placed in the CacheSample.UI project, or in its own project that has a reference to System.Web. Implementing the Cache Provider in a separate project is my favoured approach.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;Create a new project inside the solution called CacheSample.CacheProvider, and add references to System.Web and CacheSample.Caching to this project. Add a class to the project called AspNetCacheProvider. Make the class a generic class by adding the generic parameter &amp;lt;T&amp;gt; and indicate that the class implements ICacheProvider&amp;lt;T&amp;gt;.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;The C# code for the AspNetCacheProvider class is shown below:&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; System;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; System.Collections.Generic;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; System.Linq;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; System.Web;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; System.Web.Caching;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;using&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; CacheSample.Caching;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;namespace&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; CacheSample.CacheProvider&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: blue;"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;AspNetCacheProvider&lt;/span&gt;&amp;lt;T&amp;gt; : &lt;span style="color: rgb(43, 145, 175);"&gt;ICacheProvider&lt;/span&gt;&amp;lt;T&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;        #region&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; ICacheProvider&amp;lt;T&amp;gt; Members&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; T Fetch(&lt;span style="color: blue;"&gt;string&lt;/span&gt; key, &lt;span style="color: rgb(43, 145, 175);"&gt;Func&lt;/span&gt;&amp;lt;T&amp;gt; retrieveData, &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: rgb(43, 145, 175); font-size: 9.5pt;"&gt;DateTime&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;? absoluteExpiry, &lt;span style="color: rgb(43, 145, 175);"&gt;TimeSpan&lt;/span&gt;? relativeExpiry)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;return&lt;/span&gt; FetchAndCache&amp;lt;T&amp;gt;(key, retrieveData, &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="font-size: 9.5pt;"&gt;absoluteExpiry, relativeExpiry);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: blue;"&gt;public&lt;/span&gt; &lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt; Fetch(&lt;span style="color: blue;"&gt;string&lt;/span&gt; key, &lt;span style="color: rgb(43, 145, 175);"&gt;Func&lt;/span&gt;&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt; retrieveData, &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: rgb(43, 145, 175); font-size: 9.5pt;"&gt;DateTime&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;? absoluteExpiry, &lt;span style="color: rgb(43, 145, 175);"&gt;TimeSpan&lt;/span&gt;? relativeExpiry)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;return&lt;/span&gt; FetchAndCache&amp;lt;&lt;span style="color: rgb(43, 145, 175);"&gt;IEnumerable&lt;/span&gt;&amp;lt;T&amp;gt;&amp;gt;(key, retrieveData, &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="font-size: 9.5pt;"&gt;absoluteExpiry, relativeExpiry);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;        #endregion&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;        #region&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt; Helper Methods&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: blue;"&gt;private&lt;/span&gt; U FetchAndCache&amp;lt;U&amp;gt;(&lt;span style="color: blue;"&gt;string&lt;/span&gt; key, &lt;span style="color: rgb(43, 145, 175);"&gt;Func&lt;/span&gt;&amp;lt;U&amp;gt; retrieveData, &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 36pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: rgb(43, 145, 175); font-size: 9.5pt;"&gt;DateTime&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;? absoluteExpiry, &lt;span style="color: rgb(43, 145, 175);"&gt;TimeSpan&lt;/span&gt;? relativeExpiry)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            U value;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;if&lt;/span&gt; (!TryGetValue&amp;lt;U&amp;gt;(key, &lt;span style="color: blue;"&gt;out&lt;/span&gt; value))&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                value = retrieveData();&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                &lt;span style="color: blue;"&gt;if&lt;/span&gt; (!absoluteExpiry.HasValue)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                    absoluteExpiry = &lt;span style="color: rgb(43, 145, 175);"&gt;Cache&lt;/span&gt;.NoAbsoluteExpiration;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                &lt;span style="color: blue;"&gt;if&lt;/span&gt; (!relativeExpiry.HasValue)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                    relativeExpiry = &lt;span style="color: rgb(43, 145, 175);"&gt;Cache&lt;/span&gt;.NoSlidingExpiration;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                &lt;span style="color: rgb(43, 145, 175);"&gt;HttpContext&lt;/span&gt;.Current.Cache.Insert(key, value, &lt;/span&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 0pt 72pt; line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;null&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;, absoluteExpiry.Value, relativeExpiry.Value);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;return&lt;/span&gt; value;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        &lt;span style="color: blue;"&gt;private&lt;/span&gt; &lt;span style="color: blue;"&gt;bool&lt;/span&gt; TryGetValue&amp;lt;U&amp;gt;(&lt;span style="color: blue;"&gt;string&lt;/span&gt; key, &lt;span style="color: blue;"&gt;out&lt;/span&gt; U value)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;object&lt;/span&gt; cachedValue = &lt;span style="color: rgb(43, 145, 175);"&gt;HttpContext&lt;/span&gt;.Current.Cache.Get(key);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;if&lt;/span&gt; (cachedValue == &lt;span style="color: blue;"&gt;null&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                value = &lt;span style="color: blue;"&gt;default&lt;/span&gt;(U);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;false&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            &lt;span style="color: blue;"&gt;else&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                &lt;span style="color: blue;"&gt;try&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                    value = (U)cachedValue;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                    &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;true&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                &lt;span style="color: blue;"&gt;catch&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                {&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                    value = &lt;span style="color: blue;"&gt;default&lt;/span&gt;(U);&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                    &lt;span style="color: blue;"&gt;return&lt;/span&gt; &lt;span style="color: blue;"&gt;false&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;                }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;            }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;        }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;        #endregion&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;    }&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="font-size: 9.5pt;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;The two interface Fetch() methods call a private method called FetchAndCache(). This method first checks for a element in the HttpContext.Current.Cache with the specified cache key, and if so tries to cast this to the specified type (either T or IEnumerable&amp;lt;T&amp;gt;). If the cached element is found, the FetchAndCache() method simply returns it. If it is not found in the cache, the method calls the retrievalMethod delegate to get the data from the data source, and then adds this to the HttpContext.Current.Cache.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;The final step is to add the AspNetCacheProvider class to the relevant custom configuration section in the CacheSample.UI.Web.Config file. To do this there needs to be a &amp;lt;configSections&amp;gt; element added as the first element in &amp;lt;configuration&amp;gt;. This will match a custom section called &amp;lt;cacheProvider&amp;gt; with the CacheProviderConfigurationSection. Then we add a &amp;lt;cacheProvider&amp;gt; element, with a type property set to the fully qualified assembly name of the AspNetCacheProvider class, as shown below:&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21); font-size: 9.5pt;"&gt;xml&lt;/span&gt;&lt;span style="color: red; font-size: 9.5pt;"&gt;version&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;=&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;"&lt;span style="color: blue;"&gt;1.0&lt;/span&gt;"&lt;span style="color: blue;"&gt;?&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21); font-size: 9.5pt;"&gt;configuration&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt; &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21); font-size: 9.5pt;"&gt;configSections&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;    &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21); font-size: 9.5pt;"&gt;section &lt;/span&gt;&lt;span style="color: red; font-size: 9.5pt;"&gt;name&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;=&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;"&lt;span style="color: blue;"&gt;cacheProvider&lt;/span&gt;"&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal; text-indent: 36pt;"&gt;&lt;span style="color: red; font-size: 9.5pt;"&gt;type&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;=&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;"&lt;span style="color: blue;"&gt;CacheSample.Caching.Configuration.CacheProviderConfigurationSection, CacheSample.Caching&lt;/span&gt;"&lt;span style="color: blue;"&gt; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt; &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21); font-size: 9.5pt;"&gt;configSections&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt; &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21); font-size: 9.5pt;"&gt;connectionStrings&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;    &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21); font-size: 9.5pt;"&gt;add &lt;/span&gt;&lt;span style="color: red; font-size: 9.5pt;"&gt;name&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;=&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;"&lt;span style="color: blue;"&gt;CacheSample&lt;/span&gt;"&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;         &lt;/span&gt;&lt;span style="color: red; font-size: 9.5pt;"&gt;connectionString&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;=&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;"&lt;span style="color: blue;"&gt;data source=.\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=CacheSample&lt;/span&gt;"&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;         &lt;/span&gt;&lt;span style="color: red; font-size: 9.5pt;"&gt;providerName&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;=&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;"&lt;span style="color: blue;"&gt;System.Data.SqlClient&lt;/span&gt;"&lt;span style="color: blue;"&gt; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt; &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21); font-size: 9.5pt;"&gt;connectionStrings&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt; &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21); font-size: 9.5pt;"&gt;cacheProvider &lt;/span&gt;&lt;span style="color: red; font-size: 9.5pt;"&gt;type&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;=&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;"&lt;span style="color: blue;"&gt;CacheSample.CacheProvider.AspNetCacheProvider`1, CacheSample.CacheProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null&lt;/span&gt;"&lt;span style="color: blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt; &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21); font-size: 9.5pt;"&gt;cacheProvider&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt; &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21); font-size: 9.5pt;"&gt;system.web&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;    &amp;lt;&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21); font-size: 9.5pt;"&gt;compilation &lt;/span&gt;&lt;span style="color: red; font-size: 9.5pt;"&gt;debug&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;=&lt;/span&gt;&lt;span style="font-size: 9.5pt;"&gt;"&lt;span style="color: blue;"&gt;true&lt;/span&gt;"&lt;span style="color: red;"&gt;targetFramework&lt;/span&gt;&lt;span style="color: blue;"&gt;=&lt;/span&gt;"&lt;span style="color: blue;"&gt;4.0&lt;/span&gt;"&lt;span style="color: blue;"&gt; /&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt; &amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21); font-size: 9.5pt;"&gt;system.web&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="line-height: normal;"&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: rgb(163, 21, 21); font-size: 9.5pt;"&gt;configuration&lt;/span&gt;&lt;span style="color: blue; font-size: 9.5pt;"&gt;&amp;gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="line-height: normal;"&gt; &lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;One point to note is that the fully qualified assembly name of the AspNetCacheProvider class includes the notation `1 after the class name, which indicates that it is a generic class with a single generic type parameter.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;The CacheSample.UI project needs to have references added to CacheSample.Caching and CacheSample.CacheProvider so that the actual application is aware of the relevant cache provider implementation.&lt;/div&gt;
&lt;div style="margin: 10pt 0cm 0pt;"&gt;&lt;b&gt;&lt;font size="5"&gt;&lt;font size="4"&gt;&lt;font color="#4f81bd"&gt;Conclusion&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;After implementing this solution, you should have a working cache provider mechanism, that will allow the middle and data access layers to implement caching support when retrieving data, without any knowledge of the actually caching implementation. If the UI is not ASP.NET based, if for example it is Winforms or WPF, the implementation of ICacheProvider&amp;lt;T&amp;gt; would be written around whatever technology is available. It could even be a standalone caching system that takes full responsibility for adding and removing items from a global store.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;The next part of this article will show how this caching mechanism may be extended to provide support for cache dependencies, such as the System.Web.Caching.SqlCacheDependency.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt;Another possible extension would be to cache the cache provider implementations instead of storing them in a static Dictionary in the CacheProviderFactory. This would prevent a build up of seldom used cache providers in the application memory, as they could be removed from the cache if not used often enough, although in reality there are probably unlikely to be vast numbers of cache provider implementation instances, as most applications do not have a massive number of business object or model types.&lt;/div&gt;
&lt;div style="margin: 0cm 0cm 10pt;"&gt; &lt;/div&gt; &lt;img src="http://geekswithblogs.net/Rhames/aggbug/143422.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rhames</dc:creator>
            <guid>http://geekswithblogs.net/Rhames/archive/2011/01/10/using-the-asp.net-cache-to-cache-data-in-a-model.aspx</guid>
            <pubDate>Mon, 10 Jan 2011 23:35:06 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Rhames/comments/143422.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Rhames/archive/2011/01/10/using-the-asp.net-cache-to-cache-data-in-a-model.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/Rhames/comments/commentRss/143422.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Rhames/services/trackbacks/143422.aspx</trackback:ping>
        </item>
        <item>
            <title>Trim Leading Zeros from a string in SQL Server</title>
            <category>SQL Server</category>
            <link>http://geekswithblogs.net/Rhames/archive/2009/08/21/trim-leading-zeros-from-a-string-in-sql-server.aspx</link>
            <description>&lt;p&gt;Originally posted on: &lt;a href='http://geekswithblogs.net/Rhames/archive/2009/08/21/trim-leading-zeros-from-a-string-in-sql-server.aspx'&gt;http://geekswithblogs.net/Rhames/archive/2009/08/21/trim-leading-zeros-from-a-string-in-sql-server.aspx&lt;/a&gt;&lt;/p&gt;&lt;p style="MARGIN: 12pt 0cm 3pt"&gt;&lt;font size="3" face="Times New Roman"&gt;This user defined function is based on a function posted at this very useful site: &lt;/font&gt;&lt;a href="http://www.sql-server-helper.com/functions/trim-leading-zeros.aspx"&gt;&lt;font color="#800080" size="3" face="Times New Roman"&gt;http://www.sql-server-helper.com/functions/trim-leading-zeros.aspx&lt;/font&gt;&lt;/a&gt;&lt;font size="3" face="Times New Roman"&gt;.&lt;/font&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;font size="3" face="Times New Roman"&gt; &lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"&gt;&lt;font size="3" face="Times New Roman"&gt;I have modified it slightly to handle spaces within the input string; by first replacing any existing spaces with ‘¬’ (this seems to be a suitably obscure character that is never likely to occur in the input string).&lt;/font&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"&gt;&lt;o:p&gt;&lt;font size="3" face="Times New Roman"&gt; &lt;/font&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;CREATE&lt;/span&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt; &lt;span style="COLOR: blue"&gt;FUNCTION&lt;/span&gt; RemoveLeadingZerosFromVarChar &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; COLOR: gray; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;(&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;      &lt;/span&gt;&lt;span style="COLOR: green"&gt;-- Add the parameters for the function here&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;      &lt;/span&gt;@inputStr &lt;span style="COLOR: blue"&gt;varchar&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; COLOR: gray; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;RETURNS&lt;/span&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt; &lt;span style="COLOR: blue"&gt;varchar&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;AS&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;BEGIN&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;      &lt;/span&gt;&lt;span style="COLOR: green"&gt;-- Declare the return variable here&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;      &lt;/span&gt;&lt;span style="COLOR: blue"&gt;DECLARE&lt;/span&gt; @Result &lt;span style="COLOR: blue"&gt;varchar&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; COLOR: gray; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;      &lt;/span&gt;&lt;span style="COLOR: green"&gt;-- This function works by replacing all '0' characters with a ' ' (space)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;      &lt;/span&gt;&lt;span style="COLOR: green"&gt;-- character, then using LTRIM to strip all the leading spaces&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;      &lt;/span&gt;&lt;span style="COLOR: green"&gt;-- Finally all ' ' characters remaining are changed back into '0' characters&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;      &lt;/span&gt;&lt;span style="COLOR: green"&gt;-- (this restores any '0' characters indise the input string)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;      &lt;/span&gt;&lt;span style="COLOR: green"&gt;-- To handle actual spaces within the input string, first any spaces are&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;      &lt;/span&gt;&lt;span style="COLOR: green"&gt;-- replaced with an obscure character that is never likely to occur ('¬')&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;      &lt;/span&gt;&lt;span style="COLOR: green"&gt;-- so that this can be replaced with spaces again at the end.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; COLOR: green; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;      &lt;/span&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt; @Result &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;/span&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="COLOR: fuchsia"&gt;replace&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="COLOR: fuchsia"&gt;replace&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="COLOR: fuchsia"&gt;ltrim&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="COLOR: fuchsia"&gt;replace&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="COLOR: fuchsia"&gt;          replace&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;ltrim&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;@inputStr&lt;span style="COLOR: gray"&gt;),&lt;/span&gt; &lt;span style="COLOR: red"&gt;' '&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;span style="COLOR: red"&gt;'¬'&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: green"&gt;-- replace existing spaces with '¬'&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 6"&gt;          &lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;span style="COLOR: red"&gt;'0'&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;span style="COLOR: red"&gt;' '&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: green"&gt;-- replace '0' with ' '&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 5"&gt;          &lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: green"&gt;-- end of LTRIM to remove leading '0's that have been changed to ' 's&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 3"&gt;          &lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;span style="COLOR: red"&gt;' '&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;span style="COLOR: red"&gt;'0'&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: green"&gt;-- change ' ' back to '0'&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 2"&gt;          &lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;span style="COLOR: red"&gt;'¬'&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;span style="COLOR: red"&gt;' '&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: green"&gt;-- change '¬' back to ' '&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; COLOR: green; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;      &lt;/span&gt;&lt;span style="COLOR: green"&gt;-- Return the result of the function&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;      &lt;/span&gt;&lt;span style="COLOR: blue"&gt;RETURN&lt;/span&gt; @Result&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;END&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;GO&lt;/span&gt;&lt;/p&gt; &lt;img src="http://geekswithblogs.net/Rhames/aggbug/134229.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rhames</dc:creator>
            <guid>http://geekswithblogs.net/Rhames/archive/2009/08/21/trim-leading-zeros-from-a-string-in-sql-server.aspx</guid>
            <pubDate>Fri, 21 Aug 2009 08:16:06 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Rhames/comments/134229.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Rhames/archive/2009/08/21/trim-leading-zeros-from-a-string-in-sql-server.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/Rhames/comments/commentRss/134229.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Rhames/services/trackbacks/134229.aspx</trackback:ping>
        </item>
        <item>
            <title>Using int.TryParse() within a LINQ where clause</title>
            <category>LINQ</category>
            <category>C#</category>
            <link>http://geekswithblogs.net/Rhames/archive/2009/08/12/using-int.tryparse-within-a-linq-where-clause.aspx</link>
            <description>&lt;p&gt;Originally posted on: &lt;a href='http://geekswithblogs.net/Rhames/archive/2009/08/12/using-int.tryparse-within-a-linq-where-clause.aspx'&gt;http://geekswithblogs.net/Rhames/archive/2009/08/12/using-int.tryparse-within-a-linq-where-clause.aspx&lt;/a&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt"&gt;&lt;font size="3" face="Times New Roman"&gt;I pretty new to LINQ, and I’m keen to get more experience using it, so whenever an opportunity arises I like to try writing LINQ queries. &lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt"&gt;&lt;o:p&gt;&lt;font size="3" face="Times New Roman"&gt; &lt;/font&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt"&gt;&lt;font size="3" face="Times New Roman"&gt;I needed to write a method to extract a comma separated list of numbers from a config file, and return this as List&amp;lt;int&amp;gt;. I was looking at ways to do this using LINQ, but hit a problem. I wanted my LINQ query to filter out any values in the CSV string that could not be parsed as an int, without causing an exception. Using int.TryParse() seemed like a possible solution, but I had problems because the TryParse() method has an out parameter to store the parsed result. I ended up with the code below, which seems to work, but looks very messy, because I think it is parsing the string twice.&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt"&gt;&lt;o:p&gt;&lt;font size="3" face="Times New Roman"&gt; &lt;/font&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt"&gt;&lt;font size="3" face="Times New Roman"&gt;Anybody have any suggestions of how to improve this code?&lt;/font&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt"&gt;&lt;o:p&gt;&lt;font size="3" face="Times New Roman"&gt; &lt;/font&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;; color: blue; font-size: 10pt; mso-no-proof: yes"&gt;public&lt;/span&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt; mso-no-proof: yes"&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;List&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;int&lt;/span&gt;&amp;gt; AuthorisedGroups&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt; mso-no-proof: yes"&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="text-indent: 36pt; margin: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;; color: blue; font-size: 10pt; mso-no-proof: yes"&gt;get&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;      &lt;/span&gt;{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;      &lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;      &lt;/span&gt;&lt;span style="color: blue"&gt;string&lt;/span&gt;[] authorisedGroupsStr = &lt;span style="color: #2b91af"&gt;ConfigurationManager&lt;/span&gt;.AppSettings[&lt;span style="color: #a31515"&gt;"AuthorisedGroups"&lt;/span&gt;].Split(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: blue"&gt;char&lt;/span&gt;[] { &lt;span style="color: #a31515"&gt;','&lt;/span&gt; });&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt; mso-no-proof: yes"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="text-indent: 36pt; margin: 0cm 0cm 0pt 36pt; mso-layout-grid-align: none"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;; color: blue; font-size: 10pt; mso-no-proof: yes"&gt;int&lt;/span&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt; mso-no-proof: yes"&gt; authorisedGroupInt;&lt;span style="mso-spacerun: yes"&gt;                &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt; mso-no-proof: yes"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="text-indent: 36pt; margin: 0cm 0cm 0pt 36pt; mso-layout-grid-align: none"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;; color: blue; font-size: 10pt; mso-no-proof: yes"&gt;var&lt;/span&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt; mso-no-proof: yes"&gt; authorisedGroupsInt = &lt;span style="color: blue"&gt;from&lt;/span&gt; authorisedGroupStr &lt;span style="color: blue"&gt;in&lt;/span&gt; authorisedGroupsStr&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;                                          &lt;/span&gt;&lt;span style="color: blue"&gt;where&lt;/span&gt; &lt;span style="color: blue"&gt;int&lt;/span&gt;.TryParse(authorisedGroupStr, &lt;span style="color: blue"&gt;out&lt;/span&gt; authorisedGroupInt)&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;     &lt;/span&gt;&lt;span style="mso-spacerun: yes"&gt;                                     &lt;/span&gt;&lt;span style="color: blue"&gt;select&lt;/span&gt; &lt;span style="color: blue"&gt;int&lt;/span&gt;.Parse(authorisedGroupStr);&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-spacerun: yes"&gt;  &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="text-indent: 36pt; margin: 0cm 0cm 0pt 36pt; mso-layout-grid-align: none"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;; color: blue; font-size: 10pt; mso-no-proof: yes"&gt;return&lt;/span&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt; mso-no-proof: yes"&gt; authorisedGroupsInt.ToList();&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="text-indent: 36pt; margin: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt; mso-no-proof: yes"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt; mso-layout-grid-align: none"&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;; font-size: 10pt; mso-no-proof: yes"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="margin: 0cm 0cm 0pt"&gt;&lt;o:p&gt;&lt;font size="3" face="Times New Roman"&gt; &lt;/font&gt;&lt;/o:p&gt;&lt;/p&gt; &lt;img src="http://geekswithblogs.net/Rhames/aggbug/134049.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rhames</dc:creator>
            <guid>http://geekswithblogs.net/Rhames/archive/2009/08/12/using-int.tryparse-within-a-linq-where-clause.aspx</guid>
            <pubDate>Wed, 12 Aug 2009 09:37:22 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Rhames/comments/134049.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Rhames/archive/2009/08/12/using-int.tryparse-within-a-linq-where-clause.aspx#feedback</comments>
            <slash:comments>7</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/Rhames/comments/commentRss/134049.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Rhames/services/trackbacks/134049.aspx</trackback:ping>
        </item>
        <item>
            <title>Getting the Time part only from a SQL Server DateTime</title>
            <category>SQL Server</category>
            <link>http://geekswithblogs.net/Rhames/archive/2009/08/11/getting-the-time-part-only-from-a-sql-server-datetime.aspx</link>
            <description>&lt;p&gt;Originally posted on: &lt;a href='http://geekswithblogs.net/Rhames/archive/2009/08/11/getting-the-time-part-only-from-a-sql-server-datetime.aspx'&gt;http://geekswithblogs.net/Rhames/archive/2009/08/11/getting-the-time-part-only-from-a-sql-server-datetime.aspx&lt;/a&gt;&lt;/p&gt;&lt;div style="MARGIN: 0cm 0cm 10pt"&gt;&lt;span style="LINE-HEIGHT: 115%; FONT-SIZE: 10pt"&gt;&lt;span style="COLOR: gray"&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"&gt;&lt;font color="#000000" size="3" face="Times New Roman"&gt;Just some code to get the time part only from a DateTime field (is SQL Server 2000 or 2005), with the date set to a reference date&lt;/font&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: Arial; FONT-SIZE: 9pt"&gt;&lt;o:p&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;declare&lt;/span&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;font color="#000000"&gt; @timetest &lt;/font&gt;&lt;span style="COLOR: blue"&gt;datetime&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;declare&lt;/span&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;font color="#000000"&gt; @refdate &lt;/font&gt;&lt;span style="COLOR: blue"&gt;datetime&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;set&lt;/span&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;font color="#000000"&gt; @timetest &lt;/font&gt;&lt;span style="COLOR: gray"&gt;=&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="COLOR: fuchsia"&gt;getdate&lt;/span&gt;&lt;span style="COLOR: gray"&gt;()&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;set&lt;/span&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;font color="#000000"&gt; @refdate &lt;/font&gt;&lt;span style="COLOR: gray"&gt;=&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="COLOR: red"&gt;'30 Dec 1899 00:00:00'&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; COLOR: red; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;select&lt;/span&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;font color="#000000"&gt; @timetest&lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;&lt;font color="#000000"&gt;      &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;font color="#000000"&gt; @refdate &lt;o:p&gt;&lt;/o:p&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;&lt;font color="#000000"&gt;      &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="COLOR: fuchsia"&gt;dateadd&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;day&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="COLOR: fuchsia"&gt;datediff&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;day&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;font color="#000000"&gt; @refdate &lt;/font&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;font color="#000000"&gt; @timetest&lt;/font&gt;&lt;span style="COLOR: gray"&gt;),&lt;/span&gt;&lt;font color="#000000"&gt; @refdate &lt;/font&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="COLOR: green"&gt;-- date part only&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class="MsoNormal"&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;&lt;font color="#000000"&gt;      &lt;/font&gt;&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="COLOR: fuchsia"&gt;dateadd&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;day&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="COLOR: fuchsia"&gt;datediff&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;day&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;font color="#000000"&gt; @refdate &lt;/font&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;font color="#000000"&gt; @timetest&lt;/font&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="COLOR: gray"&gt;*&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="COLOR: gray"&gt;-&lt;/span&gt;&lt;font color="#000000"&gt;1&lt;/font&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;font color="#000000"&gt; @timetest&lt;/font&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="COLOR: green"&gt;-- time part only, with reference date&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"&gt;&lt;o:p&gt;&lt;font color="#000000" size="3" face="Times New Roman"&gt; &lt;/font&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"&gt;&lt;font color="#000000"&gt;&lt;font size="3" face="Times New Roman"&gt;Instead of &lt;/font&gt;&lt;span style="FONT-FAMILY: &amp;quot;Courier New&amp;quot;; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;@refdate, &lt;/span&gt;&lt;font size="3" face="Times New Roman"&gt;you could use 0.&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p style="MARGIN: 0cm 0cm 0pt" class="MsoNormal"&gt;&lt;o:p&gt;&lt;font color="#000000" size="3" face="Times New Roman"&gt; &lt;/font&gt;&lt;/o:p&gt;&lt;/p&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt; &lt;img src="http://geekswithblogs.net/Rhames/aggbug/134021.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rhames</dc:creator>
            <guid>http://geekswithblogs.net/Rhames/archive/2009/08/11/getting-the-time-part-only-from-a-sql-server-datetime.aspx</guid>
            <pubDate>Tue, 11 Aug 2009 11:24:50 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Rhames/comments/134021.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Rhames/archive/2009/08/11/getting-the-time-part-only-from-a-sql-server-datetime.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/Rhames/comments/commentRss/134021.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Rhames/services/trackbacks/134021.aspx</trackback:ping>
        </item>
        <item>
            <title>SQL server script to generate CREATE INDEX commands</title>
            <category>SQL Server</category>
            <link>http://geekswithblogs.net/Rhames/archive/2008/11/21/sql-server-script-to-generate-create-index-commands.aspx</link>
            <description>&lt;p&gt;Originally posted on: &lt;a href='http://geekswithblogs.net/Rhames/archive/2008/11/21/sql-server-script-to-generate-create-index-commands.aspx'&gt;http://geekswithblogs.net/Rhames/archive/2008/11/21/sql-server-script-to-generate-create-index-commands.aspx&lt;/a&gt;&lt;/p&gt;&lt;div style="MARGIN: 0cm 0cm 10pt"&gt;This script generates the “CREATE INDEX” scripts for a SQL Server database:&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;set&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;nocount&lt;/span&gt; &lt;span style="COLOR: blue"&gt;on&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;declare&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @index &lt;span style="COLOR: blue"&gt;table&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;(&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt; &lt;span style="COLOR: blue"&gt;int&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      objectName &lt;span style="COLOR: blue"&gt;sysname&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      index_id &lt;span style="COLOR: blue"&gt;int&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      indexName &lt;span style="COLOR: blue"&gt;sysname&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      fill_factor &lt;span style="COLOR: blue"&gt;tinyint&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: blue"&gt;allow_row_locks&lt;/span&gt; &lt;span style="COLOR: blue"&gt;bit&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: blue"&gt;allow_page_locks&lt;/span&gt; &lt;span style="COLOR: blue"&gt;bit&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      is_padded &lt;span style="COLOR: blue"&gt;bit&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      indexText &lt;span style="COLOR: blue"&gt;varchar&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;),&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      indexTextEnd &lt;span style="COLOR: blue"&gt;varchar&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;declare&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @indexColumn &lt;span style="COLOR: blue"&gt;table&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;(&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt; &lt;span style="COLOR: blue"&gt;int&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      index_id &lt;span style="COLOR: blue"&gt;int&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      column_id &lt;span style="COLOR: blue"&gt;int&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      index_column_id &lt;span style="COLOR: blue"&gt;int&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      max_index_column_id &lt;span style="COLOR: blue"&gt;int&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      is_descending_key &lt;span style="COLOR: blue"&gt;bit&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      is_included_column &lt;span style="COLOR: blue"&gt;bit&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      columnName &lt;span style="COLOR: blue"&gt;varchar&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;255&lt;span style="COLOR: gray"&gt;),&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      indexText &lt;span style="COLOR: blue"&gt;varchar&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;null&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;insert&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;into&lt;/span&gt; @index &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;select&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: fuchsia"&gt;object_name&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt;&lt;span style="COLOR: gray"&gt;),&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;index_id&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;name&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      fill_factor&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: blue"&gt;allow_row_locks&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: blue"&gt;allow_page_locks&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      is_padded&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: red"&gt;'CREATE NONCLUSTERED INDEX ['&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;name &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: red"&gt;'] ON [dbo].['&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;object_name&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: red"&gt;'] '&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: blue"&gt;char&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;13&lt;span style="COLOR: gray"&gt;),&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: red"&gt;'WITH (PAD_INDEX = '&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;CASE&lt;/span&gt; &lt;span style="COLOR: blue"&gt;WHEN&lt;/span&gt; is_padded &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; 1 &lt;span style="COLOR: blue"&gt;THEN&lt;/span&gt; &lt;span style="COLOR: red"&gt;' ON '&lt;/span&gt; &lt;span style="COLOR: blue"&gt;ELSE&lt;/span&gt; &lt;span style="COLOR: red"&gt;' OFF '&lt;/span&gt; &lt;span style="COLOR: blue"&gt;END&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: red"&gt;', STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = '&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;CASE&lt;/span&gt; &lt;span style="COLOR: blue"&gt;WHEN&lt;/span&gt; &lt;span style="COLOR: blue"&gt;allow_row_locks&lt;/span&gt; &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; 1 &lt;span style="COLOR: blue"&gt;THEN&lt;/span&gt; &lt;span style="COLOR: red"&gt;' ON '&lt;/span&gt; &lt;span style="COLOR: blue"&gt;ELSE&lt;/span&gt; &lt;span style="COLOR: red"&gt;' OFF '&lt;/span&gt; &lt;span style="COLOR: blue"&gt;END&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: red"&gt;', ALLOW_PAGE_LOCKS = '&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;CASE&lt;/span&gt; &lt;span style="COLOR: blue"&gt;WHEN&lt;/span&gt; &lt;span style="COLOR: blue"&gt;allow_page_locks&lt;/span&gt; &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; 1 &lt;span style="COLOR: blue"&gt;THEN&lt;/span&gt; &lt;span style="COLOR: red"&gt;' ON '&lt;/span&gt; &lt;span style="COLOR: blue"&gt;ELSE&lt;/span&gt; &lt;span style="COLOR: red"&gt;' OFF '&lt;/span&gt; &lt;span style="COLOR: blue"&gt;END&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;CASE&lt;/span&gt; &lt;span style="COLOR: blue"&gt;WHEN&lt;/span&gt; fill_factor &lt;span style="COLOR: gray"&gt;&amp;gt;&lt;/span&gt; 0 &lt;span style="COLOR: blue"&gt;THEN&lt;/span&gt; &lt;span style="COLOR: red"&gt;', FILLFACTOR = '&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;convert&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: blue"&gt;varchar&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;3&lt;span style="COLOR: gray"&gt;),&lt;/span&gt; fill_factor&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: blue"&gt;ELSE&lt;/span&gt; &lt;span style="COLOR: red"&gt;''&lt;/span&gt; &lt;span style="COLOR: blue"&gt;END&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: red"&gt;')'&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: blue"&gt;CHAR&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;13&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;from&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: green"&gt;sys.indexes&lt;/span&gt; i&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;where&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;type &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; 2 &lt;span style="COLOR: gray"&gt;and&lt;/span&gt; i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;is_unique_constraint &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; 0&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;and&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: fuchsia"&gt;objectproperty&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt; &lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;span style="COLOR: red"&gt;'IsUserTable'&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; 1 &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;order&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;by&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;object_name&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt;&lt;span style="COLOR: gray"&gt;),&lt;/span&gt; i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;name&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;insert&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;into&lt;/span&gt; @indexColumn&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;select&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;index_id&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      ic&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;column_id&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      ic&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;index_column_id&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;ic&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;index_column_id&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: blue"&gt;over&lt;/span&gt; &lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: blue"&gt;partition&lt;/span&gt; &lt;span style="COLOR: blue"&gt;by&lt;/span&gt;      i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;index_id&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; is_included_column&lt;span style="COLOR: gray"&gt;),&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      is_descending_key&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      is_included_column&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: red"&gt;'['&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; c&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;name &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: red"&gt;']'&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: gray"&gt;null&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;from&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @index i &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;join&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: green"&gt;sys.index_columns&lt;/span&gt; ic&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;on&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt; &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; ic&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;and&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;index_id &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; ic&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;index_id&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;join&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: green"&gt;sys.columns&lt;/span&gt; c&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;on&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; ic&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt; &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; c&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;and&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; ic&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;column_id &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; c&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;column_id&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;order&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;by&lt;/span&gt; i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;index_id&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; ic&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;index_column_id&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;declare&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @fields &lt;span style="COLOR: blue"&gt;varchar&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;max&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;declare&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @object_id &lt;span style="COLOR: blue"&gt;int&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; @index_id &lt;span style="COLOR: blue"&gt;int&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;select&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @fields &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: gray"&gt;null,&lt;/span&gt; @object_id &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: gray"&gt;-&lt;/span&gt;1&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; @index_id &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: gray"&gt;-&lt;/span&gt;1&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;update&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @indexColumn&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;set&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @fields &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; indexText &lt;span style="COLOR: gray"&gt;=&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: blue"&gt;case&lt;/span&gt; &lt;span style="COLOR: blue"&gt;when&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt; &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;isnull&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;@object_id&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;and&lt;/span&gt; index_id &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;isnull&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;@index_id&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; index_id&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;then&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;isnull&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;@fields &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: red"&gt;', '&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;span style="COLOR: red"&gt;' '&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; columnName &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: blue"&gt;case&lt;/span&gt; &lt;span style="COLOR: blue"&gt;when&lt;/span&gt; is_descending_key &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; 0 &lt;span style="COLOR: blue"&gt;then&lt;/span&gt; &lt;span style="COLOR: red"&gt;' ASC'&lt;/span&gt; &lt;span style="COLOR: blue"&gt;else&lt;/span&gt; &lt;span style="COLOR: red"&gt;' DESC'&lt;/span&gt; &lt;span style="COLOR: blue"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;else&lt;/span&gt; columnName &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: blue"&gt;case&lt;/span&gt; &lt;span style="COLOR: blue"&gt;when&lt;/span&gt; is_descending_key &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; 0 &lt;span style="COLOR: blue"&gt;then&lt;/span&gt; &lt;span style="COLOR: red"&gt;' ASC'&lt;/span&gt; &lt;span style="COLOR: blue"&gt;else&lt;/span&gt; &lt;span style="COLOR: red"&gt;' DESC'&lt;/span&gt; &lt;span style="COLOR: blue"&gt;end&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;end&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      @object_id &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: blue"&gt;case&lt;/span&gt; &lt;span style="COLOR: blue"&gt;when&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt; &lt;span style="COLOR: gray"&gt;&amp;lt;&amp;gt;&lt;/span&gt; @object_id &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;then&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt; &lt;span style="COLOR: blue"&gt;else&lt;/span&gt; @object_id &lt;span style="COLOR: blue"&gt;end&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      @index_id &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: blue"&gt;case&lt;/span&gt; &lt;span style="COLOR: blue"&gt;when&lt;/span&gt; index_id &lt;span style="COLOR: gray"&gt;&amp;lt;&amp;gt;&lt;/span&gt; @index_id&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;then&lt;/span&gt; index_id &lt;span style="COLOR: blue"&gt;else&lt;/span&gt; @index_id &lt;span style="COLOR: blue"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;from&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @indexColumn&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;where&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; is_included_column &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; 0&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;select&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @fields &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: gray"&gt;null,&lt;/span&gt; @object_id &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: gray"&gt;-&lt;/span&gt;1&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; @index_id &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: gray"&gt;-&lt;/span&gt;1&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;update&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @indexColumn&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;set&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @fields &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; indexText &lt;span style="COLOR: gray"&gt;=&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: blue"&gt;case&lt;/span&gt; &lt;span style="COLOR: blue"&gt;when&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt; &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;isnull&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;@object_id&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;and&lt;/span&gt; index_id &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;isnull&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;@index_id&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; index_id&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;then&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;isnull&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;@fields &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: red"&gt;', '&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;span style="COLOR: red"&gt;' '&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; columnName &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;else&lt;/span&gt; columnName &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;end&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      @object_id &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: blue"&gt;case&lt;/span&gt; &lt;span style="COLOR: blue"&gt;when&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt; &lt;span style="COLOR: gray"&gt;&amp;lt;&amp;gt;&lt;/span&gt; @object_id &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;then&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt; &lt;span style="COLOR: blue"&gt;else&lt;/span&gt; @object_id &lt;span style="COLOR: blue"&gt;end&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      @index_id &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: blue"&gt;case&lt;/span&gt; &lt;span style="COLOR: blue"&gt;when&lt;/span&gt; index_id &lt;span style="COLOR: gray"&gt;&amp;lt;&amp;gt;&lt;/span&gt; @index_id&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;then&lt;/span&gt; index_id &lt;span style="COLOR: blue"&gt;else&lt;/span&gt; @index_id &lt;span style="COLOR: blue"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;from&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @indexColumn&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;where&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; is_included_column &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; 1&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;update&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @index&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;set&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; indexText &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;indexText &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: red"&gt;'( '&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: blue"&gt;char&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;13&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: blue"&gt;char&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;9&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; ic&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;indexText &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: blue"&gt;char&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;13&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: red"&gt;') '&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;from&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @index i &lt;span style="COLOR: gray"&gt;join&lt;/span&gt; @indexColumn ic&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;on&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt; &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; ic&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;and&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;index_id &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; ic&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;index_id&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;and&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; ic&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;index_column_id &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; ic&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;max_index_column_id &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;and&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; ic&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;is_included_column &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; 0&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;update&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @index&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;set&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; indexText &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;indexText &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: red"&gt;'INCLUDE ( '&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: blue"&gt;char&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;13&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: blue"&gt;char&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;9&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; ic&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;indexText &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: blue"&gt;char&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;13&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: red"&gt;') '&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;from&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @index i &lt;span style="COLOR: gray"&gt;join&lt;/span&gt; @indexColumn ic&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;on&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt; &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; ic&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;object_id&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;and&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; i&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;index_id &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; ic&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;index_id&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;and&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; ic&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;index_column_id &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; ic&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;max_index_column_id &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;and&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; ic&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;is_included_column &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; 1&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;update&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @index&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;set&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; indexText &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; indexText &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; indexTextEnd&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;from&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @index&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;select&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; indexText&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: green"&gt;--, objectName, indexName &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;from&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @index &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt; &lt;/div&gt; &lt;img src="http://geekswithblogs.net/Rhames/aggbug/127272.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rhames</dc:creator>
            <guid>http://geekswithblogs.net/Rhames/archive/2008/11/21/sql-server-script-to-generate-create-index-commands.aspx</guid>
            <pubDate>Fri, 21 Nov 2008 12:30:01 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Rhames/comments/127272.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Rhames/archive/2008/11/21/sql-server-script-to-generate-create-index-commands.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/Rhames/comments/commentRss/127272.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Rhames/services/trackbacks/127272.aspx</trackback:ping>
        </item>
        <item>
            <title>SQL Server datetime - getting the date part only</title>
            <category>SQL Server</category>
            <link>http://geekswithblogs.net/Rhames/archive/2008/11/19/sql-server-datetime---getting-the-date-part-only.aspx</link>
            <description>&lt;p&gt;Originally posted on: &lt;a href='http://geekswithblogs.net/Rhames/archive/2008/11/19/sql-server-datetime---getting-the-date-part-only.aspx'&gt;http://geekswithblogs.net/Rhames/archive/2008/11/19/sql-server-datetime---getting-the-date-part-only.aspx&lt;/a&gt;&lt;/p&gt;&lt;div style="MARGIN: 0cm 0cm 10pt"&gt;I am always forgetting the format for using DATEADD and DATEPART functions to get just part of a SQL Server datetime (e.g. just the date), so I am posting it here so I’ll always know where to find it.&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;declare&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @date &lt;span style="COLOR: blue"&gt;datetime&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;set&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @date &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;getdate&lt;/span&gt;&lt;span style="COLOR: gray"&gt;()&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 115%"&gt;select&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%"&gt; @date&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;dateadd&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;day&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;datediff&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;day&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; 0&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; @date&lt;span style="COLOR: gray"&gt;),&lt;/span&gt; 0&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt; &lt;/div&gt; &lt;img src="http://geekswithblogs.net/Rhames/aggbug/127229.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rhames</dc:creator>
            <guid>http://geekswithblogs.net/Rhames/archive/2008/11/19/sql-server-datetime---getting-the-date-part-only.aspx</guid>
            <pubDate>Wed, 19 Nov 2008 15:17:14 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Rhames/comments/127229.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Rhames/archive/2008/11/19/sql-server-datetime---getting-the-date-part-only.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/Rhames/comments/commentRss/127229.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Rhames/services/trackbacks/127229.aspx</trackback:ping>
        </item>
        <item>
            <title>SQL Server CHECKSUM() with Single Quotes (‘) gives duplicate values.</title>
            <category>SQL Server</category>
            <link>http://geekswithblogs.net/Rhames/archive/2008/10/30/sql-server-checksum-with-single-quotes--gives-duplicate-values.aspx</link>
            <description>&lt;p&gt;Originally posted on: &lt;a href='http://geekswithblogs.net/Rhames/archive/2008/10/30/sql-server-checksum-with-single-quotes--gives-duplicate-values.aspx'&gt;http://geekswithblogs.net/Rhames/archive/2008/10/30/sql-server-checksum-with-single-quotes--gives-duplicate-values.aspx&lt;/a&gt;&lt;/p&gt;&lt;div style="MARGIN: 24pt 0cm 0pt"&gt;&lt;strong&gt;&lt;font size="6"&gt;&lt;font color="#365f91" size="5"&gt;SQL Server CHECKSUM() with Single Quotes (‘) gives duplicate values.&lt;/font&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt;A database I have been working on uses a checksum across address data to try to establish whether an entered address already exists in the database.&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt;A problem occurred because two addresses had the same checksum value, but the actual address was subtlety different. The difference was that one had the street address as “St James’ Street”, whereas the other had “St James Street”.&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt;The following test script shows that the single quote does not appear to alter the checksum calculation:&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;declare&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @t1 &lt;span style="COLOR: blue"&gt;table&lt;/span&gt; &lt;span style="COLOR: gray"&gt;(&lt;/span&gt;s &lt;span style="COLOR: blue"&gt;varchar&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;50&lt;span style="COLOR: gray"&gt;))&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;insert&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;into&lt;/span&gt; @t1 &lt;span style="COLOR: blue"&gt;values&lt;/span&gt; &lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: red"&gt;'10 St James Street'&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;insert&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;into&lt;/span&gt; @t1 &lt;span style="COLOR: blue"&gt;values&lt;/span&gt; &lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: red"&gt;'10 St James'' Street'&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;insert&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;into&lt;/span&gt; @t1 &lt;span style="COLOR: blue"&gt;values&lt;/span&gt; &lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: red"&gt;'1''0'' S''t'' J''a''m''e''s'' S''t''r''e''e''t'&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;select&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: fuchsia"&gt;checksum&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;s&lt;span style="COLOR: gray"&gt;),&lt;/span&gt; &lt;span style="COLOR: gray"&gt;*&lt;/span&gt; &lt;span style="COLOR: blue"&gt;from&lt;/span&gt; @t1&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt;These three rows all give the same checksum value (178448437).&lt;/div&gt; &lt;img src="http://geekswithblogs.net/Rhames/aggbug/126387.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rhames</dc:creator>
            <guid>http://geekswithblogs.net/Rhames/archive/2008/10/30/sql-server-checksum-with-single-quotes--gives-duplicate-values.aspx</guid>
            <pubDate>Thu, 30 Oct 2008 10:58:40 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Rhames/comments/126387.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Rhames/archive/2008/10/30/sql-server-checksum-with-single-quotes--gives-duplicate-values.aspx#feedback</comments>
            <slash:comments>4</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/Rhames/comments/commentRss/126387.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Rhames/services/trackbacks/126387.aspx</trackback:ping>
        </item>
        <item>
            <title>Why you should always specify the SqlDbType for an ADO.NET SqlParameter object.</title>
            <category>SQL Server</category>
            <category>ADO.NET</category>
            <link>http://geekswithblogs.net/Rhames/archive/2008/10/29/why-you-should-always-specify-the-sqldbtype-for-an-ado.net.aspx</link>
            <description>&lt;p&gt;Originally posted on: &lt;a href='http://geekswithblogs.net/Rhames/archive/2008/10/29/why-you-should-always-specify-the-sqldbtype-for-an-ado.net.aspx'&gt;http://geekswithblogs.net/Rhames/archive/2008/10/29/why-you-should-always-specify-the-sqldbtype-for-an-ado.net.aspx&lt;/a&gt;&lt;/p&gt;&lt;div style="MARGIN: 24pt 0cm 0pt"&gt;&lt;strong&gt;&lt;font size="6"&gt;&lt;font color="#365f91" size="5"&gt;How not specifying the ADO.NET SqlParameter DbType can lead to a horrible query execution plan!&lt;/font&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt;I was looking into a query that was performing much worse than expected. The query was a simple select of the form:&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;select&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      e&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;EmployeeID&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: fuchsia"&gt;count&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;r&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;EmployeeRoleID&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;from&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; Employee e&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;left&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: gray"&gt;join&lt;/span&gt; EmployeeRole r&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: blue"&gt;on&lt;/span&gt; e&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;EmployeeID &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; r&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;EmployeeID&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;where&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; e&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;EmployeeRef &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; @EmployeeRef&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 115%"&gt;group&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%"&gt; &lt;span style="COLOR: blue"&gt;by&lt;/span&gt; e&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;EmployeeID&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt;I expected the query plan for this to be fairly straightforward, a couple of Index Seeks and a Left Outer Join, but on examining the query plan, I saw something horrendous. There were 3 Nest Outer Joins, and a Sort that was taking 53% of the execution cost.&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt;Further examination revealed that although the EmployeeRef field on the Employee table was a varchar(50), the @EmployeeRef parameter was being passed in as a nvarchar(6)!&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt;I tracked this down to the use of the ADO.NET SqlParameter(String, Object) constructor. The developer had used this constructor thus:&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: #2b91af"&gt;SqlParameter&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; param1 = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: #2b91af"&gt;SqlParameter&lt;/span&gt;(&lt;span style="COLOR: #a31515"&gt;"@EmployeeRef"&lt;/span&gt;, &lt;span style="COLOR: #a31515"&gt;"XXX123"&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;sqlCommand.Parameters.Add(param1);&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt;This constructor only initialises the name and the value of the SqlParameter object. As the SqlDbType is not specified, the CLR infers the type, in this case as nvarchar(6).&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt;I changed the above code to:&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;sqlCommand.Parameters.Add(&lt;span style="COLOR: #a31515"&gt;"@EmployeeRef"&lt;/span&gt;, &lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 108pt; TEXT-INDENT: 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: #2b91af"&gt;SqlDbType&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt;.VarChar, 50).Value = &lt;span style="COLOR: #a31515"&gt;"XXX123"&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt;This gave the expected query execution plan, and the cost of the statement was reduced by about 50%.&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt;I tend to avoid any usage of the SqlParameter constructors that do not specify the SqlDbType, unless I am sure I will set the type explicitly later. For the same reason I avoid the SqlParameter.AddWithValue() method:&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;sqlCommand.Parameters.AddWithValue(&lt;span style="COLOR: #a31515"&gt;"@EmployeeRef"&lt;/span&gt;, &lt;span style="COLOR: #a31515"&gt;"XXX123"&lt;/span&gt;);&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt;I have on more than one occasion seen the following code, which is obviously wrong, but can give very subtle errors:&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;sqlCommand.Parameters.AddWithValue(&lt;span style="COLOR: #a31515"&gt;"@EmployeeRef"&lt;/span&gt;, &lt;span style="COLOR: #2b91af"&gt;SqlDbType&lt;/span&gt;.Int);&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt; &lt;/div&gt; &lt;img src="http://geekswithblogs.net/Rhames/aggbug/126342.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rhames</dc:creator>
            <guid>http://geekswithblogs.net/Rhames/archive/2008/10/29/why-you-should-always-specify-the-sqldbtype-for-an-ado.net.aspx</guid>
            <pubDate>Wed, 29 Oct 2008 15:50:30 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Rhames/comments/126342.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Rhames/archive/2008/10/29/why-you-should-always-specify-the-sqldbtype-for-an-ado.net.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/Rhames/comments/commentRss/126342.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Rhames/services/trackbacks/126342.aspx</trackback:ping>
        </item>
        <item>
            <title>Creating SQL Wildcards with Recursion and a Common Table Expression in SQL Server 2005</title>
            <category>SQL Server</category>
            <link>http://geekswithblogs.net/Rhames/archive/2008/10/28/creating-sql-wildcards-with-recursion-and-a-common-table-expression.aspx</link>
            <description>&lt;p&gt;Originally posted on: &lt;a href='http://geekswithblogs.net/Rhames/archive/2008/10/28/creating-sql-wildcards-with-recursion-and-a-common-table-expression.aspx'&gt;http://geekswithblogs.net/Rhames/archive/2008/10/28/creating-sql-wildcards-with-recursion-and-a-common-table-expression.aspx&lt;/a&gt;&lt;/p&gt;&lt;div style="MARGIN: 24pt 0cm 0pt"&gt;&lt;strong&gt;&lt;font size="6"&gt;&lt;font color="#365f91" size="5"&gt;Creating Test Wildcards with a Common Table Expression in SQL Server 2005&lt;/font&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt;I needed to create a SQL Server table of test wildcard strings ranging from ‘AA%’ through to ‘ZZ%’. This seemed to be a prime candidate for using recursion with a Common Table Expression (CTE). After a little playing around I came up with the following:&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;with&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; wildcards &lt;span style="COLOR: blue"&gt;as&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;select&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;char&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;65&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: blue"&gt;char&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;65&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: red"&gt;'%'&lt;/span&gt; &lt;span style="COLOR: blue"&gt;as&lt;/span&gt; wildcard&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; 65 &lt;span style="COLOR: blue"&gt;as&lt;/span&gt; num1&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; 65 &lt;span style="COLOR: blue"&gt;as&lt;/span&gt; num2&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;union all&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;select&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;char&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;case&lt;/span&gt; &lt;span style="COLOR: blue"&gt;when&lt;/span&gt; num2 &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; 90 &lt;span style="COLOR: blue"&gt;then&lt;/span&gt; num1 &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; 1&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;else&lt;/span&gt; num1 &lt;span style="COLOR: blue"&gt;end&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="TEXT-INDENT: 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;+&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;char&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;case&lt;/span&gt; &lt;span style="COLOR: blue"&gt;when&lt;/span&gt; num2 &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; 90 &lt;span style="COLOR: blue"&gt;then&lt;/span&gt; 65&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;else&lt;/span&gt; num2 &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; 1 &lt;span style="COLOR: blue"&gt;end&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; &lt;span style="COLOR: red"&gt;'%'&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: blue"&gt;case&lt;/span&gt; &lt;span style="COLOR: blue"&gt;when&lt;/span&gt; num2 &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; 90 &lt;span style="COLOR: blue"&gt;then&lt;/span&gt; num1 &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; 1&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;else&lt;/span&gt; num1 &lt;span style="COLOR: blue"&gt;end&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;      &lt;span style="COLOR: blue"&gt;case&lt;/span&gt; &lt;span style="COLOR: blue"&gt;when&lt;/span&gt; num2 &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; 90 &lt;span style="COLOR: blue"&gt;then&lt;/span&gt; 65&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;            &lt;span style="COLOR: blue"&gt;else&lt;/span&gt; num2 &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; 1 &lt;span style="COLOR: blue"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;from&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; wildcards &lt;span style="COLOR: blue"&gt;where&lt;/span&gt; num1 &lt;span style="COLOR: gray"&gt;&amp;lt;&lt;/span&gt; 91&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;and&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: gray"&gt;(&lt;/span&gt;num1 &lt;span style="COLOR: gray"&gt;&amp;lt;&amp;gt;&lt;/span&gt; 90 &lt;span style="COLOR: gray"&gt;or&lt;/span&gt; num2 &lt;span style="COLOR: gray"&gt;&amp;lt;&amp;gt;&lt;/span&gt; 90&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt;Obviously, to actually put the results somewhere useful, I had an INSERT statement which selected from the Wildcards CTE:&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;insert&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;into&lt;/span&gt; TestFilter &lt;span style="COLOR: gray"&gt;(&lt;/span&gt;Filter&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;select&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; wildcard &lt;span style="COLOR: blue"&gt;from&lt;/span&gt; wildcards &lt;span style="COLOR: blue"&gt;option&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: blue"&gt;MaxRecursion&lt;/span&gt; 676&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt;Selecting the num1 and num2 values from the Wildcards CTE shows you what is going on:&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue; LINE-HEIGHT: 115%"&gt;select&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%"&gt; &lt;span style="COLOR: gray"&gt;*&lt;/span&gt; &lt;span style="COLOR: blue"&gt;from&lt;/span&gt; wildcards &lt;span style="COLOR: blue"&gt;option&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;&lt;span style="COLOR: blue"&gt;MaxRecursion&lt;/span&gt; 676&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt"&gt; &lt;/div&gt; &lt;img src="http://geekswithblogs.net/Rhames/aggbug/126284.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rhames</dc:creator>
            <guid>http://geekswithblogs.net/Rhames/archive/2008/10/28/creating-sql-wildcards-with-recursion-and-a-common-table-expression.aspx</guid>
            <pubDate>Tue, 28 Oct 2008 15:21:53 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Rhames/comments/126284.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Rhames/archive/2008/10/28/creating-sql-wildcards-with-recursion-and-a-common-table-expression.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/Rhames/comments/commentRss/126284.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Rhames/services/trackbacks/126284.aspx</trackback:ping>
        </item>
        <item>
            <title>Calculating Running Totals in SQL Server 2005, The optimal solution?</title>
            <category>SQL Server</category>
            <link>http://geekswithblogs.net/Rhames/archive/2008/10/28/calculating-running-totals-in-sql-server-2005---the-optimal.aspx</link>
            <description>&lt;p&gt;Originally posted on: &lt;a href='http://geekswithblogs.net/Rhames/archive/2008/10/28/calculating-running-totals-in-sql-server-2005---the-optimal.aspx'&gt;http://geekswithblogs.net/Rhames/archive/2008/10/28/calculating-running-totals-in-sql-server-2005---the-optimal.aspx&lt;/a&gt;&lt;/p&gt;&lt;div style="MARGIN: 24pt 0cm 0pt"&gt;&lt;strong&gt;&lt;font size="6"&gt;&lt;font size="5"&gt;&lt;font color="#365f91"&gt;Using “Update to a local variable” to calculate running totals in SQL. &lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;Recently I was looking at an existing view on a client's SQL server 2005 database. This view calculated the running total for a transaction amount from a table, but was performing very poorly.&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;I had always believed there were three different methods for calculating a running total using TSQL:&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt 36pt; TEXT-INDENT: -18pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;1.&lt;span style="FONT: 7pt 'Times New Roman'"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 9pt"&gt;Use a nested sub-query&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt 36pt; TEXT-INDENT: -18pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;2.&lt;span style="FONT: 7pt 'Times New Roman'"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 9pt"&gt;Use a self join&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt 36pt; TEXT-INDENT: -18pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;3.&lt;span style="FONT: 7pt 'Times New Roman'"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 9pt"&gt;Use Cursors&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;My own personal preference was to use the cursors option. If the cursor guidelines are followed, I've always found this to be the quickest, because the other two methods involve multiple scans of the table. The key for the cursor method is to ensure the data you are "cursoring" through is in the correct order, as the query optimzier does not understand cursors. This usually means cursoring through the data by clustered index, or copying the data into a temp table / table var first, in the relevant order.&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;A blog posted by Garth Wells back in 2001 gives these three techniques (&lt;a href="http://www.sqlteam.com/article/calculating-running-totals"&gt;&lt;span style="COLOR: blue"&gt;http://www.sqlteam.com/article/calculating-running-totals&lt;/span&gt;&lt;/a&gt;)&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;I came across a fourth technique for the running total calculation, which is related to the cursor method. Like the cursor method, it involves a single scan of the source table, then inserting the calculated running total for each row into a temp table or table variable. However, instead of using a cursor, it makes use of the following UPDATE command syntax:&lt;/span&gt;&lt;/div&gt;
&lt;div style="TEXT-INDENT: 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;UPDATE &lt;/span&gt;&lt;em&gt;&lt;span style="FONT-SIZE: 9pt"&gt;table&lt;/span&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt; TEXT-INDENT: 36pt; LINE-HEIGHT: normal"&gt;&lt;span&gt;SET&lt;em&gt; variable&lt;/em&gt; = &lt;/span&gt;&lt;em&gt;&lt;span style="FONT-SIZE: 9pt"&gt;column&lt;/span&gt;&lt;/em&gt;&lt;span style="FONT-SIZE: 9pt"&gt; = &lt;/span&gt;&lt;em&gt;&lt;span style="FONT-SIZE: 9pt"&gt;expression&lt;/span&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;The TSQL to calculate the running total is:&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;DECLARE&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @SalesTbl &lt;span style="COLOR: blue"&gt;TABLE&lt;/span&gt; &lt;span style="COLOR: gray"&gt;(&lt;/span&gt;DayCount &lt;span style="COLOR: blue"&gt;smallint&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; Sales &lt;span style="COLOR: blue"&gt;money&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; RunningTotal &lt;span style="COLOR: blue"&gt;money&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;DECLARE&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @RunningTotal &lt;span style="COLOR: blue"&gt;money&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;SET&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @RunningTotal &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; 0&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;INSERT&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;INTO&lt;/span&gt; @SalesTbl &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;SELECT&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; DayCount&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; Sales&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; &lt;span style="COLOR: gray"&gt;null&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;FROM&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; Sales&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;ORDER&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;BY&lt;/span&gt; DayCount&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;UPDATE&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @SalesTbl &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;SET&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @RunningTotal &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; RunningTotal &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; @RunningTotal &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; Sales&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;FROM&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @SalesTbl &lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;SELECT&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: gray"&gt;*&lt;/span&gt; &lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; @SalesTbl &lt;/span&gt;&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;I tested this query along with the other three methods on a simple set of test data (actually the same test data from Garth Wells’ blog mentioned above).&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;The results of my test runs are:&lt;/span&gt;&lt;/div&gt;
&lt;p&gt; &lt;/p&gt;
&lt;table style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; MARGIN: auto auto auto 62.1pt; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse" cellspacing="0" cellpadding="0" border="1"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 148.85pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent" valign="top" width="198"&gt;
            &lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt;&lt;strong&gt;&lt;span style="FONT-SIZE: 9pt"&gt;Method&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
            &lt;/td&gt;
            &lt;td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; BORDER-LEFT-COLOR: #d4d0c8; PADDING-BOTTOM: 0cm; WIDTH: 127.6pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent" valign="top" width="170"&gt;
            &lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt;&lt;strong&gt;&lt;span style="FONT-SIZE: 9pt"&gt;Time Taken &lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 148.85pt; BORDER-TOP-COLOR: #d4d0c8; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent" valign="top" width="198"&gt;
            &lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;Nested sub-query&lt;/span&gt;&lt;/div&gt;
            &lt;/td&gt;
            &lt;td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; BORDER-LEFT-COLOR: #d4d0c8; PADDING-BOTTOM: 0cm; WIDTH: 127.6pt; BORDER-TOP-COLOR: #d4d0c8; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent" valign="top" width="170"&gt;
            &lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;9300 ms&lt;/span&gt;&lt;/div&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 148.85pt; BORDER-TOP-COLOR: #d4d0c8; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent" valign="top" width="198"&gt;
            &lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;Self join&lt;/span&gt;&lt;/div&gt;
            &lt;/td&gt;
            &lt;td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; BORDER-LEFT-COLOR: #d4d0c8; PADDING-BOTTOM: 0cm; WIDTH: 127.6pt; BORDER-TOP-COLOR: #d4d0c8; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent" valign="top" width="170"&gt;
            &lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;6100 ms&lt;/span&gt;&lt;/div&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 148.85pt; BORDER-TOP-COLOR: #d4d0c8; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent" valign="top" width="198"&gt;
            &lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;Cursor&lt;/span&gt;&lt;/div&gt;
            &lt;/td&gt;
            &lt;td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; BORDER-LEFT-COLOR: #d4d0c8; PADDING-BOTTOM: 0cm; WIDTH: 127.6pt; BORDER-TOP-COLOR: #d4d0c8; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent" valign="top" width="170"&gt;
            &lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;400 ms&lt;/span&gt;&lt;/div&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 148.85pt; BORDER-TOP-COLOR: #d4d0c8; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent" valign="top" width="198"&gt;
            &lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;Update to local variable&lt;/span&gt;&lt;/div&gt;
            &lt;/td&gt;
            &lt;td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; BORDER-LEFT-COLOR: #d4d0c8; PADDING-BOTTOM: 0cm; WIDTH: 127.6pt; BORDER-TOP-COLOR: #d4d0c8; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid; BACKGROUND-COLOR: transparent" valign="top" width="170"&gt;
            &lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;140 ms&lt;/span&gt;&lt;/div&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;I was surprised just how much faster using the “Update to a local variable” method was. I expected it to be similar to the cursor method, as both involve a single scan of the source table, and both calculate the running total once only for each row in the table. The Nested Sub-query and Self join methods are so much slower because they involve the repeated recalculation of all of the previous running totals.&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;
&lt;p class="MsoNormal" style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto"&gt;&lt;strong style="mso-bidi-font-weight: normal"&gt;&lt;u&gt;&lt;span style="FONT-SIZE: 9pt; FONT-FAMILY: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;Note:&lt;/span&gt;&lt;/u&gt;&lt;/strong&gt;&lt;span style="FONT-SIZE: 9pt; FONT-FAMILY: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt; There is a pretty big assumption in using the “Update to local variable” method. This is that the Update statement will update the rows in the temp table in the correct order. There is no simple way to specify the order for an Update statement, so potentially this method could fail, although I have not seen this actually happen yet!&lt;/span&gt;&lt;/p&gt;
&lt;p class="MsoNormal" style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto"&gt;&lt;span style="FONT-SIZE: 9pt; FONT-FAMILY: &amp;quot;Arial&amp;quot;,&amp;quot;sans-serif&amp;quot;; mso-fareast-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;I &lt;em&gt;think&lt;/em&gt; that if I use a table variable, then the update will probably be in the correct order, because there are no indexes for the query optimizer to use, and parallellism will not occur. However, I can't be sure about this!&lt;/span&gt;&lt;/p&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;The following script was used to create the test data:&lt;/span&gt;&lt;/div&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;CREATE&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;TABLE&lt;/span&gt; Sales &lt;span style="COLOR: gray"&gt;(&lt;/span&gt;DayCount &lt;span style="COLOR: blue"&gt;smallint&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; Sales &lt;span style="COLOR: blue"&gt;money&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;CREATE&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;CLUSTERED&lt;/span&gt; &lt;span style="COLOR: blue"&gt;INDEX&lt;/span&gt; ndx_DayCount &lt;span style="COLOR: blue"&gt;ON&lt;/span&gt; Sales&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;DayCount&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;go&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;INSERT&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; Sales &lt;span style="COLOR: blue"&gt;VALUES&lt;/span&gt; &lt;span style="COLOR: gray"&gt;(&lt;/span&gt;1&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;120&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;INSERT&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; Sales &lt;span style="COLOR: blue"&gt;VALUES&lt;/span&gt; &lt;span style="COLOR: gray"&gt;(&lt;/span&gt;2&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;60&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;INSERT&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; Sales &lt;span style="COLOR: blue"&gt;VALUES&lt;/span&gt; &lt;span style="COLOR: gray"&gt;(&lt;/span&gt;3&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;125&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;INSERT&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; Sales &lt;span style="COLOR: blue"&gt;VALUES&lt;/span&gt; &lt;span style="COLOR: gray"&gt;(&lt;/span&gt;4&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;40&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;DECLARE&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @DayCount &lt;span style="COLOR: blue"&gt;smallint&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; @Sales &lt;span style="COLOR: blue"&gt;money&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;SET&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @DayCount &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; 5&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;SET&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @Sales &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; 10&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;WHILE&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @DayCount &lt;span style="COLOR: gray"&gt;&amp;lt;&lt;/span&gt; 5000&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;&lt;span style="COLOR: blue"&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;blockquote dir="ltr" style="MARGIN-RIGHT: 0px"&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;&lt;span style="COLOR: blue"&gt;INSERT&lt;/span&gt; Sales &lt;span style="COLOR: blue"&gt;VALUES&lt;/span&gt; &lt;span style="COLOR: gray"&gt;(&lt;/span&gt;@DayCount&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;@Sales&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;&lt;span style="COLOR: blue"&gt;SET&lt;/span&gt; @DayCount &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; @DayCount &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; 1&lt;/span&gt;&lt;/div&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;&lt;span style="COLOR: blue"&gt;SET&lt;/span&gt; @Sales &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; @Sales &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; 15&lt;/span&gt;&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div style="LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;&lt;span style="COLOR: blue"&gt;END&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;The queries used in my tests for the other three methods are posted below:&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt 36pt; TEXT-INDENT: -18pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;1.&lt;span style="FONT: 7pt 'Times New Roman'"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 9pt"&gt;&lt;strong&gt;Nested Sub-query&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;SELECT&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; DayCount&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;       Sales&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;       Sales&lt;span style="COLOR: gray"&gt;+&lt;/span&gt;&lt;span style="COLOR: fuchsia"&gt;COALESCE&lt;/span&gt;&lt;span style="COLOR: gray"&gt;((&lt;/span&gt;&lt;span style="COLOR: blue"&gt;SELECT&lt;/span&gt; &lt;span style="COLOR: fuchsia"&gt;SUM&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;Sales&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;                      &lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; Sales b &lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;                      &lt;span style="COLOR: blue"&gt;WHERE&lt;/span&gt; b&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;DayCount &lt;span style="COLOR: gray"&gt;&amp;lt;&lt;/span&gt; a&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;DayCount&lt;span style="COLOR: gray"&gt;),&lt;/span&gt;0&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;                         &lt;span style="COLOR: blue"&gt;AS&lt;/span&gt; RunningTotal&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;FROM&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; Sales a&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;ORDER&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;BY&lt;/span&gt; DayCount&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt 36pt; TEXT-INDENT: -18pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;2.&lt;span style="FONT: 7pt 'Times New Roman'"&gt;    &lt;strong&gt; &lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 9pt"&gt;&lt;strong&gt;Self join&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;SELECT&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; a&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;DayCount&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;       a&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;Sales&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;       &lt;span style="COLOR: fuchsia"&gt;SUM&lt;/span&gt;&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;b&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;Sales&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;FROM&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; Sales a&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: gray"&gt;INNER &lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt;&lt;span style="COLOR: gray"&gt;JOIN&lt;/span&gt; Sales b&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;ON&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: gray"&gt;(&lt;/span&gt;b&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;DayCount &lt;span style="COLOR: gray"&gt;&amp;lt;=&lt;/span&gt; a&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;DayCount&lt;span style="COLOR: gray"&gt;)&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;GROUP&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;BY&lt;/span&gt; a&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;DayCount&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;a&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;Sales&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;ORDER&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;BY&lt;/span&gt; a&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;DayCount&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;a&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;Sales&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; TEXT-INDENT: -18pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 9pt"&gt;3.&lt;span style="FONT: 7pt 'Times New Roman'"&gt;     &lt;/span&gt;&lt;/span&gt;&lt;span style="FONT-SIZE: 9pt"&gt;&lt;strong&gt;Cursor&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt 36pt"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;DECLARE&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @SalesTbl &lt;span style="COLOR: blue"&gt;TABLE&lt;/span&gt; &lt;span style="COLOR: gray"&gt;(&lt;/span&gt;DayCount &lt;span style="COLOR: blue"&gt;smallint&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; Sales &lt;span style="COLOR: blue"&gt;money&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; RunningTotal &lt;span style="COLOR: blue"&gt;money&lt;/span&gt;&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;DECLARE&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @DayCount &lt;span style="COLOR: blue"&gt;smallint&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;        @Sales &lt;span style="COLOR: blue"&gt;money&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt;        @RunningTotal &lt;span style="COLOR: blue"&gt;money&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;SET&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; @RunningTotal &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; 0&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;DECLARE&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; rt_cursor &lt;span style="COLOR: blue"&gt;CURSOR&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;FOR&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;SELECT&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; DayCount&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; Sales&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;FROM&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; Sales&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;ORDER&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;BY&lt;/span&gt; DayCount&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;OPEN&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; rt_cursor&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;FETCH&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;NEXT&lt;/span&gt; &lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; rt_cursor &lt;span style="COLOR: blue"&gt;INTO&lt;/span&gt; @DayCount&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;@Sales&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;WHILE&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: fuchsia"&gt;@@FETCH_STATUS&lt;/span&gt; &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; 0&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;SET&lt;/span&gt; @RunningTotal &lt;span style="COLOR: gray"&gt;=&lt;/span&gt; @RunningTotal &lt;span style="COLOR: gray"&gt;+&lt;/span&gt; @Sales&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;INSERT&lt;/span&gt; @SalesTbl &lt;span style="COLOR: blue"&gt;VALUES&lt;/span&gt; &lt;span style="COLOR: gray"&gt;(&lt;/span&gt;@DayCount&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;@Sales&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;@RunningTotal&lt;span style="COLOR: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;FETCH&lt;/span&gt; &lt;span style="COLOR: blue"&gt;NEXT&lt;/span&gt; &lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; rt_cursor &lt;span style="COLOR: blue"&gt;INTO&lt;/span&gt; @DayCount&lt;span style="COLOR: gray"&gt;,&lt;/span&gt;@Sales&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: blue"&gt;END&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;CLOSE&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; rt_cursor&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;DEALLOCATE&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; rt_cursor&lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt; &lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal"&gt;&lt;span style="FONT-SIZE: 10pt; COLOR: blue"&gt;SELECT&lt;/span&gt;&lt;span style="FONT-SIZE: 10pt"&gt; &lt;span style="COLOR: gray"&gt;*&lt;/span&gt; &lt;span style="COLOR: blue"&gt;FROM&lt;/span&gt; @SalesTbl &lt;/span&gt;&lt;/div&gt;
&lt;div style="MARGIN: 0cm 0cm 10pt; LINE-HEIGHT: normal"&gt; &lt;/div&gt; &lt;img src="http://geekswithblogs.net/Rhames/aggbug/126277.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rhames</dc:creator>
            <guid>http://geekswithblogs.net/Rhames/archive/2008/10/28/calculating-running-totals-in-sql-server-2005---the-optimal.aspx</guid>
            <pubDate>Tue, 28 Oct 2008 10:22:45 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/Rhames/comments/126277.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/Rhames/archive/2008/10/28/calculating-running-totals-in-sql-server-2005---the-optimal.aspx#feedback</comments>
            <slash:comments>15</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/Rhames/comments/commentRss/126277.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/Rhames/services/trackbacks/126277.aspx</trackback:ping>
        </item>
    </channel>
</rss>