Sleeping Cat

Lazy Cat

Progress isn’t made by early risers. It’s made by lazy men trying to find easier ways to do something.
Robert A. Heinlein

Lazy loading is a design pattern to defer initialization of objects until needed.  Reading data from a database can take time. Validation tables are typically static for the life of the application. These tables need only be read once.

If all the validation records are read at once, the load time of the application may be lengthy. Some developers show a splash screen to cover the lengthy load time. Yet, not every form of the application uses every validation table and the user rarely uses every screen. Therefore, some of the validation tables read from the database are not needed.

A better practice is to use Lazy Loading.

Only access the database when needed. The application is available for the user much quicker. The load requirements are small for each form and the user rarely notices the first time only performance hit.

Another reason to use Lazy Loading is to insure proper initialization. For a large application with a first time initialization method, the order of initialization may be critical. As the application grows or changes, the developer needs to maintain the initialization code. With Lazy Loading, initialization happens when needed and in the proper order without further developer intervention.

The example below shows some simple Lazy Loading. The database connection string method, GetConnectionString() is purposely left unspecified. Maybe it is reading a config file or perhaps it is calling a Web Service or a user pop up form gathers the data. For Lazy Loading, any of these options will work.

Start with a simple class to hold the data.  Just two simple properties, Name and Description.

public class MajorData
{
  public string Name {get; set; }
  public string Description {get; set; }
}

AppConfig is a simple static class that is also using Lazy Loading.  The ConnectionString is a read only property.  If ConectionString is null, call the private static method, GetConnectionString.  This method could return a hard coded string, or it could read the App.config file, or it could call a web service.  How this method is implemented, is unimportant.  The consumer of the AppConfig class will only access the ConnectionString property.

public static class AppConfig
{
  private static string _ConnectionString = null;
  public static string ConnectionString
  {
    get
    {
      if (_ConnectionString == null)
        _ConnectionString = GetConnectionString();

      return _ConnectionString;
    }
  }
}

In the AppConfig static class, I frequently have lists of data that are constant for the life of the application.  This lists are used to populate drop down lists or validate data.  The static property shown below is only read from the database once.  The database read occurs only if some portion of the program needs the data in MajorList.

private static List<MajorData> _MajorList = null;
public static List<MajorDat> MajorList
{
  get
  {
    if (_MajorList == null)
      _MajorList = MajorAction.GetMajorList();

    return _MajorList;
  }
}

The listing of the method, GetMajorList, is shown below.  I do this to show the usage of the Lazy Loaded ConnectionString and the best practice of database connection.  The using statement can be used with any class that implements the interface, IDisposable.  For a database connection, dispose will release any resources even if an exception is thrown.

public static class MajorAction
{
  public static List<MajorData> GetMajorList()
  {
    List<MajorData> majorList = new List<MajorData>();
    using (SqlConnection sqlCon = new SqlConnection(AppConfig.ConnectionString))
    {
      // Fill SQL Command Text
      // Execute SQL
      // Fill List
    }

    return majorList;
  }
}

Tags:

Leave a Reply


*