Calling the Singleton Design Pattern c# Class
namespace OrderProcessing.BusinessLayer
{
public class BLLClass
{
public BLLClass()
{
//
// TODO: Add constructor logic here
//
}
public string bllAddClass(ClassModelAddUpdateRequst Requst)
{
string response = "error";
response = Helper.Instance.ExecuteScalar(
commandText: "USP_CLASS"
, commandType: CommandType.StoredProcedure
, sqlParameters:
new SqlParameter[]{
new SqlParameter("@action", SqlDbType.Char, 20) { Value =Requst.action }
,new SqlParameter("@ClassNameNumber", SqlDbType.Int) { Value =Requst.ClassNameNumber }
,new SqlParameter("@ClassNameText", SqlDbType.VarChar, 20) { Value = Requst.ClassNameText }
,new SqlParameter("@ClassNameRoman", SqlDbType.NVarChar, 20) { Value = Requst.ClassNameRoman }
,new SqlParameter("@ClassNameOdia", SqlDbType.NVarChar, 20) { Value = Requst.ClassNameOdia }
,new SqlParameter("@EntryBy", SqlDbType.VarChar, 20) { Value = Requst.userId }
}
);
return response;
}
}
Singleton Design Pattern c# Class
public sealed class Helper
{
private Helper() { }
private static Helper instance = null;
public static Helper Instance
{
get
{
if (instance == null)
{
instance = new Helper();
}
return instance;
}
}
private string objCon;
private SqlConnection Conn;
public SqlConnection Connect()
{
objCon = ConfigurationManager.ConnectionStrings["sqlServerConn"].ConnectionString;
Conn = new SqlConnection(objCon);
if (Conn.State == ConnectionState.Open)
Conn.Close();
Conn.Open();
return Conn;
}
public int ExecuteNonQuery(string commandText, CommandType commandType, SqlParameter[] sqlParameters)
{
int x = 0;
try
{
Conn = Connect();
SqlCommand cmd = new SqlCommand(commandText, Conn);
cmd.CommandType = commandType;
cmd.CommandTimeout = 0;
if (sqlParameters != null && sqlParameters.Length != 0)
{
cmd.Parameters.AddRange(sqlParameters);
}
x = cmd.ExecuteNonQuery();
Conn.Close();
Conn.Dispose();
cmd.Dispose();
}
catch (Exception ex)
{ throw ex; }
return x;
}
}