域实体中的外键属性 [英] Foreign key properties in domain entities

查看:101
本文介绍了域实体中的外键属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在域驱动设计中,对于任何数据持久性细节,域模型应该是完全未知的.

In Domain-Driven Design, the domain model should be completely unbeknownst of any data persistent specifics.

假设Employee属于Department.域实体可能看起来像这样:

Let's say that an Employee belongs to a Department. The domain entities could look like this:

public Employee
{
    public string EmployeeId { get; set; }
    public string FirstName { get; set; }
    public string LastName{ get; set; }
    public Department Department { get; set; }
    public int DepartmentId { get; set; }
}

public Department
{
    public string DepartmentId { get; set; }
    public string Name{ get; set; }
}

Employee.DepartmentId与域模型是否真的相关,还是基础架构存储详细信息?

Is Employee.DepartmentId really relevant in the domain model or is that an infrastructure storage detail?

在这个级别上,关系当然重要吗?

Surely Employee.Department is the relationship that matters at this level?

在我的情况下,这些实体将存储在SQL数据库中,并且实体框架将检索数据,因此数据库中将存在Employee.DepartmentId列.

In my case, these entities will be stored in a SQL database and the data will be retrieved by Entity Framework, so an Employee.DepartmentId column will exist in the database.

推荐答案

如果使用外键,则在Entity Framework中生活会更轻松:

Life is easier in the Entity Framework if you use foreign keys:

为什么实体框架将现有对象重新插入到我的数据库中?

使用缺少的外键进行操作

您绝对正确地说外键与域模型无关.它是持久性模型的一部分.

And you are absolutely correct to say that the foreign key is not really relevant to the domain model. It is part of the persistence model.

因此,您需要确定要加入哪个阵营.您是纯粹主义者还是实用主义者?是否使用单独的域模型和持久性模型?

So you need to decide which camp to join. Are you a purist or a pragmatist? Separate domain models and persistence models or not?

实体框架下的ORM实体与域实体6.0

这篇关于域实体中的外键属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆