0

In the past I have used primarily singletons and static classes for my DAL, but have been tasked with creating a new DAL in a high load MVC5 environment where performance is the only thing that matters.

Singletons like this seems like they'd be slow on performance and potential high in memory usage. Statics would have a low foot print but also could result in high memory and possible concurrency issues.

public class DAL
{
    private DAL()
    {

    }

    private static readonly DAL _singleton = new DAL();

    public static DAL Instance()
    {
        return _singleton;
    }
}
  • You're question does not describe the problem in enough detail. Is this a high traffic site querying a database where the content of the database is not changing frequently, or you can be notified of appropriate data changes? MVC Controller caching can assist with this type of scenario. See: http://stackoverflow.com/questions/343899/how-to-cache-data-in-a-mvc-application – Aaron Hudon Oct 27 '16 at 18:01
  • Basically very high traffic where the content of the database is huge, but only updated at night via a batch job. And all of those transactions are inserts; very few actual update queries. – PleaseMakeItStop Oct 27 '16 at 18:19

1 Answers1

1

The virtually universal approach is to have an instance with a request lifetime. Things like statics and singletons are almost always a bad idea in the context of a web application because of inherent issues like concurrency.

Performance really has no place in this discussion because all you're talking about is instantiating a class per request or just once. At most, this is a micro-optimization, and not a very meaningful one at that.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444