4 Using EF Core in business logic

 

This chapter covers

  • Understanding business logic and its use of EF Core
  • Looking at three types of business logic, from the easy to the complex
  • Reviewing each type of business logic, with pros and cons
  • Adding a step that validates the data before it’s written to the database
  • Using transactions to daisy-chain code sequences

Real-world applications are built to supply a set of services, ranging from holding a simple list of things on your computer to managing a nuclear reactor. Every real-world problem has a set of rules, often referred to as business rules, or by the more generic name domain rules. (This book uses business rules.)

The code you write to implement a business rule is known as business logic or domain logic. Because business rules can be complex, the business logic you write can also be complex. Just think about all the checks and steps that should be done when you order something online.

Business logic can range from a simple check of status to massive artificial intelligence (AI) code, but in nearly all cases, business logic needs access to a database. Although all the approaches in chapters 2 and 3 come into play, the way you apply those EF Core commands in business logic can be a little different, which is why I’ve written this chapter.

4.1 The questions to ask and the decisions you need to make before you start coding

4.1.1 The three levels of complexity of your business logic code

4.2 Complex business logic example: Processing an order for books

4.3 Using a design pattern to implement complex business logic

4.3.1 Five guidelines for building business logic that uses EF Core

4.4 Implementing the business logic for processing an order

4.4.1 Guideline 1: Business logic has first call on defining the database structure

4.4.2 Guideline 2: Business logic should have no distractions

4.4.3 Guideline 3: Business logic should think that it’s working on in-memory data

4.4.4 Guideline 4: Isolate the database access code into a separate project