4

我创建了一个通用存储库类,我的所有其他存储库类都从该类继承。这很棒,因为这意味着几乎所有的管道都为所有存储库完成了一次。我在这里对我正在谈论的内容进行了完整的解释,但这里是我的 GenericRepository 的代码(为简洁起见,删除了一些代码):

public abstract class GenericRepository<T> : IGenericRepository<T> where T : class, new()
{
    private IMyDbContext _myDbContext;

    public GenericRepository(IMyDbContext myDbContext)
    {
        _myDbContext = myDbContext;
    }

    protected IMyDbContext Context
    {
        get
        {
            return _myDbContext;
        }
    }

    public IQueryable<T> AsQueryable()
    {
        IQueryable<T> query = Context.Set<T>();
        return query;
    }

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

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

如您所见,我有一个 Create 方法和一个 Update 方法。拥有“CreateOrUpdate”方法会非常方便,因此我不必每次必须将某些内容保存到数据库时手动检查现有对象。

我在 Entity Framework 中的每个对象都有一个“Id”,但这里的挑战是 GenericRepository 与“T”一起使用。

现在,通过相当长的介绍,我的具体问题。

如何为我创建一个通用CreateOrUpdate方法GenericRepository

更新

在 Marcins 响应之后,我在我的 GenericRepository 中实现了以下通用方法。我需要一些时间才能测试它是否按预期工作,但它看起来很有希望。

public virtual bool Exists(Guid id)
{
    return Context.Set<T>().Any(t => t.Id == id);
}

public virtual void CreateOrUpdate(T entity)
{
    if (Exists(entity.Id))
    {
        var oldEntity = GetSingle(entity.Id);
        Context.Entry(oldEntity).CurrentValues.SetValues(entity);
        Update(oldEntity);
    }
    else
    {
        Create(entity);
    }
}

上面的代码在更新时与数据库的往返次数不少于 3 次。我确信它可以被优化,但这并不是这个问题的真正练习。

这个问题更好地处理了这个主题: ObjectStateManager 中已经存在具有相同键的对象。ObjectStateManager 无法跟踪具有相同键的多个对象

4

1 回答 1

4

创建一个带有Id属性的接口,在每个实体上实现它,并向您的类添加另一个通用约束:

public interface IEntity
{
    int Id { get; set;}
}

public abstract class GenericRepository<T> : IGenericRepository<T> where T : class, IEntity, new()

这样,您就可以Id在通用存储库类中使用属​​性。

当然 -Id不必是int,也可以是Guid

于 2013-08-17T10:32:51.467 回答