AskReference
ExplanationIntermediate

Why does the author recommend creating separate service files for creatures and explorers instead of a single combined service file?

The author says it is tempting to write a single service file for both creatures and explorers, but almost inevitably the two resources will need to be handled differently at some point. A little extra structure at the start, meaning separate service files, will pay off later and simplify future changes.

The author notes that at this stage the service layer is basically a pass-through, and creature and explorer needs are nearly the same, which makes a combined file tempting. However, the author argues that almost inevitably they will need to be handled differently later. Creating separate service modules up front is a small amount of structure that pays off later, just as separate web modules were created in Chapter 8 to make later changes easier. The layout already mirrors this pattern by giving each resource its own file in the web, service, data, and model directories.

Key points

  • Combining both resources into one service file is tempting because the initial code is nearly identical and mostly pass-through.
  • The author says the services will almost inevitably need to handle creatures and explorers differently later.
  • Adding a little extra structure at the start is described as a case that will pay off later.
  • Separate service files mirror the separate web modules already created, making future changes and maintenance simpler.
Source:FastAPI: Modern Python Web Development· Service Layer· p. 125–137

Related questions

Cover of FastAPI: Modern Python Web Development

FastAPI: Modern Python Web Development

Bill Lubanovic;

First Edition · O'Reilly Media, Inc.

View this ebook