C# 存储库之间的依赖关系

C# 存储库之间的依赖关系,c#,entity-framework,repository,domain-driven-design,C#,Entity Framework,Repository,Domain Driven Design,我正在使用实体框架和DDD 我有以下实体:个人、客户、员工 人是抽象的。 客户和员工继承人。 Person引用了Person地址(列表) 我应该为每种类型设置一个存储库,还是只为每种具体类型设置一个存储库?(只有客户和员工) 我是否可以让一个存储库人员在客户和员工存储库内部依赖它来避免冗余代码?(通过构造函数注入使用DI)我只需要为客户和员工提供存储库 如果它们之间存在共享逻辑,则将其封装在抽象基类中,并让存储库从中继承 所以你会得到这种结构: public interface ICustome

我正在使用实体框架和DDD

我有以下实体:个人、客户、员工

人是抽象的。 客户和员工继承人。 Person引用了Person地址(列表)

我应该为每种类型设置一个存储库,还是只为每种具体类型设置一个存储库?(只有客户和员工)


我是否可以让一个存储库人员在客户和员工存储库内部依赖它来避免冗余代码?(通过构造函数注入使用DI)

我只需要为客户和员工提供存储库

如果它们之间存在共享逻辑,则将其封装在抽象基类中,并让存储库从中继承

所以你会得到这种结构:

public interface ICustomerRepo { }

public interface IEmployeeRepo {  }

public abstract class PersonRepoBase<T> {  }

public class CustomerRepo : PersonRepoBase<Customer>, ICustomerRepo { }

public class EmployeeRepo : PersonRepoBase<Employee>, IEmployeeRepo { }
公共接口ICCustomerRepo{}
公共接口IEmployeeRepo{}
公共抽象类PersonRepoBase{}
公共类CustomerRepo:PersonRepoBase,ICCustomerRepo{}
公共类EmployeeRepo:PersonRepoBase,IEEmployeeRepo{}

这并不是要给出一个完整的应用程序结构,而是一个很好的基础,用于设计在使用不同的存储库和继承等时可能需要的内容

// Model stuff...
public interface IBaseEntity
{
    [Key]
    int Id { get; set; }
}

public abstract class BaseEntity : IBaseEntity
{
    [Key]
    public virtual int Id { get; set; }

    // Add some extra fields for all Models that use BaseEntity
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    [Display(Name = "Last Modified")]
    public virtual DateTime LastModified { get; set; }

    [ConcurrencyCheck]
    [Timestamp]
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public virtual byte[] Timestamp { get; set; }
}

public class Person : BaseEntity
{
    // Person Model here...
}

public class Employee : Person
{
    // Extra Employee Model items here
}

public class Customer : Person
{
    // Extra Customer Model items here
}
// End Model stuff

// Repository stuff...
public interface IRepository<T> where T : class
{
    IQueryable<T> GetAll();
    T GetById(int? id);
    T Add(T entity);
    void Update(T entity);
    void Delete(T entity);
    void Delete(int id);
    void Commit(); // To save changes rather than performing a save after each Add/Update/Delete etc.
}

public class EFRepository<T> : IRepository<T> where T : class, IBaseEntity
{
    public virtual IQueryable<T> GetAll()
    {
        return DbSet.AsQueryable<T>();
    }

    public virtual T GetById(int? id)
    {
        var item = DbSet.Find(id);
        return item;
    }

    public virtual T Add(T entity)
    {
        DbEntityEntry dbEntityEntry = DbContext.Entry(entity);
        if (dbEntityEntry.State != EntityState.Detached)
        {
            dbEntityEntry.State = EntityState.Added;
        }
        else
        {
            DbSet.Add(entity);
        }
        // SaveChanges() - removed from each DbSet, so can call on all changes in one transaction.
        // Using Unit Of Work Pattern. Can still use it here though if wished.
        return entity;
    }
    // And so on for each storage method.
}

public interface IEmployeeRepository: IRepository<Employee>
public interface ICustomerRepository: IRepository<Customer>

public class EmployeeRepository : EFRepository<Employee>, IEmployeeRepository
public class CustomerRepository : EFRepository<Customer>, ICustomerRepository
// End Repository stuff
//模型材料。。。
公共接口实体
{
[关键]
int Id{get;set;}
}
公共抽象类BaseEntity:IBaseEntity
{
[关键]
公共虚拟整数Id{get;set;}
//为使用BaseEntity的所有模型添加一些额外字段
[数据库生成(DatabaseGeneratedOption.Computed)]
[显示(Name=“上次修改”)]
公共虚拟日期时间LastModified{get;set;}
[并发检查]
[时间戳]
[数据库生成(DatabaseGeneratedOption.Computed)]
公共虚拟字节[]时间戳{get;set;}
}
公共类人物:BaseEntity
{
//这里的人物模型。。。
}
公共类员工:人
{
//这里有额外的员工模型项
}
公共类客户:个人
{
//这里有额外的客户模型项目
}
//终端模型材料
//存储库的东西。。。
公共接口IRepository,其中T:class
{
IQueryable GetAll();
T GetById(int?id);
T添加(T实体);
无效更新(T实体);
无效删除(T实体);
无效删除(int-id);
void Commit();//保存更改,而不是在每次添加/更新/删除等操作后执行保存。
}
公共类eRepository:IRepository,其中T:class,IBaseEntity
{
公共虚拟IQueryable GetAll()
{
返回DbSet.AsQueryable();
}
公共虚拟T GetById(int?id)
{
var项目=数据库集查找(id);
退货项目;
}
公共虚拟T添加(T实体)
{
DbEntityEntry DbEntityEntry=DbContext.Entry(实体);
if(dbEntityEntry.State!=EntityState.Distached)
{
dbEntityEntry.State=EntityState.Added;
}
其他的
{
添加(实体);
}
//SaveChanges()-从每个数据库集中删除,因此可以在一个事务中调用所有更改。
//使用工作单元模式。如果愿意,仍然可以在此处使用它。
返回实体;
}
//对于每种存储方法,等等。
}
公共接口IEmployeeRepository:IRepository
公共接口ICCustomerRepository:IRepository
公共类EmployeeRepository:eRepository,IEmployeeRepository
公共类CustomerRepository:eRepository,ICustomerRepository
//结束存储库的东西
基本上,您可以通过声明接口和类来添加新模型及其存储库,而这些接口和类中几乎没有任何内容。一切都继承自基本crud功能等。 对于要添加的模型,在特殊情况下,您只需要添加从数据库获取记录的新方法-FindEmployeeByHairColor(颜色)。例如,所有其他EF GET、FIND等,无论类型如何,都是相同的

这可能会非常深入,使用服务提供对存储库中核心方法的访问,添加工作单元模式以将多个数据库集组合到一个事务中,等等

但是使用这种类型的布局允许我将我希望使用的特定存储库/服务注入到每一层,并将所有逻辑保留在一个类中,该类在使用类似逻辑的所有内容中重复使用

希望这有帮助