Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Singleton Design Pattern in Asp.net using C#

, 8 Sep 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
codeproject Introduction When we want to make a only one instance of a class and also making sure that  there is a global access point to that object then the design pattern we user is called Singleton. The pattern ensures that the class is instantiated only once and that all requests are di

Introduction

When we want to make a only one instance of a class and also making sure that  there is a global access point to that object then the design pattern we user is called Singleton. The pattern ensures that the class is instantiated only once and that all requests are directed to that one and only object.  

Usage

In asp.net you can use singleton through sessions or application variables easily however i will show you how to implement singleton object  to User class.  

Problem

In Asp.net to get user information like first name, surname or username  through Memebership object does not look neat because  to get UserID first you need to know user User.Identity.Name then use Membership.GetUser in order to get more detail for user.

Solution

I have created a interface called IUser which you can use anywhere on the page.

	public interface IUser
      {
          string Email { get; set; }
          string Firstname { get; set; }
          string Surname { get; set; }
          string UserID { get; set; }
          string Username { get; set; }
      }

Here is our Singleton class implementing IUser.

 

 public sealed class Singleton
       {
           private const string IUserSessionName = "User";
           private static readonly object padlock = new object();
           private static IUser objUser ;
           Singleton()
           {

           }
           /// <span class="code-SummaryComment"><summary>
</span>           /// Loading user information
           /// <span class="code-SummaryComment"></summary>
</span>           public static IUser IUserInstance
           {
               get
               {
                   lock (padlock)
                   {
                                              
                          if (null == Session[IUserSessionName])
                          {
                           
                           string userID = Membership.GetUser().ProviderUserKey.ToString();
                           // getting userinformation from database
                           objUser = new UserClass().GetUserInformation(userID);
                           Session[IUserSessionName] = objUser;
                          }
                          else
                          {
                          objUser = (IUser)Current.Session[IUserSessionName];
                          }
                      
           
                      return objUser;
                  }
               }
               set
               {
                   Session[IUserSessionName] = value;
               }
           }
          
           public static void UserInstanceFlush()
           {
               Session[IUserSessionName] = null;
           }

       }

Because of IuserSessionName  you are also making sure that you will keep this session name unique and  at only one place.

IUser is a static property which can be used on any page from Singleton.IUserInstance.UserID.

UserInstanceFlush  flushes the session if you want to remove the session value.

Note: People may argue its not a pure Singleton object example because its one instance of object per user and i agree with all of them its a one instance of object per user when they get authenticated throughout for all pages in application and give you extra user properties through the interface IUser. 

your feed is welcome.

View this article on my blog

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Salmanzz
Software Developer (Senior) BMJ
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
QuestionPeace Guys! Here we go.. Pinmembern_zaheer_ahmed15-Dec-14 4:07 
Generalsingleton pattern PinmemberJava Experience1-Feb-13 0:11 
Generalanother implementation of Singleton using C# Pinmembervn_nilesh26-Jun-12 4:51 
GeneralMy vote of 1 PinmemberPogoboyMtK10-Sep-10 10:54 
GeneralLots of work for replacing 1 lousy line PinmemberJV99999-Sep-10 4:40 
GeneralRe: Lots of work for replacing 1 lousy line PinmemberSalmanzz9-Sep-10 4:47 
GeneralRe: Lots of work for replacing 1 lousy line PinmemberJV99999-Sep-10 4:58 
GeneralMy vote of 1 Pinmembersadf235edgg2321-Jul-10 5:33 
GeneralThread Safety PinmemberDetoX8316-Aug-09 11:02 
GeneralRe: Thread Safety PinmemberSalmanzz16-Aug-09 11:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 8 Sep 2010
Article Copyright 2009 by Salmanzz
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid