带有工作单元的存储库模式的常用方法

时间:2019-08-09 15:29:06

标签: .net-core c#-7.3

我有一个项目,我试图使用带有存储库设计模式的工作单元来使用EF Core执行CRUD操作。 FWIW,我在.NET Core 2.2上。

问题:

我是否可以创建一个(抽象的)类来为某些模型而非某些模型实现某些操作?在当前的思考过程中,我想在CountRepository类中执行此操作。我想从TypeA,TypeB和TypeC返回实体记录-这些实体记录满足CountDate和XCount字段的特定条件。

布局和详细信息:

Solution.EFProject

  • 模型\

    • TypeA.cs
    • TypeB.cs
    • TypeC.cs
    • OtherModel.cs
  • BaseEntity.cs
  • BaseTypeEntity.cs
  • Context.cs

Solution.DAL

  • 核心\
    • 存储库\
      1. IRepository.cs
      2. ICountRepository.cs
      3. ITypeARepository.cs
      4. ITypeBRepository.cs
      5. ITypeCRepository.cs
      6. IOtherModelRepository.cs
    • IUnitOfWork.cs

  • 持久性\
    • 存储库\
      1. Repository.cs
      2. CountRepository.cs
      3. TypeARepository.cs
      4. TypeBRepository.cs
      5. TypeCRepository.cs
      6. OtherModelRepository.cs
    • UnitOfWork.cs

Solution.EFProject.BaseEntity.cs

public abstract  class BaseEntity
{
    public bool Enabled { get; set; }
    public DateTime CreatedDate { get; set; }

    public BaseEntity()
    {
        Enabled = true;
        CreatedDate = DateTime.Now;
    }
}

Solution.EFProject.BaseCountEntity.cs

public abstract class BaseCountEntity : BaseEntity
{
    public DateTime CountDate { get; set; }
    public long XCount { get; set; }
}

Solution.EFProject.Models.TypeA.cs

public class TypeA : BaseCountEntity 
{
  public int TypeAId { get; set; }
  public long ACount { get; set; }
}

Solution.EFProject.Models.TypeB.cs

public class TypeB : BaseCountEntity 
{
  public int TypeAId { get; set; }
  public long B1Count { get; set; }
  public long B2Count { get; set; }
}

Solution.EFProject.Models.TypeC.cs

public class TypeB : BaseCountEntity 
{
  public int TypeAId { get; set; }
  public long C1Count { get; set; }
  public long C2Count { get; set; }
  public long C3Count { get; set; }
}

Solution.DAL.Persistence.Repositories.Repository.cs

public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
    protected readonly DbContext Context;
    protected readonly DbSet<TEntity> _entities;

    public Repository(DbContext context)
    {
        Context = context;
        _entities = Context.Set<TEntity>();
    }

    // GET method(s) on <TEntity> collection
    // ADD method(s) on <TEntity> collection
    // UPDATE method(s) on <TEntity> collection
    // REMOVE method(s) on <TEntity> collection
}

0 个答案:

没有答案