Implementing Simple Cache for CodeSmith and NetTiers (ASP.NET 2.0)

Posted by Peter Kellner on November 02, 2006 · 7 mins read

In this article, I will show a simple way to implement Cache for a GetData type call in the SqlClient layer of the generated code. I won't go through all the details of getting to this point but will show the code and briefly explain how it works. I'm new to CodeSmith so I'm sure there is a better way to solve this problem, but for now, this is one technique that works.

Just to give a lay of the land, my project is called peterkellnerblog and it has several subprojects that each create their own dll's. They are:

  • c:\..\..\peterkellnerblog.Website
  • peterkellnerblog.Data
  • peterkellnerblog.Data.SqlClient
  • peterkellnerblog.Entities
  • peterkellnerblog.Web

    The project we are going to focus on is peterkelnerblog.Data.SqlClient. In this project, there already is a method that gets data with the following signature (this file is SqlWpCommentProviderBase.generate.cs).

     public override peterkellnerblog.Entities.TList<WpComments>


                TransactionManager transactionManager,

                System.Int32 commentPostID, int start,

                int pageLength, out int count)



    This file you are not meant to change (notice that is has the word "generated" in the name). The file you can modify, which is SqlWpCommentProvider.cs. What you essentially do is call the above file and add Cache to the end of the name. That way, if you want a Cached method, you use the new name instead. Here is the file below.

     #region Using directives


    using System;

    using System.Data;

    using System.Collections;

    using System.Diagnostics;

    using Microsoft.Practices.EnterpriseLibrary.Data;

    using System.ComponentModel;


    using peterkellnerblog.Entities;

    using peterkellnerblog.Data;

    using System.Web.Caching;

    using System.Web;




    namespace peterkellnerblog.Data.SqlClient



        /// This class is the SqlClient Data Access Logic Component implementation for the <see cref="WpComments"/> entity.




        public partial class SqlWpCommentsProvider: SqlWpCommentsProviderBase


            /// <summary>

            /// Creates a new <see cref="SqlWpCommentsProvider"/> instance.

            /// Uses connection string to connect to datasource.

            /// </summary>

            /// <param name="connectionString">The connection string to the database.</param>

            /// <param name="useStoredProcedure">A boolean value that indicates if we use the stored procedures or embedded queries.</param>

            /// <param name="providerInvariantName">Name of the invariant provider use by the DbProviderFactory.</param>

            public SqlWpCommentsProvider(string connectionString, bool useStoredProcedure, string providerInvariantName): base(connectionString, useStoredProcedure, providerInvariantName){}


            /// <summary>


            /// </summary>

            /// <param name="transactionManager"></param>

            /// <param name="commentPostID"></param>

            /// <param name="start"></param>

            /// <param name="pageLength"></param>

            /// <param name="count"></param>

            /// <returns></returns>

            public override peterkellnerblog.Entities.TList<WpComments>

                GetByCommentPostIDCache(TransactionManager transactionManager,

                System.Int32 commentPostID, int start, int pageLength,

                out int count)


                int newCnt;

                string cacheKey = "WpComments_GetByCommentPostID" +

                    commentPostID.ToString() + "_" +

    String() + "_" +



                TList<WpComments> tListWpComments = null;

                if (HttpContext.Current.Cache[cacheKey] != null)


                    tListWpComments =


                    count = tListWpComments.Count;




                    tListWpComments = base.GetByCommentPostID

                        (transactionManager, commentPostID, start, pageLength,

                        out newCnt);

                    HttpContext.Current.Cache.Insert(cacheKey, tListWpComments,

                        null, DateTime.Now.AddSeconds(15), TimeSpan.FromDays(0));

                    count = newCnt;


                count = tListWpComments.Count;

                return tListWpComments;




    The above method simply use the built in ASP.NET Cache class.