I have a web app, this app as you already know will have a lot of requests, and each request will run on a different thread, meaning that if I use singleton to access my DAL library it will not be a problem.
However I'm unsure about wether this is the best approach, because I read that one request my use different threads sometimes, and I have also read that locking threads may cause a performance lost sometimes when using single instances.
Just so you understand me better this is what I plan to do:
DAL <- SingleInstance <- BAL <- SingleInstance <- Controllers <- Views
Is this a good approach?
This is the singleton I plan to use:
public static Products Instance
{
get
{
return Nested.instance;
}
}
class Nested
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Nested()
{
}
internal static readonly Products instance = new Products();
Note: My Dal will access the database using ADO.NET(I have my reasons to use ado), and the BAL will only use this method to do select or CRUD operations.
My Ado.NET code:
public static readonly string ConnectionString = @"Server=localhost;Database=test;Uid=root;Pwd=test;";
public bool IsProduct(string name)
{
var conn = new SqlConnection(ConnectionString);
bool result;
try
{
conn.Open();
var command = new SqlCommand();
SqlParameter[] parameters = new SqlParameter[1];
parameters[0] = new SqlParameter("@Product", name);
command.Connection = conn;
command.CommandText = "SPC_GET_PRODUCT";
command.CommandType = System.Data.CommandType.StoredProcedure;
result = Convert.ToBoolean(command.ExecuteScalar());
}
finally
{
conn.Close();
conn.Dispose();
}
return result;
}
Thanks.