-1

What are rules when creating C# Domain Classes in MVC, and how should they differ from Scaffold Database Model Classes?

Database classes are often normalized, for easier crud operation. Are domain classes generally more flat and denormalized?

Additionaly, this article states to separate change tracking temporal table, and remove them from domain classes.

https://softwareengineering.stackexchange.com/questions/363001/separating-the-domain-model-from-the-data-model

Current question is not a duplicate - as this question is more about C# physical materialized classes and not about modelling itself.

What's the difference between Data Modelling and Domain Modelling?

Tony Ngo
  • 19,166
  • 4
  • 38
  • 60
  • This is too broad for stack overflow – TheGeneral Aug 09 '19 at 02:41
  • aw man new it was coming, how would you rephrase the question, and why was this acceptable https://stackoverflow.com/questions/3507671/whats-the-difference-between-data-modelling-and-domain-modelling ? What's the difference between Data Modelling and Domain Modelling? –  Aug 09 '19 at 02:41
  • Might be better for SoftwareEngineering, those kids dont mind answering these sorts of design questions – TheGeneral Aug 09 '19 at 02:41
  • well that question was allowed on stack, https://stackoverflow.com/questions/3507671/whats-the-difference-between-data-modelling-and-domain-modelling –  Aug 09 '19 at 02:42
  • 10 years ago :) – TheGeneral Aug 09 '19 at 02:42
  • well for a person legitimately trying to learn, you think software exchange is better? ok I will ask there then –  Aug 09 '19 at 02:43

2 Answers2

1

The terms need to be defined better. Your scaffolded EF classes are "entities", hence the "Entity" in Entity Framework. Entities are strictly a class with an identifier. The identity of the class is tied to the identitifer, so two things are equal only if they have the same identifier.

When you start talking about something like a "domain model", that, too, is an entity, but it's a more expansive concept than what EF deals with. A domain entity has behavior and encapsulates business logic. The problem comes when people try to blur the lines between EF's concept of an entity and a domain entity.

In DDD, the infrastructure is completely abstracted. Persistence is not an issue for the domain, but rather for your infrastructure layer. EF is that infrastructure layer. By trying to use the EF entity as a domain entity, you're creating a hard dependency between the domain and the infrastructure, which causes nothing but problems.

As a result, you want to have separate domain entities where you encapsulate your business logic. Then, in your infrastructure layer, you translate these domain entities into corresponding EF entities for the simple purpose of persistence. As such, the EF entities should be very basic, containing only logic that's necessary for persistence, i.e. no business logic.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • hi Chris, can you cite an example, write classes in C# or Java, and show how they are different? Thanks, –  Aug 09 '19 at 14:47
  • 1
    I'd encourage you to simply read up on DDD. The differences are stark, and not easy to show in some simplified compare/contrast scenario, especially in this format. The EF entity will just be props, with maybe some data annotations. Nothing spectacular. The domain model is *much* more. – Chris Pratt Aug 09 '19 at 15:10
  • well we do have repository methods for each our domains, I have been trying to find a github project which can highlight differences,don't see one yet, –  Aug 09 '19 at 15:33
0

Domain class:

For me the domain class is how you define your application model.

Like example I have CMS system like Wordpress I will have some model like Post, Comment, Settings, etc...

So those model are the plain POCO object allow you define the shape of your application.

Database Scaffold Classes:

As the name speak for it's self. It's the class that dotnet ef cli tool generate for you. It hold the definition of database like this

 protected override void Up(MigrationBuilder migrationBuilder)
 {
                migrationBuilder.CreateTable(
                    name: "AspNetRoles",
                    columns: table => new
                    {
                        Id = table.Column<string>(nullable: false),
                        Name = table.Column<string>(maxLength: 256, nullable: true),
                        NormalizedName = table.Column<string>(maxLength: 256, nullable: true),
                        ConcurrencyStamp = table.Column<string>(nullable: true),
                        Discriminator = table.Column<string>(nullable: false)
                    },
                    constraints: table =>
                    {
                        table.PrimaryKey("PK_AspNetRoles", x => x.Id);
                    });
}

protected override void Down(MigrationBuilder migrationBuilder)
{
            migrationBuilder.DropTable(
                name: "AspNetRoleClaims");
}

Each time you change your domain class and you run dotnet ef migration add MigrationName it will generate new Database Scaffold Classes that reflect the new change for the new database. Database Scaffold Classes contain 2 method Up and Down. Up will allow you add new table or modifty column. Down is to drop table.

Tony Ngo
  • 19,166
  • 4
  • 38
  • 60
  • hi TonyNgo, can you cite an example, write classes in C# or Java, and show how they are different? Thanks, –  Aug 09 '19 at 14:48
  • I dont know java. So I cant write it. – Tony Ngo Aug 09 '19 at 15:08
  • I have been trying to locate a github project which can highlight differences,can't find one yet –  Aug 09 '19 at 15:33