1

I have been trying to implement a repository pattern in my project. I am not sure if i am using dispose correctly. I took the pattern from the MVA course on entity framework.

My repository

public static bool IsAwesome { get { return true; } }

   public class Repository<T> : IDisposable where T : class 
   {
    private ApplicationDbContext db = null;
    protected DbSet<T> DbSet { get; set; }

    public Repository()
    {
        db = new ApplicationDbContext();
        DbSet = db.Set<T>();
    }

    public List<T> GetAll()
    {
        return DbSet.ToList();
    }

    public T Get(int id)
    {
        return DbSet.Find(id);
    }

    public T GetWithString(string id)
    {
        return DbSet.Find(id);
    }

    public void Add(T entity)
    {
        DbSet.Add(entity);
    }

    public void Update(T entity)
    {
        DbSet.Attach(entity);
        db.Entry(entity);
    }

    public void SaveChanges()
    {
        db.SaveChanges();
    }


    private bool disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                db.Dispose();
            }
        }
        this.disposed = true;
    }

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

Example of imageRepository which inherits from repository

 public class ImageRepository : Repository<Image>
{

    public Image GetLatest(int vehicleId)
    {
        return DbSet.FirstOrDefault(p => p.VehicleId == vehicleId);
    }

    public List<Image> GetImagesByVehicleId(int vehicleId)
    {
        return DbSet.Where(p => p.VehicleId == vehicleId).ToList();
    }

}

Using my repository on top of the controller and disposing in the bottom of my controller

    ImageRepository imageRepository = new ImageRepository();
    UserRepository userRepository = new UserRepository();

    protected override void Dispose(bool disposing)
    {
        imageRepository.Dispose();
        userRepository.Dispose();
        base.Dispose(disposing);
    }

Will my code handle all unmanaged connections and close them correctly?

Thank you in advance. Im still a bit new to MVC and EF. I am sorry if my question is a bit newbish. My first post in here. So i hope i did not break any rules:)

2 Answers2

0

Add your Dispose code in UnitOfWork,Remove From GenericRepository

 private bool disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                Context.Dispose();
            }
        }
        this.disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
Hithesh
  • 441
  • 4
  • 9
  • Not sure what you mean with unitOfWork? - In the repository which inherits from generic repository? - Or is something else?? ) – Mathias Hove Feb 05 '16 at 16:08
0

Will my code handle all unmanaged connections and close them correctly?

Apparently yes.

However, you are not quite following the pattern. You don't have to SuppressFinalize as you don't have a finalizer in your class.Have a read about proper implementation of IDisposable Pattern.

Community
  • 1
  • 1
CharithJ
  • 46,289
  • 20
  • 116
  • 131