.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
Easy Web
Imran Ghani

Home >> Forum >> OOPS >> Post New QuestionBookmark and Share Subscribe to Forum

SingleTon Design Patterns

Posted By: BangaruBabuPureti     Posted Date: April 12, 2010    Points:2   Category :OOPS
How Can We apply Single Ton Design Patters in ASP.NET

Any Body Explain With Example


Thanks In Advance

Responses
Author: Amit Mehra             
Posted Date: April 12, 2010     Points: 5   

Hi,

Yes we can because when a page is rendered than it can do lot of operation.


public sealed class Singleton
{
static Singleton instance=null;
static readonly object padlock = new object();

Singleton()
{
}

public static Singleton Instance
{
get
{
lock (padlock)
{
if (instance==null)
{
instance = new Singleton();
}
return instance;
}
}
}
}


http://www.yoda.arachsys.com/csharp/singleton.html

Thanks,
Amit
Please "Mark As Answer " if this post help you.
Author: BangaruBabuPureti             
Posted Date: April 13, 2010     Points: 5   

if possible Explain This Thing In Real Time DB example or provide a link and

What are The Uses and Limitations of Singleton Design Patterns

BangaruBabu Pureti
http://bangarubabupureti.spaces.live.com/
Author: Rishi             
Posted Date: April 20, 2010     Points: 5   

Hi,

Following is the code you use in your main or in Data Access Layer
// Instantiates a singleton DB Class
DBSingleton singleton = DBSingleton.Instance;
// Gets the connection object
SqlConnection myCon = singleton.GetConnection();

Following is the code uses singleton class for DB connection

public sealed class DBSingleton
{
private static DBSingleton instance = null; // To store the only instance of the Singleton class
private string strConnString = string.Empty; // Stores the connection string
private SqlConnection con; // Handle to the DB connection

/// <summary>
/// Private constructor for one time initalization Process
/// </summary>
private DBSingleton()
{
try
{
// Getting the connection string from the config file
strConnString = ConfigurationSettings.AppSettings["ConnString"].ToString();
OpenDBConnection(); // Openning the connection

// You can implement one time initialization code here
}
catch (Exception ex)
{
MessageBox.Show("Error in the DB Connection : " + ex.Message.ToString().Trim());
}
}

// Destructor for closing the connection
~DBSingleton()
{
CloseDBConnection(); // Metho to close the connection
}

/// <summary>
/// Opens the connection
/// </summary>
private void OpenDBConnection()
{
if (strConnString != null && strConnString.Trim().Length > 0)
con = new SqlConnection(strConnString);
}

/// <summary>
/// This method returns the connection Object to the user
/// This functionality can be refined as to avoid exposing the connection to the outside worl
/// All the DB functionalities can be built inside the same library
/// </summary>
/// <returns></returns>
public SqlConnection GetConnection()
{
if (con.State == ConnectionState.Open)
return con;
else
return null;
}

/// <summary>
/// Closes the opened connection
/// </summary>
private void CloseDBConnection()
{
if (con.State == ConnectionState.Open) // Checks for the Open State
con.Close();
}

/// <summary>
/// Instanct Property for the DBSingleton Class
/// </summary>
public static DBSingleton Instance // Property for DBSingleton Class Instatiation
{
get
{
if (instance == null) // It cecks whether already instantiated or not
{
instance = new DBSingleton(); // For the first time instatiating
}
return instance; // Returns the only instance of the singleton class
}
}
}

Thanks
Rishi
Please "Mark As Answer " if this post help you.

Download Attachment



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend