实体框架、抽象类、通用存储库和通用管理器

本文关键字:管理器 存储 实体 框架 抽象类 | 更新日期: 2023-09-27 18:30:49

在项目中,我们使用通用存储库和通用管理器,因此我们不需要重写每个存储库/管理器中的每个更新/删除等方法。

这是它们的外观:

public interface IBaseRep<T> : IDisposable where T : class, PrivateObject
{
    DbSet<T> DatabaseSet { get; set; }
    DbContext Dal { get; set; }
    T Find(int? id);
    T Find(Expression<Func<T, bool>> predicate);
    ICollection<T> Select(Expression<Func<T, bool>> predicate = null,
        Expression<Func<T, string>> orderBy = null,
        string includeProperties = "");
    T Create(T obj);
    T Update(T obj);
    bool Delete(T obj);
    bool Delete(int id);
    bool Delete(Expression<Func<T, bool>> predicate);
    IQueryable<T> SelectAsQuery(
        Expression<Func<T, bool>> predicate = null, 
        Expression<Func<T, string>> orderBy = null, 
        string includeProperties = "");
}
public class BaseRep<T> : IBaseRep<T> where T : class, PrivateObject
{
    public DbSet<T> DatabaseSet { get; set; }
    public DbContext Dal { get; set; }
    public EORTCBaseRep(DbContext dal)
    {
        this.Dal = dal;
        this.DatabaseSet = Dal.Set<T>();
    }
    public virtual T Find(int? id)
    {
        return this.DatabaseSet.Find(id);
    }
    public virtual T Find(Expression<Func<T, bool>> predicate)
    {
        return Select(predicate).FirstOrDefault();
    }
    public virtual ICollection<T> Select(
        Expression<Func<T, bool>> predicate = null,
        Expression<Func<T, string>> orderBy = null,
        string includeProperties = "")
    {
        return SelectAsQuery(predicate, orderBy, includeProperties).ToList();
    }
    public virtual IQueryable<T> SelectAsQuery(
       Expression<Func<T, bool>> predicate = null,
       Expression<Func<T, string>> orderBy = null,
       string includeProperties = "")
    {
        IQueryable<T> query = this.DatabaseSet;
        if (predicate != null)
            query = query.Where(predicate);
        foreach (var includeProperty in includeProperties.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
            query = query.Include(includeProperty);
        if (orderBy != null)
            query = query.OrderBy(orderBy);
        return query;
    }
    public virtual T Create(T obj)
    {
        this.Dal.Entry<T>(obj).State = EntityState.Added;
        this.Dal.SaveChanges();
        return obj;
    }
    public virtual T Update(T obj)
    {
        this.Dal.Entry<T>(obj).State = EntityState.Modified;
        this.Dal.SaveChanges();
        return obj;
    }
    public virtual bool Delete(T obj)
    {
        if (obj is ILogicallyDeletable)
        {
            this.Dal.Entry<T>(obj).State = EntityState.Modified;
            (obj as ILogicallyDeletable).IsDeleted = true;
        }
        else
        {
            this.Dal.Entry<T>(obj).State = EntityState.Deleted;
        }
        return this.Dal.SaveChanges() == 1;
    }
    public virtual bool Delete(int id)
    {
        T obj = Find(id);
        return Delete(obj);
    }
    public virtual bool Delete(Expression<Func<T, bool>> predicate)
    {
        foreach (T item in Select(predicate))
        {
            Delete(item);
        }
        return this.Dal.SaveChanges() == 1;
    }
    public virtual void Dispose()
    {
        this.Dal.Dispose();
    }
}

我们的经理是这样的:

public interface IBaseManager<T> : IDisposable where T : class, PrivateObject
{
    T Find(int? id);
    T Find(Expression<Func<T, bool>> predicate);
    ICollection<T> Select(Expression<Func<T, bool>> predicate = null,
        Expression<Func<T, string>> orderBy = null,
        string includeProperties = "");
    T Create(T obj);
    T Update(T obj);
    bool Delete(T obj);
    bool Delete(int id);
    bool Delete(Expression<Func<T, bool>> predicate);
    IQueryable<T> SelectAsQuery(
        Expression<Func<T, bool>> predicate = null,
        Expression<Func<T, string>> orderBy = null,
        string includeProperties = "");
}
public class BaseManager<T> : IBaseManager<T> where T : class, PrivateObject
{
    protected IBaseRep<T> Repository;
    public virtual T Find(int? id)
    {
        return this.Repository.Find(id);
    }
    public virtual T Find(Expression<Func<T, bool>> predicate)
    {
        return this.Repository.Find(predicate);
    }
    public virtual ICollection<T> Select(
        Expression<Func<T, bool>> predicate = null,
        Expression<Func<T, string>> orderBy = null,
        string includeProperties = "")
    {
        return this.Repository.Select(predicate, orderBy, includeProperties);
    }
    public virtual IQueryable<T> SelectAsQuery(
        Expression<Func<T, bool>> predicate = null,
        Expression<Func<T, string>> orderBy = null,
        string includeProperties = "")
    {
        return this.Repository.SelectAsQuery(predicate, orderBy, includeProperties);
    }
    public virtual T Create(T obj)
    {
        return this.Repository.Create(obj);
    }
    public virtual T Update(T obj)
    {
        return this.Repository.Update(obj);
    }
    public virtual bool Delete(T obj)
    {
        return this.Repository.Delete(obj);
    }
    public virtual bool Delete(int id)
    {
        return this.Repository.Delete(id);
    }
    public virtual bool Delete(Expression<Func<T, bool>> predicate)
    {
        return this.Repository.Delete(predicate);
    }
    public virtual void Dispose()
    {
        if (this.Repository != null)
            this.Repository.Dispose();
    }
}

这很好用。

但是,我们现在需要对多个实体类型使用相同的数据库表:

public abstract class AbstractSite : PrivateObject, IActivable, ILogicallyDeletable
{
    public int Id { get; set; }
}
public class EthicCommittee : AbstractSite
{
    public int Number { get; set; }
}
public class Site : AbstractSite
{
    public string Name { get; set; }
}

这就是我们使用通用管理器的方式:

public class AbstractSiteManager : BaseManager<AbstractSite>
{
    public AbstractSiteManager (PrismaDAL prismaDAL = null)
    {
        this.Repository = new AbstractSiteRep(prismaDAL);
    }
}

以及我们如何使用通用存储库:

public class AbstractSiteRep : PrismaBaseRep<AbstractSite>
{
    public AbstractSiteRep (PrismaDAL prismaDAL = null)
        : base(prismaDAL)
    {}
}
public class PrismaBaseRep<T> : BaseRep<T> where T : class, PrivateObject
{
    public PrismaBaseRep(PrismaDAL prismaDAL = null) : base((prismaDAL == null) ? new PrismaDAL() : prismaDAL)
    { }
}

但是现在,我们想使用具体类型,而不是抽象类型(抽象网站=抽象;网站=具体,招聘机构=具体...无需触摸通用存储库/管理器。因此,我们将拥有 X 个通用存储库(其中 X :具体类型的数量)。所有这些都指向同一个数据库表。这将使我们能够避免强制转换,并允许我们限制可以使用一个管理器/存储库操作的类型。

伙计们,你们知道我该如何做到这一点吗?

实体框架、抽象类、通用存储库和通用管理器

已解决我的错误。

正如@Mike C所说,这工作正常。

我只是不知道如果我引用的是具体对象类型而不是抽象(在 TPH 中),EF 能够找到正确的表。