开发者

Class for DB access via an Entity Model, how to cleanup the connection?

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 object开发者_如何转开发s like you use in LINQ to SQL, so should I make DataAccess implement iDisposeable?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜