3. App Architecture for the Shopping Cart Feature


Now that we know what we're trying to build, let's figure out how to build it.

This diagram shows all the required components:

Data Layer

We need three repositories:

  • LocalCartRepository: so we can use local storage when we add items to the cart as a guest
  • RemoteCartRepository: so we can use a remote database when we add items to the cart as a signed-in user
  • AuthRepository: to figure out if we're signed-in or not

Presentation Layer

We have these two widgets called AddToCart and ShoppingCartItem that can be used to modify the shopping cart contents.

In addition, we also have ShoppingCartIcon and CartTotalText widgets that need to rebuild when the shopping cart data changes.

Application Layer

The CartService class acts as a middle man between the controllers and the repositories.

This is the perfect place to write logic that:

  • depends on multiple data sources or repositories
  • needs to be used (shared) by more than one widget or controller

For more details, read this:

Complete and Continue