Some of you find it useful

Listing of my everyday findings about .NET

  • nature
  • nature
  • nature
  • nature
  • nature
  • nature

Donut Caching with ASP.NET Substitution control

by tariqulazam

We all are using ASP.NET Caching features to improve the performance of our site and we all know about very widely used caching terms like 'Output caching','Fragment Caching' etc. For those who are not familiar Google is your friend.

Today I am not going to show how caching can be used rather I will describe a hidden gem in the cache infrastructure of ASP.NET. I am calling it hidden and Gem at the same time as I did not encounter any scenario before where this could be used (or I did not even think about caching…) and where applicable it could be very handy. It’s called ‘Donut caching’ and as the name implies, it is like a hole in the cached output where you can programmatically set value.

Let me tell you the scenario that I encounter today at work. We have a fairly large application and we are in the process of optimizing the performance of that application. It has got a Header User Control and a couple of controls in the header are populated based on logged in user roles. The logged in user name also displayed in the header. For some database operation heavy page (search result page), we want to implement output caching, but the barrier was the header control. We do not want to redesign the page and encapsulate the search result grid into another User control and implement fragment caching there. Here comes the ASP.NET Substitution control in our rescue.

To start with, let us look at the mark-up of the Substitution control first.

<asp:substitution methodname="GetUserName" runat="Server" id="subUserName"></asp:substitution>

The html rendered by this control is the hole in the donut. The only important and useful bit is the MethodName property. It should reference a static method and should return a string. So whatever string you return from the GetUserName method will be rendered in the output. For displaying the logged name user name we have used the following method. For populating other controls we create the markup as necessary in the similar fashion.

 Public static string GetUserName(HttpContext context){  	       
         return Session["UserDisplayName"].ToString();   
 }  

So far, everything was easy, but I am pretty sure you will run into issue soon, as most of the developer depends on Session for keeping some user specific information, like Display Name, etc. The above mentioned code will work fine on the first instance but you will encounter a NullReference exception when you come back to the page later. It is because, for the cache version of the page, the page was never created and hence no session. Despite this limitation, asp:Substitution is a very handy control, that could save you some of your time and headache.

I have attached a very simple website project here. Download it and see the asp:substitution control in action. I hope that helps and give you an idea when and how to use the substitution control.

Post category: ASP.NET, C#

An extensible and testable N-Tier application architecture

by tariqulazam

I am pretty sure that all the developers out there have tried at least once to develop their application using N-Tier Application architecture. As I remember, I did the same and many times I ended up with application layers where the functional responsibilities are spread over the layers. As the application grew, it became a nightmare to maintain the code. Once I became familiar with the Unit Testing, I tried to create Unit Test for some of my previous written projects and I failed in almost every scenario. The deficiencies in those designs were

- Code was not extensible.
- Code was not loosely coupled.
- No clear separation betweens Layers.
- Not Testable

In this article, I will try to show you how to create an application which does not have all these deficiencies. There are other designs available out there but I found this approach quiet easy to understand, extensible and maintainable. I have used NUnit Framework for creating the Unit Test. So you will need to have NUnit installed on your PC, if you want to run the Unit Test. I have also attached a sample application here with the application. So, if you think code is enough for you to understand everything, download the sample project and stop reading here.

Let’s start with a very simple problem scenario. We want to build an application which will be able to Register Students and will allow students to enrol for some courses. I have attached a sql script of a simple database which contains only three tables – Students, Courses and StudentCourses.

‘Program to an interface, not an implementation’ – is the first principle of reusable object oriented programming and you may hear about other design principles like ‘Open for extension but closed for modification’, commonly known as (OCP) open close principle. In short these principles imply to use abstraction in order to decouple your system from the dependency. In our problem domain, the system has dependency to Database. By following and implementing those principles, we will be able to create a Business Logic Layer Assembly which we can use in all types of projects and the Data Access Layer will be extensible enough to support different data sources like SQL, Oracle, XML etc. Most importantly we will be able to Unit Test the assemblies.

Our solution contains five individual projects
   1. Business Logic Layer (Class Library Project) – contains business object definition, collections and data Service interfaces.
   2. Data Access Layer (Class Library Project) – contains different implementation of provider specific data access classes.
   3. ServiceLocator (Class Library Project) – contains class which is used to register and locate different service instances.
   4. UnitTest (Class Library Project) – Contains couple of unit tests.
   5. TestApp (Windows console application)

I always like to keep my business objects classes very simple; it will have just the properties it requires and the constructors. Let’s have a look at the student class

public class Student
    {
        private int _studentId;
        public int StudentId {
            get { return _studentId; }
            set { _studentId = value; }
        }

        private string _firstName;
        public string FirstName {
            get { return _firstName; }
            set { _firstName = value; }
        }

        private string _lastName;
        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }

        private Courses _courses;
        public Courses RegisteredCourses { 
            get{
                if (_courses == null)
                    _courses = ((ICourseDataService)ServiceLocator.
                        Instance.Resolve<ICourseDataService>()).GetCoursesByStudentId(this.StudentId);
                return _courses;
            }
        }

        public Student(int studentId) {
            this._studentId = studentId;
        }

        public Student(int studentId, string firstName, string lastName) {
            this._studentId = studentId;
            this._firstName = firstName;
            this._lastName = lastName;
        }
    }

It has nothing to describe except the RegisteredCourses property and how it gets it value. I will come back to it later in this article. The collections classes are inheriting from Generic List class. I like to keep it in this way because later we can extend them to implement sorting and filtering functionality.

Our Data Access classes implements the data service interface defined in the BLL project. Have a look at one of them. We have abstracted the student related database functionality in an Interface called IStudentDataService which has the following signature

public interface IStudentDataService {
   Students GetAllStudents();
   Student GetStudentById(int studentId);
   Student InsertStudent(string firstName, string lastName); 
}

Now see the SQL Server implementation of IStudentDataService interface. I have used Microsoft Enterprise Library to implement this.

public class StudentSqlDataService:IStudentDataService
    {
        #region IStudentDataService Members

        public Students GetAllStudents()
        {
            Students students = new Students();
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand cmd = db.GetStoredProcCommand(StudentProcedures.PROC_GET_ALLSTUDENTS);
            DataSet ds = null;
            ds = db.ExecuteDataSet(cmd);
            foreach (DataRow dr in ds.Tables[0].Rows) {
                students.Add(Make(dr));
            }
            return students;
        }

        

        public Student GetStudentById(int studentId)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand cmd = db.GetStoredProcCommand(StudentProcedures.PROC_GET_STUDNETBYID);
            db.AddInParameter(cmd, "@StudentId", DbType.Int32, studentId);
            IDataReader reader = db.ExecuteReader(cmd);
            if (reader != null)
                return Make(reader[0] as DataRow);

            return null;
        }

        public Student InsertStudent(string firstName, string lastName)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand cmd = db.GetStoredProcCommand(StudentProcedures.PROC_INSERT_STUDENT);
            db.AddInParameter(cmd, "@FirstName", DbType.String, firstName);
            db.AddInParameter(cmd, "@LastName", DbType.String, lastName);
            IDataReader reader = db.ExecuteReader(cmd);
            if (reader != null)
                return Make(reader[0] as DataRow);

            return null;
        }

        #endregion

        private Student Make(DataRow dr) {
            return new Student(Convert.ToInt32(dr["StudentId"]), Convert.ToString(dr["FirstName"]), Convert.ToString(dr["LastName"]));
        }
    }

Once the DAL is ready to use, its now time to see the service locator project. I am not going to describe the service locator concept in details. Google is your friend and the one that i have used in my project is copied from the very well written article by Stefano Ricciardi. You will find it here. I just extend this to register services. 

If you are familiar with Unity Application Block, it has got a container which also can be used as a service locator. Service locator holds the relationship between the abstract and concrete instance. Now have a look at the RegisteredCourses property on the Student object. It is asking for a Concrete instance of ICourseDataService and calls GetCoursesByStudentId method from it. We have implemented it using the lazy pattern as we don’t want to hit the database unless we need to have the registered courses of a student.

Now registering the services is the responsibility of the client Application. Here we have UnitTest and Console as two different host application. We have implemented another implementation of IStudentDataService called StudentMockDataServices to be used in the Unit Test.  So whatever services are registered in the host application, the application will be able to use them. Registering the services is a one time task and ideally you should do it at the start of the application. For web application Global.asax Application_Start is the perfect place for this. Service registration is also implemented using lazy pattern, so it will not block the application loading for generating the concrete service instances. The concrete instances will be created only when required. Lets see the code for the service registration.

private static void RegisterServices() {
    ServiceLocator.Instance.RegisterService<IStudentDataService, StudentSqlDataService>();
    ServiceLocator.Instance.RegisterService<ICourseDataService, CourseSqlDataService>();
}

I have included a sample database script. Run this script on a blank SQL database and it will create the tables, stored procedures and populate the tables with some sample data to see the console application on action. This same approach can be used to decouple other application dependency like Settings, Event Logging etc.  

I hope it helps. I would like to hear your comments and questions you may have regarding this. I would also like to take this opportunity to thank Refky Wahib, my manager at Center for Learning Innvoation, for kindly reviewing my codes and teaching me many things.

Post category: C#, Design Pattern

Implementing Conditional GET in ASP.NET Website

by tariqulazam

Today I am going to show you how I have implemented Condition Get for my website tariqulazam.info. Those who are not familiar with conditional get can have a look at the definition of conditional Get below.

From Http1.1 spcification : The semantics of the GET method change to a "conditional GET" if the request message includes an If-Modified-Since, If-Unmodified-Since, If-Match, If-None-Match, or If-Range header field. A conditional GET method requests that the entity be transferred only under the circumstances described by the conditional header field(s). The conditional GET method is intended to reduce unnecessary network usage by allowing cached entities to be refreshed without requiring multiple requests or transferring data already held by the client.

To understand the scenario clearly, let’s have a look at the following two http response header of tariqulazam.info homepage, one for the initial request and the other is for when the user returns back to the home page later. I have used HttpFox to capture this headers. 

Response Header Response header

Figure 1: Response headers for two request for same page when Conditional Get is not used.

If you look at the (status-line) and Content-Length header, you will see that status-line and the content length is same for both request. That indicates even if the content of the home page is not modified during the time between this two visits, every request to the home page cost us almost the same amount of data transfer. Conditional Get comes in to action in this scenario by reducing the data transfer for the subsequent requests to the same page.

So, if you understand what conditional get is, it is now time to have a closer look at the request header. When a client visits a webpage for the first time, in short when the client does not have cached entities for this webpage, the request header does not have any of these request header fields

If-Modified-Since, If-Unmodified-Since, If-Match, If-None-Match

When the server receives the request and it does not find any of those header fields, it just process the request and send the response to the client with a status code 200(OK). Next time when the client request the same page, it sends If-Modified-Since and If-None-Match request header fields and based on this values the server decides whether it should process the request or just send an 304 (Not-Modified) status code to the client. Note that the content length will be 0 if a 304 response code is sent. How the server will decide is dependent on the developer and it may vary depending on individual situations. 

Request Header Response header

Figure 2: Request and Response headers for first request on tariqulazam.info homepage when conditional get is enabled.

Request Header Response header.

Figure 3: Request and Response headers for subsequent request on tariqulazam.info homepage when conditional get is enabled.

If you have still difficulties understanding Conditional GET, read this article to have a more closer look at the headers and what they do.

Lets see some C# code now on how to implement this in our website. I have attached a complete website, so you can now download it and have a look.

Start by putting a Webhelper.cs class file in the APP_Code folder. It has got just 1 public static method and 2 private static methods. I want to keep the algorithm pretty simple to find out when the content of the website has changed. For tariqulazam.info website, when a resource link, article or comments is added or updated, I keep the modified time in a static field and used that value to generate E-Tag for the content being supplied by the IIS. Later when the same page is requested I just check the If-Modified-Since and If-None-Match request header value to determine whether I should process the request or just send response code 304 without processing the request. But it is totally upto you when you send the not modified response.

Note: If you find cached response for subsequent request try a hard refresh using F5 key to get the Not Modified Header. Also try to run this sample directly by creating application on your IIS. Casini (Visual Studio built-in webserver) does not handle some of these headers properly.

Please feel free to ask any question or leave any comment you may have about this article.

Post category: ASP.NET, C#, Others

« Newer Posts Older Posts »
Subscribe to RSS

Recent Posts

Recently Added Links

Article Archive

Article Categories

Article Tag Cloud

application performance asp.net c# caching cdn conditional get css design pattern donut caching etag html5 javascript n-tier application oop tips and tricks visual studio website performance

Links Categories

Link Tag Cloud

.net ado.net asp.net c# deployment design pattern entity data model features iphone jquery monotouch oop performance ria service silverlight tutorial video wcf website performance website project

Visitor Map