Domain Objects Caching Pattern for .NET

Abstract

Caching greatly improves application performance because it reduces expensive trips to the database. But, if you want to use caching in your application, you must decide what to cache and where to put your caching code. The answer is simple. Cache your domain objects and put caching code inside your persistence classes.

Domain objects are central to any application and represent its core data and business validation rules. And, although domain objects may keep some read-only data, most of the data is transactional and changes frequently. Therefore, you cannot simply keep domain objects as “global variables” for the entirety of your application because the data will change in the database and your domain objects will become stale, thereby causing data integrity problems. You’ll have to use a proper caching solution for this. And, your options are ASP.NET Cache, Caching Application Block in Microsoft Enterprise Library, or some commercial solution such as NCache from Alachisoft. Personally, I would advise against using ASP.NET Cache because it forces you to cache from the presentation layer (ASP.NET pages), which is bad.

The best place to embed caching in your application is your domain objects’ persistence classes. In this article, I am extending an earlier design pattern I wrote called Domain Objects Persistence Pattern for .NET. I will show you how you can incorporate intelligent caching into your application to boost its performance and what considerations you should keep in mind while doing that.

Domain Objects Caching Pattern attempts to provide a solution for domain object caching. The domain objects in this pattern are unaware of the classes that persist them or whether they’re being cached or not, because the dependency is only one-way. This makes the domain object design much simpler and easier to understand. It also hides the caching and persistence code from other subsystems that are using the domain objects. This also works in distributed systems where only the domain objects are passed around.

Problem Definition

Domain objects form the backbone of any application. They capture data model from the database and also the business rules that apply to this data. It is very typical for most subsystems of an application to rely on these common domain objects. And, usually applications spend most of their time in either loading or saving these domain objects to the database. The actual “processing time” of these objects is very small specially for N-Tier applications where each “user request” is very short.

This means that performance of the application depends greatly on how quickly these domain objects can be made available to the application. If the application has to make numerous database trips, the performance is usually bad. But, if the application caches these objects close-by, the performance improves greatly.

At the same time, it is very important that domain object caching code is kept in such a central place that no matter who loads or saves the domain objects, the application automatically interacts with the cache. Additionally, we must hide the caching code from the rest of application so we can take it out easily if needed.

Solution

As described above, the solution is an extension of an existing design pattern called Domain Objects Persistence Pattern for .NET. That pattern already achieves the goal of separating domain objects from persistence code and from the rest of the application as well. This double-decoupling provides a great deal of flexibility in the design. The domain objects and the rest of the application is totally unaffected whether the data is coming from a relational database or any other source (for example, XML, flat files, or Active Directory/LDAP).

Therefore, the best place to embed caching code is in the persistence classes. This ensures that no matter which part of the application issues the load or save call to domain objects, caching is appropriately referenced first. This also hides all the caching code from rest of the application and lets you replace it with something else should you choose to do so.

Domain and Persistence Classes

In this sample, you will look at an Employee class from the Northwind database mapped to the “Employees” table in the database.

// Domain object "Employee" that holds your data
public class Employee {
   // Some of the private data members
   // ...
   public Employee() {}

   // Properties for Employee object
   public String    EmployeeId { get {return _employeeId;}
                                 set {_employeeId = value;}}
   public String    Title { get {return _title;}
                            set {_title = value;}}
   public ArrayList Subordinates { get {return _subordinates;}
                                   set {_subordinates = value;}}
}

// Interface for the Employee persistence
public interface IEmployeeFactory
{
   // Standard transactional methods for single-row operations
   void Load(Employee emp);
   void Insert(Employee emp);
   void Update(Employee emp);
   void Delete(Employee emp);

   // Load the related Employees (Subordinates) for this Employee
   void LoadSubordinates(Employee emp);

   // Query method to return a collection of Employee objects
   ArrayList FindByTitle(String title);
}

// Implementation of Employee persistence
public class EmployeeFactory : IEmployeeFactory
{
   // all methods described in interface above are implemented here
}

// A FactoryProvider to hide persistence implementation
public class FactoryProvider
{
   // To abstract away the actual factory implementation
   public static IEmployeeFactory GetEmployeeFactory() {
      return new EmployeeFactory(); }
}

Sample Application

Below is an example of how a client application will use this code.

public class NorthwindApp
{
   static void Main (string[] args) {
      Employee emp = new Employee();
      IEmployeeFactory iEmpFactory =
         FactoryProvider.GetEmployeeFactory();

      // Let's load an employee from the Northwind database.
      emp.EmployeeId = 2;
      iEmpFactory.load(emp);

      // Pass on the Employee object
      HandleEmployee(emp);
      HandleSubordinates(emp.Subordinates);

      // empList is a collection of Employee objects
      ArrayList empList = iEmpFactory.FindByTitle("Manager");
   }
}

The code above shows you the overall structure of your classes for handling domain objects persistence and caching. As you can see, there is clear-cut separation between the domain and persistence classes. And, there is an additional FactoryProvider class that lets you hide the persistence implementation from rest of the application. However, the domain objects (Employee in this case) moves around throughout the application.

Creating Cache Keys

Most cache systems provide you with a string-based key. At the same time, the data that you cache consists of various different classes (“Customers”, “Employees”, “Orders”, and so forth). In this situation, an EmployeeId of 1000 may conflict with an OrderId of 1000 if your keys do not contain any type information. Therefore, you need to store some type information as part of the key as well. Below are some suggested key structures. You can make up your own based on the same principles.

Keys for individual objects

If you’re only storing individual objects, you can make up your keys as following:

  • Customers:PK:1000: This means Customers object with primary key of 1000.

Keys for related objects

For each individual object, you may also want to keep related objects so you can easily find them. Here are keys for that:

  • Customers:PK:1000:REL:Orders: This means an Orders collection for Customer with primary key of 1000.

Keys for query results

Sometimes, you run queries that return a collection of objects. And, these queries may also take different run-time parameters each time. You want to store these query results so the next time you don’t have to run the query. Here are the keys for that. Please note that these keys also include run-time parameter values:

  • Employees:QRY:FindByTitleAndAge:Manager:40: This represents a query in “Employees” class called “FindByTitleAndAge” that takes two run-time parameters. The first parameter is “Title” and second is “Age”. And, their runtime parameter values are specified.

More by Author

Must Read