English 中文(简体)
C# DDD with EF 核心使用同一领域实体/制图表或单独类别汇总
原标题:C# DDD with EF Core use same domain entity / aggregate for mapping table or separate class

I don t know which option is most suitable between those 2.

备选办法1:使用聚合物或实体(合计次级)绘制表格。

public class User : AggregateRoot 
{
    string Name { get; }
    string Email { get; }

    // Logic ...
}

如今,欧洲法院 核心实体:

public class EntityTypeConfiguration : IEntityTypeConfiguration<User> 
{
    public void Configure(EntityTypeBuilder<User> builder) 
    {
        // ... map props.
    }
}

备选办法2:重复编码,设立2个类别,但范围本身。

public class User : AggregateRoot 
{
    string Name { get; }
    string Email { get; }

    // Logic ...
}

public class UserTable 
{
    Guid Id { get; }
    string Name { get; }
    string Email { get; }

    // No logic here ...
}
public class EntityTypeConfiguration : IEntityTypeConfiguration<UserTable> 
{
    public void Configure(EntityTypeBuilder<UserTable> builder) 
    {
        // ... map props.
    }
}

有了第二种选择,我可以把我的实体保留在同一倍,例如<条码>EFCoreEntities,法典应当更清洁,但有许多重复的代码。

采用第二种方法,我必须在<代码>UserTable和User/code>之间(可能使用地图/自动制图仪)书写额外地图。

例如,这将是<代码>。 国家统计研究所:

// Here parameter is an User but have to map into UserTable
Add(User user) 
{
    var entity = user.MapToUserTable();
    dbContext.Users.Add(entity)
    // ...
}

你们使用哪种选择、任何利弊?

问题回答

这是一个人们很少谈论的非常好的问题。 许多人将告诉你,他们完全放弃DDD。

我先利用备选案文2,单独实体和领域,开发一个相对较大的系统。 以下是我的经验。

http://www.un.org

  • Domain is not restricted to the database schema.
    • This was our original motivation. However, we NEVER encountered a situation where the domain deviated from the schema. It was always 1-to-1. This means the mapping became an additional "indirection" where the expected deviation never happened.

<Option 2 Czech ?>:

  • Tons of mapping code that gets complex real quick. We also started doing tons of unit-testing just for the mapping.
  • Extra layer with no tangible benefit. If the mapping stays 1-to-1. This means the mapping became an additional "indirection" where the expected deviation never happened.
    • I also found the extra layer made it harder to navigate.

Option 1 is a cleaner approach BUT with two caveats

  1. www.un.org/Depts/DGACM/index_french.htm 数据库图示对贵领域进行了限制。 确保数据库图形设计良好。 实行适当的正常化。 如果你的团队在数据库设计上没有良好的背景,那么随着这个领域变得复杂,就更难改变。

  2. <>2> 依靠1至many导航特性。 例如,如果利用收集的导航特性进行验证,而你忘记装载这些特性,那么你的验证就会无声失败,因为从域方面看,无法知道导航特性是装上还是没有装上。 这增加了领域与持久性之间的汇合水平,而该领域应注意持久性。

解决办法要么是

  1. Get comfortable with lazy loading
  2. Enable auto-loading from the IEntityTypeConfiguration.

这两项加权办法在扩大规模方面都具有不同的业绩影响,但也有减轻这种影响的办法。 例: 在收集大量数据、必要时应用预测、使用假肢以及考虑数据库中储存的计算前数据时。

If you ever decide to enable lazy loading, preemptively catch any "1-N select problem" as soon as they happen. Setup a performance monitoring, integration, and load testing

最后,备选案文2探讨影子特性,只暴露需要暴露的领域。 采用适当的资本,使贵实体/主要实体保持冰冰和原样。

它真正涉及贸易。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...