10 Useful software patterns for EF Core applications

 

This chapter covers

  • Applying the separation-of-concerns principle
  • Using a LINQ mapper to speed up development
  • Using a domain-driven-design approach to EF Core
  • Splitting your database across multiple DbContexts
  • Building error-handling for database errors

This chapter introduces techniques, patterns, and packages to help you become a more productive developer. I find that I become a better developer by taking a pattern or design principle that looks promising, using it in a project, reviewing how that went, and improving the pattern in the next project. This chapter shares techniques I’ve used and perfected over many years as well as some that I’ve only just starting looking at now that EF Core has been released.

The techniques in this chapter aren’t the only ones you could use, and not all the approaches I describe will be applicable to your needs, but they’re a good mix of techniques to consider. Like me, you won’t know if an approach is useful until you’ve used it in a real-world project, but you have to start somewhere. I hope this chapter gets you thinking, so enjoy the journey.

10.1 Another look at the separation-of-concerns principle

Section 5.5.2 covered the software design principle called separation of concerns (SoC). This design principle states that a software system should be decomposed into parts that overlap in functionality as little as possible. SoC is linked to two other principles:

10.2 Using patterns to speed development of database access

10.3 Speed up query development—use a LINQ mapper

10.4 Domain-driven-design database repository

10.4.1 Example Book DDD entity and repository

10.4.2 How the DDD design changes the business logic design

10.4.3 Impressions from building this DDD design

10.5 Is the Repository pattern useful with Entity Framework?

10.5.1 Some forms of Repository patterns to avoid

10.6 Splitting a database across multiple DbContexts

10.6.1 Creating DbContexts that contain only a subset of entities/tables

10.6.2 Passing data between bounded contexts

10.7 Data validation and error-handling patterns

10.7.1 Data validation to your entity classes makes for better error feedback

10.7.2 Business logic should contain checks and return a list of all errors

10.7.3 Catching database server errors and providing user-friendly feedback

Summary