Mastering Entity Framework for Object-Oriented Programming: Beyond Simple DTOs

JetBrains

Summary:
  • The video challenges the common practice of using Entity Framework (EF) primarily as a Data Transfer Object (DTO) provider, arguing it often leads to spreading business logic and validation across multiple service classes.
  • The speaker advocates for a more object-oriented programming (OOP) and Domain-Driven Design (DDD) approach, where EF is used as an Object-Relational Mapper (ORM) to persist rich domain objects.
  • Key EF Core features demonstrated include: using shadow properties for internal IDs, HasConversion for mapping value objects (like a Vehicle Identification Number - VIN), HasComplexProperty for composite value objects (like Name), and discriminators for inheritance mapping.
  • The presentation shows how to encapsulate business logic and validation directly within domain entities, making code more cohesive and readable.
  • Best practices are shared for query optimization (projections, AsNoTracking, split queries) and for managing database migrations outside of application startup, recommending the EF migrations bundle tool for deployment.
  • The core message is to design your domain objects first with proper encapsulation and behavior, then configure EF to map to these rich objects, rather than letting database structure dictate your code.
    DMV Domain Model
    DMV Domain Model [ 00:12:10 ]

Introduction & Context [00:00:00]

Understanding DTOs and Their Misuse [00:05:37]

Problems with DTO-centric EF Usage [00:07:32]

Example Domain: DMV System [00:11:36]

Traditional DTO and Service Approach (Critique) [00:13:03]

Proposed Solution: Wrapping EF Objects in OOP Entities [00:16:47]

Leveraging EF Core for True OOP (Demo) [00:23:00]

Key Takeaways and Best Practices [01:08:26]