Class design question (Disposable and singleton behavior)

Posted by user137348 on Stack Overflow See other posts from Stack Overflow or by user137348
Published on 2010-05-09T12:39:40Z Indexed on 2010/05/09 12:48 UTC
Read the original article Hit count: 142

The Repository class has singleton behavior and the _db implements the disposable pattern. As excepted the _db object gets disposed after the first call and because of the singleton behavior any other call of _db will crash.

 [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
 public class Repository : IRepository
 {
    private readonly DataBase _db;

    public Repository(DataBase db)
    {
        _db = db;
    }

    public int GetCount()
    {
        using(_db)
        {
            return _db.Menus.Count();
        }
    }

    public Item GetItem(int id)
    {
        using(_db)
        {
            return _db.Menus.FirstOrDefault(x=>x.Id == id);
        }
    } 

  }

My question is, is there any way to design this class to work properly without removing the singleton behavior? The Repositoryclass will be serving big amount of requests.

© Stack Overflow or respective owner

Related posts about object-oriented-design

Related posts about c#