ASP.NET Tip: Create a Smart Property to Instantiate Your Objects

When I'm writing Web applications, I may need to use lots of objects at various times while the application is running, but I generally don't always need all of them. Each object that gets instantiated takes system memory, and I try to keep my applications as small as possible for the best performance.

As a solution, I create "smart properties" to instantiate my objects. These properties allow me to keep a single instance of an object without worrying about whether the object has been instantiated yet. My code simply references the property, and the property figures out whether the object has been created yet or not.

Here's an example that manages a database connection:

private SqlConnection conn;
protected SqlConnection ActiveConnection
{
   get
   {
      if (conn == null)
      {
         conn = new SqlConnection("connectionstring");
         conn.Open();
      }
      return conn;
   }
}

The first time the ActiveConnection property is called, the conn object is null. It then is instantiated and opened using a connection string, which could be coming from the Web.config file or somewhere else. The SqlConnection object is kept for each subsequent call to the ActiveConnection property. The calling code simply references the ActiveConnection property instead of looking at the actual SqlConnection variable, thus eliminating the need to check whether the connection is open each time.

I use this type of property frequently with my BasePage class and take care of closing this or other similar objects in the Unload event of the page. That allows the connection to be released to the connection pool and used again later.

About the Author

Eric Smith is the owner of Northstar Computer Systems, a Web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic.



Comments

  • Synchronization...

    Posted by chmelar on 06/01/2006 11:50am

    It should be synchronized if you want to access the property from different threads:
    
    private object connSync = new object();
    private SqlConnection conn;
    protected SqlConnection ActiveConnection
    {
       get
       {
          lock (connSync)
          {
              if (conn == null)
              {
                 conn = new SqlConnection("connectionstring");
                 conn.Open();
              }
          }
    
          return conn;
       }
    }

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Managing your company's financials is the backbone of your business and is vital to the long-term health and viability of your company. To continue applying the necessary financial rigor to support rapid growth, the accounting department needs the right tools to most efficiently do their job. Read this white paper to understand the 10 essentials of a complete financial management system and how the right solution can help you keep up with the rapidly changing business world.

  • With 81% of employees using their phones at work, companies have stopped asking: "Is corporate data leaking from personal devices?" and started asking: "How do we effectively prevent corporate data from leaking from personal devices?" The answer has not been simple. ZixOne raises the bar on BYOD security by not allowing email data to reside on the device. In addition, Zix allows employees to maintain complete control of their personal device, therefore satisfying privacy demands of valued employees and the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds