简体   繁体   English

asp mvc-通用存储库和工作单元-覆盖存储库方法

[英]asp mvc - generic repository and unit of work - override repository method

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. 我是ASP MVC 2中存储库模式的新手,我试图弄清楚如何在我的通用存储库类中扩展操作,并且在弄清楚如何做或在线查找示例时遇到了麻烦。 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. 我有一种特定类型的实体,需要将其他工作添加到insert方法中。 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? 所以我的问题是,我可以将覆盖的Insert方法放在哪里? 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. 感谢您的任何建议。

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. 我不需要“覆盖”任何东西,只需要在工作单元类(duh)中添加其他工作,然后调用插入。 Thanks for the thought provoking responses. 感谢您的想法令人发指。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM