Question

I know that the title looks a bit bad :) But I dont know how to explain my problem..

This is typically a basic problem for me but I dont know answer..

I am writing a server application which is using eneter library for client-server communinication and also it has a DAL gets data from database. as it a server application, it always need to communicate with database, so I dont know which way is more effective. (approx, max 50 clients will be connected to server)

I am using entity framework and created a model from my mysql db.

the first code is here

    private MyEntities ent;

    public DbHelper()
    {
     ent = new MyEntities();
    }

   void Foo()
  { 
       ent.Mytable.where......
       ....

  }

and second type code is

    void Foo()
    {
        using (MyEntities ent = new MyEntities())
        {
            ent.Mytable.where...
        }

    }

Can I use using statement or create a global instance variable for dal class and use it for each functions.. ?

Was it helpful?

Solution

Better yet, implement IDisposable on your DAL class:

public sealed class MyDal implements IDisposable
{
    private MyEntities ent = new MyEntities();

    void Foo()
    { 
       ent.Mytable.where......
       ....

    }

    public void Dispose()
    {
        ent.Dispose();
    }
}

Then...

using(var dal = new MyDal())
{
    dal.Foo();
    //....
}

Take a read here about why my IDisposable is sealed.

OTHER TIPS

From performance viewpoint it doesn't matter. Compared to the actual database interaction creating a context instance is a very fast operation.

However, you should dispose the created context in any case because it holds a database connection as a native resource.

If your want to work with a context member in your DbHelper class this class should implement IDisposable so that you can dispose the context when the DbHelper instance itself gets disposed:

public class DbHelper : IDisposable
{
    private MyEntities ent;

    public DbHelper()
    {
        ent = new MyEntities();
    }

    public void Foo()
    {
        //...
    }

    public void Bar()
    {
        //...
    }

    public void Dispose() // implementation of IDisposable
    {
        ent.Dispose();
    }
}

You could use this class in a using block then:

using (var helper = new DbHelper())
{
     helper.Foo();
     helper.Bar();
} // helper and helper.ent gets disposed now

This depends on what other methods exist and what they do. If you want to make changes and persist those changes using the ORM, you will need the data-context that created the objects. Also, if you want the identity manager to give you the same object instance if you query the same thing twice, you will need to use the same data-context - so you'll need to keep it handy. Finally, if the type uses lazy loading and you expect it to work - then that won't work if you have disposed the data-context.

If, however, you just want read-only access to the data without change tracking, lazy loading or identity management : dispose eagerly. And maybe consider things like micro-ORMs which simply don't have those features (deliberately, to be minimal and fast).

The 2 approaches are very different in terms of the scope of the change-tracking. If both work equally well then make sure to use WithNoTracking.

You can create a member variable for your entities like in your first code. But because you cannot write a using(){} statement around it the containing class should be IDisposable. And then the consuming class should use it inside a using(){}.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top