简体   繁体   English

如何使用抽象类型的导航属性实现基于 model 的 EF Core 抽象

[英]How to implement EF Core abstract based model with navigation properties of abstract types

I am trying to implement common DDD-based entity using Entity Framework Core.我正在尝试使用 Entity Framework Core 实现基于 DDD 的通用实体。 I have 2 separated databases, external and internal, which shares common table models and logic in some places.我有 2 个独立的数据库,外部的和内部的,它们在某些地方共享通用的表模型和逻辑。 Ive tried to implement it that way:我试图以这种方式实现它:

public abstract class EmployeeBase
{
  public Guid Id {get;}
  public string Name {get; protected set;}
  public Guid DepartmentId {get; protected set;}

  public DeparmentBase {get; protected set;}

  public void DomainMethod1() {...}
  public void DomainMethod2() {...}
}
public class InternalEmployee : EmployeeBase
{
}
public class ExternalEmployee : EmployeeBase
{
}
public abstract class DepartmentBase
{
  public Guid Id {get;}
  public string Name {get; protected set;}

  public ICollection<EmployeeBase> Employees {get; protected set;}

  public void DomainMethod1() {...}
  public void DomainMethod2() {...}
}
public class InternalDepartment : DepartmentBase
{
}
public class ExternalDepartment : DepartmentBase
{
}

Is it even possible to to that in entity framework core?甚至有可能在实体框架核心中做到这一点吗? I can not find the way to configure it using FluentAPI.我找不到使用 FluentAPI 配置它的方法。 Can anyone help?谁能帮忙? I am also not sure if it is a good idea to implement domain logic in abstract classes with nested properties of abstract type as well.我也不确定在具有抽象类型嵌套属性的抽象类中实现域逻辑是否是个好主意。 Is there any better alternative?有没有更好的选择? Thank you in advance!ent提前谢谢你!ent

Is it even possible to to that in entity framework core?甚至有可能在实体框架核心中做到这一点吗?

Yes it is, documentation is here: https://learn.microsoft.com/en-us/ef/core/modeling/inheritance是的,文档在这里: https://learn.microsoft.com/en-us/ef/core/modeling/inheritance

I am also not sure if it is a good idea to implement domain logic in abstract classes with nested properties of abstract type as well.我也不确定在具有抽象类型嵌套属性的抽象类中实现域逻辑是否是个好主意。 Is there any better alternative?有没有更好的选择?

You are confusing DDD entities (domain model) with EF entities (persistence model).您将 DDD 实体(域模型)与 EF 实体(持久性模型)混淆了。

Implementing domain logic in an abstract class with nested abstract properties is not a problem.在具有嵌套抽象属性的抽象 class 中实现域逻辑不是问题。 What is a problem is doing that in an EF model, as it puts lots of stress on the object-relational mapping, especially if you have two back-end databases.在 EF model 中这样做有什么问题,因为它会给对象关系映射带来很大压力,尤其是当您有两个后端数据库时。

What an ORM (such as EF core) does is translating operations on objects into the relational world, and vice-versa. ORM(例如 EF 核心)所做的是将对象上的操作转换为关系世界,反之亦然。 They usually manipulate POCO objects with as few logic as possible inside, because they are symbols: they represent , in the object world, database concepts, such as tables, rows, columns, indexes, etc...他们通常以尽可能少的内部逻辑来操作 POCO 对象,因为它们是符号:它们代表object 世界中的数据库概念,例如表、行、列、索引等...

You can use the same business-persistence model if your application is designed with a domain-first approach.如果您的应用程序是使用域优先方法设计的,则可以使用相同的业务持久性 model。 In that situation, the ORM is only responsible to persist the domain state as-is and the database structure follows by design the domain model. However, this model can become very complex, because it must both model the business logic and respect database design good practices.在这种情况下,ORM 只负责按原样保留域 state 数据库结构遵循域 model的设计。但是,这个 model 可能会变得非常复杂,因为它必须既 model 业务逻辑尊重数据库设计良好做法。 It only works if your business model is trivial.它仅在您的业务 model 微不足道时才有效。

Here, you have constraints on your database structure (two databases) and a complex domain model (with abstract/polymorphism), so you are not free to use your persistence layer a simple "save my domain state" library.在这里,您对数据库结构(两个数据库)和复杂域 model(具有抽象/多态性)有限制,因此您不能随意使用持久层一个简单的“保存我的域状态”库。 You should split your concerns into different models:您应该将您的关注点分成不同的模型:

  • One persistence model per database, with anemic POCO and no abstract classes or properties每个数据库一个持久性 model,具有贫乏的 POCO 并且没有抽象类或属性
  • One domain model with abstract classes and properties, implementing your domain logic一个域 model 具有抽象类和属性,实现您的域逻辑

You may then implement an adapter between the models, with a repository pattern:然后,您可以使用存储库模式在模型之间实现一个适配器:

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

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