mravko
mravko

Reputation: 49

Reusing DbContext in Generic Repository

I am implementing the Repository Pattern with Entity Framework 5.0 - at least i think i am :). This is what i am using

public abstract class GenericRepository<C, T> :
 IGenericRepository<T>
    where T : class
    where C : DbContext, new()
{
    private bool disposed = false;
    private C _entities = new C();
    protected C Context
    {
        get { return _entities; }
        set { _entities = value; }
    }

    public virtual IQueryable<T> GetAll()
    {
        IQueryable<T> query = _entities.Set<T>();
        return query;
    }

    public IQueryable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
    {
        IQueryable<T> query = _entities.Set<T>().Where(predicate);
        return query;
    }

    public virtual void Add(T entity)
    {
        _entities.Set<T>().Add(entity);
    }

    public virtual void Delete(T entity)
    {
        _entities.Set<T>().Remove(entity);
    }

    public virtual void Edit(T entity)
    {
        _entities.Entry(entity).State = System.Data.EntityState.Modified;
    }

    public virtual bool Save()
    {
        return (_entities.SaveChanges() > 0);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
            if (disposing)
                _entities.Dispose();

        this.disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}

Afterwards this class is inherited by specific repository class - let's say PlaceRepository

public class PlaceRepository : GenericRepository<DbEntities, Place>, IPlaceRepository
{ }

In another layer (business layer) i am creating instances of the PlaceRepository class - in class called PlaceController. This class has specific methods for the PlaceEntity (CRUD). In this PlaceController i have one method that is used for inserting Place entity in the database, but at the same time i am inserting something in another table (let's say Country table). For CRUD operations over the Country table i have another Repository called CountryRepository.

To sum up, my method in the Place Controller creates instances of two different repositories, to use their methods, thereby creating two different contexts of DbContext. See code below

public class PlaceController 
{ 
   public bool InsertPlace(Place toInsert)
   {
      PlaceRepository _placeRepo = new PlaceRepository();
      _placeRepo.Add(toInsert);

      Country _country = new Country();

      _country.Name = "default";

      CountryRepository _countryRepo = new CountryRepository();

      _countryRepo.Add(_country);

     //now i must call save on bothRepositories
     _countryRepo.Save(); _placeRepo.Save();

}
}

I need an opinion for this scenario. Is it good to create 2 instances of the context class to make two insertions? If it isn't should i consider using/implementing another pattern?

Upvotes: 1

Views: 2068

Answers (1)

Maris
Maris

Reputation: 4776

You should use IoC(DI) containers for that. That will help you to use 1 instance of the context whole a long the project. You can look also to the way of serviceLocator pattern but I would recoment to use IoC(DI) container. I like Castle.Windsor(the realization of DI pattern) also you can look to Ninject.

Upvotes: 1

Related Questions