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

  • Not long ago, security was viewed as one of the biggest obstacles to widespread adoption of cloud-based deployments for enterprise software solutions. However, the combination of advancing technology and an increasing variety of threats that companies must guard against is rapidly turning the tide. Cloud vendors typically offer a much higher level of data center and virtual system security than most organizations can or will build out on their own. Read this white paper to learn the five ways that cloud …

  • Live Event Date: September 17, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this upcoming eSeminar and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds