4 Using EF Core in business logic

 

This chapter covers

  • Understanding business logic and its use of EF Core
  • Using a pattern for building business logic
  • Working through a business logic example
  • Adding validation of 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 the approaches in chapters 2 and 3 all 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 Why is business logic so different from other code?

4.2 Our business need—processing an order for books

4.2.1 The business rules that you need to implement

4.3 Using a design pattern to help implement 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 it’s working on in-memory data

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

4.4.5 Guideline 5: Business logic shouldn’t call EF Core’s SaveChanges

4.4.6 Putting it all together—calling the order-processing business logic

4.4.7 Any disadvantages of this business logic pattern?

4.5 Placing an order on the book app

4.6 Adding extra features to your business logic handling

4.6.1 Validating the data that you write to the database

4.6.2 Using transactions to daisy-chain a sequence of business logic code

Summary