C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Example for Singleton Pattern


Posted Date: 06 Aug 2008    Resource Type: Code Snippets    Category: Design Patterns
Author: JeyaprakashMember Level: Silver    
Rating: Points: 10



This code sample explains how to construct and use Singleton objects. The first example gives how to define the pattern. Followed by this is an example on how to obtain reference to such object. The third example calls methods on the object.

SINGLETON PATTERN IMPLEMENTATION

#region Singleton Pattern
// SINGLETON PATTERN IMPLEMENTATION
private static SqlConnection conn = null;

public static SqlConnection Instance()
{
if (conn == null)
{
conn = new SqlConnection(SQLHelper.CONN);
}
return conn;
}
#endregion


NORMAL CALL FOR REFERENCE

#region NORMAL CALL FOR REFERENCE
// Do not use this
// Normal Implementation - This code has to be replaced by singleton pattern
using (SqlConnection conn = new SqlConnection(SQLHelper.CONN))
#endregion


SINGLETON CALL

#region SINGLETON CALL
// SINGLETON CALL
using (Instance())
{
conn.Open();
using (SqlTransaction trans = conn.BeginTransaction())
{
try
{
SQLHelper.ExecuteNonQuery(trans, CommandType.StoredProcedure, INSERTSIGNON, signOnParms);
SQLHelper.ExecuteNonQuery(trans, CommandType.StoredProcedure, INSERTACCOUNT, addressParms);
trans.Commit();

}
catch
{
trans.Rollback();
throw;
}
}
}
#endregion





Responses

Author: pradipta    17 Aug 2008Member Level: Gold   Points : 2
This is not correct way of implementing singleton pattern.The method is not thread safe ,so there is a chance of multiple instances.See the following line

if (conn == null) { conn = new SqlConnection(SQLHelper.CONN); }

suppose two threads executing the above statements simultaneously then there is a chance of multiple instances.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Singleton object pattern  .  Singleton object  .  Singleton classes  .  Singleton class  .  Pattern for singleton objects  .  Example for Singleton Pattern  .  Creating singleton objects  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Singleton Pattern in C#
Previous Resource: Dynamically add Style for ASP.NET Controls
Return to Discussion Resource Index
Post New Resource
Category: Design Patterns


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use