asp mvc - generic repository and unit of work - override repository method

2.3k Views Asked by At

I am new to repository patterns in ASP MVC 2 and I am trying to figure out how to extend an operation in my generic repository class, and am having trouble figuring out how do it or find examples of it online. I want a generic repository and unit of work that will work for most cases, but want something easy to alter slightly for special cases.

I have the following generic repository interface and repository:

public interface IRepository<TEntity> where TEntity : class
{
    List<TEntity> Get();
    TEntity GetByKey(object key);
    void Insert(TEntity entity);
    void Delete(TEntity entity);
    void Save();
}

public class RepositoryBase<TEntity> : IRepository<TEntity> where TEntity : class
{
    private readonly string entity_set_name;
    public readonly string entity_key_name;

    private ObjectContext object_context;
    private ObjectSet<TEntity> object_set;

    public RepositoryBase(ObjectContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }

        object_context = context;
        object_set = object_context.CreateObjectSet<TEntity>();

        entity_set_name = object_set.EntitySet.Name;
        entity_key_name = object_set.EntitySet.ElementType.KeyMembers.Single().Name;
    }

    public virtual List<TEntity> Get()
    {
        return object_set.ToList();
    }

    public virtual TEntity GetByKey(object key)
    {
        var entityKey = new EntityKey(entity_set_name, entity_key_name, key);
        return (TEntity)object_context.GetObjectByKey(entityKey);
    }

    public virtual void Insert(TEntity entity)
    {
        object_set.AddObject(entity);
    }

    public virtual void Delete(TEntity entity)
    {
        object_set.DeleteObject(entity);
    }

    public virtual void Save()
    {
        object_context.SaveChanges();
    }
}

...and the following Unit of Work class:

public class UnitOfWork : IDisposable
{
    private MyEntities context = null;
    public Dictionary<Type, object> repositories = new Dictionary<Type, object>();

    public UnitOfWork()
    {
        context = new EmployeeAccessFormEntities();
    }

    public IRepository<TEntity> Repository<TEntity>() where TEntity : class
    {
        if (repositories.Keys.Contains(typeof(TEntity)) == true)
        {
            return repositories[typeof(TEntity)] as IRepository<TEntity>;
        }
        IRepository<TEntity> r = new RepositoryBase<TEntity>(context);
        repositories.Add(typeof(TEntity), r);
        return r;
    }

    public virtual void Save()
    {
        context.SaveChanges();
    }

    #region IDisposable Members

    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);
    }

    #endregion
}

I have a specific type of entity that needs additional work added to the insert method. I want to do someting like:

public partial class MyEntity : RepositoryBase<MyEntity>
{
    public override void Insert(MyEntity entity)
    {
        // do additional work

        base.Insert(entity);
    }
}

...but I receive an error that "Partial declarations must not specify different base classes."

So my question is, where would I be able to put that overridden Insert method? Do I have to make a seperate repository that inherits the generic repository? If I did that, how would I need to change my unit of work class?

Thank you for any advice.

1

There are 1 best solutions below

0
On BEST ANSWER

I think I understand this better after some time, so figured I would close it. I didn't need to "override" anything, I just needed to add the additional work in my unit of work class (duh), and then call the insert. Thanks for the thought provoking responses.