views:

12

answers:

1

I have a class I'm using to handle all my DB interaction. Basically when an instance of this class is created it creates an instance of the model, which will persist until the DataAccess object is out of scope.

public class DataAccess
{
    ModelContainer model;

    public DataAccess()
    {
        model = new ModelContainer();
    }

    public void Close()
    {
        if (model != null)
        {
            model.Connection.Close();
            model.Dispose();
        }
    }
}

What cleanup do I need to preform on the model? I've cought my self forgetting to call my Close() method when writing my unit tests. I like the using(DataAccess data = new DataAccess()) way of dealing with DB objects like you use in LINQ to SQL, so should I make DataAccess implement iDisposeable?

+1  A: 

If you have items in the class that implements IDisposable it is a good idea to make your class implement that aswell so that they get disposad properly. If you do not call them upon disposing your class you might have some unexcepted memory leaks. And it's easy to write "using" as you mentioned which you only can do when you implement IDisposable.

This is an excellent example on how to implenet IDisosable: http://msdn.microsoft.com/en-us/library/system.idisposable.aspx

Oskar Kjellin