1

I have the following generic repository:

public class EFRepository<TEntity, TContext> : IRepository<TEntity, TContext>,    IDisposable
    where TEntity : class
    where TContext : ObjectContext
{

    protected TContext context;

    public EFRepository(TContext context)
    {
        this.context = context;
    }

    //CRUD methods...

    public void Dispose()
    {
        if (null != context)
        {
            context.Dispose();
        }
    }
}

This is a class from the Business layer

 public class UserBLL : BaseBLL<User>
 {

    EFRepository<User, MyEntities> userRepo = null;

    public UserBLL() : base ()
    {
       //Context is created in the consructor of the base class and passed to repository
        userRepo = new EFRepository<User, MyEntities>(Context);
    }
 }

Here is the base business class:

 public class BaseBLL <TEntity> 
        where TEntity : class
    {
        protected MyEntities Context { get; set; }

        public BaseBLL()
        {
            this.Context = DataAccessHelper.Context;
            this.Context.MetadataWorkspace.LoadFromAssembly(typeof(TEntity).Assembly);
        }
    }

In this design, since I'm creating an instance of the repository in the business class constructor rather than inside a using clause, the dispose method of the repository is not getting called by default. My main question is how to make sure the context/repository is disposed.

I know I can create the repository in a using clause inside each method rather than in the constructor, but I wonder if there's a more elegant way.

Feel free to comment about the design in general as well.

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
t_and_w
  • 11
  • 1
  • 3
  • Look at [this answer](http://stackoverflow.com/questions/6987908/what-is-the-best-way-to-instantiate-and-dispose-dbcontext-in-mvc/6990244#6990244) – Eranga Aug 10 '11 at 06:01

2 Answers2

2

Wrap Dbcontext with UnitOfWork and inside of UnitOfWork implement dispose method.

Reference : http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/

marvelTracker
  • 4,691
  • 3
  • 37
  • 49
1

It is completely wrong. You are creating context outside of the repository so repository cannot be responsible for the disposal. The layer where the repository is constructed for the disposal = BaseBLL must be disposable and upper layer must dispose it correctly when it doesn't need it any more.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • OK, how would you redesign it than? Would you create the context inside the repository rather than passing it? How would you make sure it's properly disposed? Would you create an instance of the repository in the business layer inside a using clause? – t_and_w Aug 11 '11 at 03:22
  • I wouldn't say *completely* wrong, just unseemly. The repository could know enough to create the context, but it would couple it with that context's creation, which is coupled to the connection string etc. I would instead inject the context into the repository's constructor using an IoC container such as Unity. If you're doing all this through the container, and have setup lifetime properly, then you don't have to worry about dispose. – Kit Aug 17 '11 at 21:05