3

I'm building a App that use Context of EF in Singleton Pattern like NHibernate work with Session:

public class DbContextFactory
{
    private static volatile DbContextFactory _dbContextFactory;
    private static readonly object SyncRoot = new Object();
    public DbContext Context;

    public static DbContextFactory Instance
    {
        get
        {
            if (_dbContextFactory == null)
            {
                lock (SyncRoot)
                {
                    if (_dbContextFactory == null)
                        _dbContextFactory = new DbContextFactory();
                }
            }
            return _dbContextFactory;
        }
    }

    public DbContext GetOrCreateContext()
    {
        if (this.Context == null)
            this.Context = new DbContext(ConfigurationManager.AppSettings["DefaultConnectionString"]);

        return Context;
    }
}

I'm using Ninject to Inject the Context:

public class DbContextModule : NinjectModule
{
    public override void Load()
    {
        Bind<IDbContext>().ToConstant(DbContextFactory.Instance.GetOrCreateContext());
    }
}

I'm reading about that approach and some people are saying that is bad practice and I'll have problems.

Someone that know about this with EF can explain me in more details?

1
  • Did you only read it's bad practice? Without explanation? Commented May 6, 2014 at 8:07

3 Answers 3

3

NHibernate doesn't use Session as singleton ... Such scenario has only meaning in very rare cases where your application is very short batch representing single transaction / unit of work.

Here are described reasons why you should not use shared / long living context. In case of multithreaded or server application serving multiple clients you must not use shared context.

Sign up to request clarification or add additional context in comments.

7 Comments

What's Singleton in NHibernate? The SessionFactory?
Look at my approach, exist any problem? I can say to Ninject use in a RequestScope.
Yes the factory can be used as a singleton and create a new Session each time it is required. Request scope is normally used for context in web application scenarios.
I'm confusing, look at my approach, I'm resolving the problem put InRequestScope in Ninject Configuration?
I don't see any request scope in your code and still using your singleton factory in request scope will not solve it because it will return the same context instance every time.
|
0

A good pratice is to use a datacontext per unit of work To have more information about unit of work with EF you should read http://blogs.msdn.com/b/adonet/archive/2009/06/16/using-repository-and-unit-of-work-patterns-with-entity-framework-4-0.aspx

When sharing the same datacontext you are probably saving a few 10s of milliseconds. The word micro optimisation springs to mind - in which case you probably shouldn't be using Entity Framework.

Having a DataContext that is never closed allows you to lazy load whenever you want. You may have left your Service and now be in your Controller or worse still, your View. Accessing the database from a View is asking for performance problems as I'm sure you didn't allow that intentionally. It is most likely because you forgot to eager load all the data you need to populate your view.

1 Comment

Article no longer available.
0
public class Dock
{
    // Statik field
    private static Dock _dock;
    // lock object
    private static object lockObject = new object();

    // We prevent the constructive method from being modeled with new by 
    //making it secret.
    private Dock()
    {

    }
    // Class in Instance
    public static Dock Instance()
    {   
        if (_dock == null)
        {
            lock (lockObject)
            {
                if (_dock == null)
                    _dock = new Dock();
            }
        }
        return _dock;
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.