HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

My implementation of the repository pattern

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
patternimplementationtherepository

Problem

I'm using this pattern since a few months and I was wondering if I can make it any better.

The one thing I am not satisfied about is the dispose method. In every repository I have to add a dispose method that calls the base dispose method. Is there a way to get rid of the dispose method in every repository and just let it (automatically) call the base dispose method?

I'm using Linq2Sql.

Interface:

public interface IRepository where TEntityType : class
{
    TEntityType GetById(int id);

    IQueryable GetAll();

    void Delete(TEntityType item);

    void Add(TEntityType item);
}


Base:

public class BaseRepository where TEntityType : class
{
    private readonly DataClassesDataContext _dataContext;

    protected BaseRepository()
    {
        _dataContext = new DataClassesDataContext();
    }

    protected Table GetTable()
    {
        return _dataContext.GetTable();
    }

    protected void SaveChanges()
    {
        _dataContext.SubmitChanges();
    }

    protected virtual void Dispose()
    {
        if (_dataContext != null)
        {
            _dataContext.Dispose();
        }
    }
}


Repository class:

public class FirstClassRepository : BaseRepository, IRepository, IDisposable
{
    public FirstTable GetById(int id)
    {
        return GetTable().FirstOrDefault(x => x.Test == id);
    }

    public IQueryable GetAll()
    {
        return GetTable();
    }

    public void Delete(FirstTable item)
    {
        GetTable().DeleteOnSubmit(item);
    }

    public void Add(FirstTable item)
    {
        GetTable().InsertOnSubmit(item);
    }

    public new void Dispose()
    {
        base.Dispose();
    }
}

Solution

The one thing I am not satisfied about is the dispose method. In every repository I have to add a dispose method that calls the base dispose method.

IDisposable

The IDisposable interface should be impelemented in the base class. By using the default pattern of disposing, you will get this result

public class BaseRepository : IDisposable where TEntityType : class
{
    private readonly DataClassesDataContext _dataContext;

    protected BaseRepository()
    {
        _dataContext = new DataClassesDataContext();
    }

    protected Table GetTable()
    {
        return _dataContext.GetTable();
    }

    protected void SaveChanges()
    {
        _dataContext.SubmitChanges();
    }

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

    ~BaseRepository()
    {
        Dispose(false);
    }

    protected void Dispose(Boolean disposing)
    {
        // free unmanaged ressources here
        if (disposing)
        {
            // This method is called from Dispose() so it is safe to
            // free managed ressources here
            if (_dataContext != null)
            {
                _dataContext.Dispose();
            }
        }
    }
}


But wait, what if you also need to dispose ressources of the FirstClassRepository object which descends from that object. Then the FirstClassRepositoryobject also needs to implement the IDisposable interface with the pattern below.

public class FirstClassRepository : BaseRepository, IRepository, IDisposable
{
    public FirstTable GetById(int id)
    {
        return GetTable().FirstOrDefault(x => x.Test == id);
    }

    public IQueryable GetAll()
    {
        return GetTable();
    }

    public void Delete(FirstTable item)
    {
        GetTable().DeleteOnSubmit(item);
    }

    public void Add(FirstTable item)
    {
        GetTable().InsertOnSubmit(item);
    }

    public void Dispose()
    {
        try
        {
            Dispose(true); //true: safe to free managed resources
            GC.SuppressFinalize(this);
        }
        finally
        {
            base.Dispose();
        }
    }

    ~FirstClassRepository()
    {
        Dispose(false);
    }

    protected void Dispose(Boolean disposing)
    {
        // free unmanaged ressources here
        if (disposing)
        {
            // This method is called from Dispose() so it is safe to
            // free managed ressources here
        }
    }
}


If the FirstClassRepository obect doesn't need to dispose ressources you can omit the implementation of IDisposable.

EDIT based on comment:

The ~FirstClassRepository() method is what the compiler takes as beeing the Finalize() method of the object. If you forget to call the Dispose() method of that object, this method will be called at the time your object is destructed.

So as the method is implemented by calling the overloaded Dispose(Boolean) method with the parameter == false, only unmanaged ressources will be freed (where the comment is written) as the GC takes care of the managed objects.

Otherwise your implementation looks fine for me.

Some SO links

proper-use-of-the-idisposable-interface

when-should-i-use-gc-suppressfinalize

Code Snippets

public class BaseRepository<TEntityType> : IDisposable where TEntityType : class
{
    private readonly DataClassesDataContext _dataContext;

    protected BaseRepository()
    {
        _dataContext = new DataClassesDataContext();
    }

    protected Table<TEntityType> GetTable()
    {
        return _dataContext.GetTable<TEntityType>();
    }

    protected void SaveChanges()
    {
        _dataContext.SubmitChanges();
    }

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

    ~BaseRepository()
    {
        Dispose(false);
    }

    protected void Dispose(Boolean disposing)
    {
        // free unmanaged ressources here
        if (disposing)
        {
            // This method is called from Dispose() so it is safe to
            // free managed ressources here
            if (_dataContext != null)
            {
                _dataContext.Dispose();
            }
        }
    }
}
public class FirstClassRepository : BaseRepository<FirstTable>, IRepository<FirstTable>, IDisposable
{
    public FirstTable GetById(int id)
    {
        return GetTable().FirstOrDefault(x => x.Test == id);
    }

    public IQueryable<FirstTable> GetAll()
    {
        return GetTable();
    }

    public void Delete(FirstTable item)
    {
        GetTable().DeleteOnSubmit(item);
    }

    public void Add(FirstTable item)
    {
        GetTable().InsertOnSubmit(item);
    }

    public void Dispose()
    {
        try
        {
            Dispose(true); //true: safe to free managed resources
            GC.SuppressFinalize(this);
        }
        finally
        {
            base.Dispose();
        }
    }

    ~FirstClassRepository()
    {
        Dispose(false);
    }

    protected void Dispose(Boolean disposing)
    {
        // free unmanaged ressources here
        if (disposing)
        {
            // This method is called from Dispose() so it is safe to
            // free managed ressources here
        }
    }
}

Context

StackExchange Code Review Q#59042, answer score: 3

Revisions (0)

No revisions yet.