10

I have an entity class which is auto generated from my database model. This class inherits the ObjectContext which inturn inherits IDisposable.

I have created a repository that has various methods which use a single instance of the entity object to interact with the database.

Auto generated class

public partial class DevEntities : ObjectContext
{
    public const string ConnectionString = "name=DevEntities";
    public const string ContainerName = "DevEntities";

Repository Class

DevEntities db = new DevEntities();

        public Customer GetCustomerByID(int id)
    {
        var customers = db.Customers.FirstOrDefault(c => c.CustomerId == id);

        return customers;
    }

    public Customer GetCustomerByPasswordUsername(string email, string password)
    {
        var customers = db.Customers.FirstOrDefault(c => c.Email == email && c.Password == password);

        return customers;
    }

From this you can see that I make multiple references to the db instance. My question is, am I better to instantiate a new DevEntity within each method, thus being able to implement the using statement, and so ensure correct disposal, or is my current implementation ok?

Millie Smith
  • 4,536
  • 2
  • 24
  • 60
hoakey
  • 998
  • 3
  • 18
  • 34

1 Answers1

9

I would implement IDisposable on the Repository class as well, so it can dispose the ObjectContext. If you return a different ObjectContext each time, you can run into problems when doing queries between those objects, as those are attached to different ObjectContexts, which will result in an exception.

Definition:

public class Repository : IDisposable
{
    DevEntities db = new DevEntities();

    public Customer GetCustomerByID(int id)
    {
        var customers = db.Customers.FirstOrDefault(c => c.CustomerId == id);

        return customers;
    }

    public Customer GetCustomerByPasswordUsername(string email, string password)
    {
        var customers = db.Customers.FirstOrDefault(c => c.Email == email && c.Password == password);

        return customers;
    }

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

Usage:

using(Repository r = new Repository())
{
  //do stuff with your repository
}

Doing this, your repository takes care of disposing the ObjectContext after you used it.

Femaref
  • 60,705
  • 7
  • 138
  • 176
  • If I did that I would need to override the dispose method of IDisposable I think? So what might that look like? Would it dispose the object and each method would make reference to that method, and thus siposing there instance? – hoakey Jan 02 '11 at 15:55
  • No, you don't have to override it, you implement the interface on your repository; now you can use the repository in the using statement, so the class gets automatically disposed at the end of the usage. – Femaref Jan 02 '11 at 16:05
  • Sorry for my thickness, I still don't quite follow how that would work. Would you mind elaborating a little more please? Many thanks. – hoakey Jan 02 '11 at 16:19
  • 1
    How do you wrap that in a using statement when you've lazy loading enabled? – Praveen Feb 17 '11 at 23:37
  • the same way. of course you can't leave the using and expect to be able to access the rest of the data, but that's the same if you were using the ObjectContext directly. – Femaref Feb 17 '11 at 23:51
  • When using this "using statement" how can I mock the Repository class when writing unit tests? I would want my unit test to not actually use the DevEntities() object. – Mitch May 20 '16 at 13:41