lundi 30 mars 2015

Is using the singleton pattern a good idea when accessing libraries using MVC?

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. Thanks.


Aucun commentaire:

Enregistrer un commentaire