Should I be worried about lazy loading in a web application?
I found out that for single user lazy loading, this is the pattern:
private MyObject()
{
}
public static MyObject Instance
{
get { return SingletonCreator.CreatorInstance; }
}
private static class SingletonCreator
{
private static readonly MyObject _instance = new MyObject();
public static MyObject CreatorInstance
{
get { return _instance; }
}
}
But a simpler template would be:
private static readonly MyObject _instance = new MyObject();
private MyObject()
{
}
public static MyObject Instance
{
get { return _instance; }
}
It won't be lazy loading. But is this really what I should be worried about in a web application?
a source to share
Unless you have static methods that don't need a singleton, just including a static constructor is enough to make the class lazy. Without a static constructor, it's still mostly lazy.
See more on singleton options for details and options.
Typically, the code you provided at the bottom of the question is lazy enough unless you really want to initialize the singleton if it won't be used. (Basically with setup beforefieldinit
, the JIT will usually check that every type used in a method is initialized, without beforefieldinit
it having to wait for the first actual use of the class at runtime. See my beforefieldinit
page , but the important thing is that it still won't initialize everything singletons as soon as the assembly loads, or something like that.)
a source to share
Lazy loading usually means that you are lazy loading something from the database. What you do is usually called " lazy initialization " (technically "lazy initialization" is a method of implementing the lazy loading pattern).
As for your original questions: first, you don't need a singleton. If you still need to, here 's how it should be done correctly. Third, you don't need a singleton.
a source to share
All well and good, but the point of lazy loading is to avoid loading (usually unmanaged) resources (hitting the database, filesystem, etc.) until you need it, thus avoiding loading them before proactive use I really need to. Using the singleton pattern itself does not necessarily imply lazy loading, as you can properly create a managed instance at any time.
Aspects of lazy loading come into play if any resources you are loading are available only by calling the appropriate method; if when this singleton instance is created it executes a whole chain of database queries and pushes the result and then not lazy loading as far as I know.
In ORM scenarios, lazy loading usually refers directly to deferring the loading of another object in relation to the first access, thus avoiding a second, potentially unnecessary request.
Then, if you know that you are going to use those relationships in the process of using the object (i.e. if you got User
to enumerate them Posts
or whatever) then you probably want to direct the ORM to load related objects at the same time, which is like usually prompts to execute one query with a connection, rather than loop and execute multiple queries later; in this case you don't need lazy loading.
a source to share
Be sure not to confuse page load, session time, and application. However, are you sure you want to use a static singleton instance? Once created, it will run until the application (web server) exits or runs iisreset.
Consider caching in HttpContext.Current.Items
instead if you want one loaded instance per page to load, or perhaps in HttpContext.Current.Session
if you want one user.
a source to share