Catalog Microservice with Vertical Slice Architecture and CQRS

Mehmet Ozkaya
5 min readMar 22, 2024

--

In this article, we will start our EShop Microservices and very first microservices will be Catalog.API with Vertical Slice Architecture and CQRS.

I have just published course — .NET 8 Microservices: C# 12, DDD, CQRS, Vertical/Clean Architecture.

The Blueprint

Our mission is to construct the Catalog.API using a blend of cutting-edge technologies and architectural principles:

  • Minimal APIs: Combining the streamlined power of .NET 8 and C# 12 for rapid HTTP API development.
  • Vertical Slice Architecture: Modularizing our codebase into cohesive features for enhanced maintainability.
  • CQRS with MediatR: Separating read and write operations to increase scalability and flexibility.
  • Marten Library: Transforming PostgreSQL into a versatile Document DB, optimized for transactional operations.

Domain Analysis of Catalog Microservices

We’re delving into the Domain Analysis of Catalog Microservices. This foundational step, as outlined in our “Way of Learning” methodology, encompasses Domain Models, Application Use Cases, REST API Endpoints, and the Underlying Data Structures.

Domain Models

At the heart of our Catalog Microservices lies the ‘Product’ model, intricately linked with ‘Category’. A product in our domain is not just an item but an entity with attributes that define its essence — ID, Name, Category, Description, Image, and Price.

This detailed representation not only gives us clarity on the structure of our data but also prepares us for the potential domain events that might arise, such as price changes, which could lead to broader integration events.

Application Use Cases

Our Catalog isn’t just a repository; it’s an interactive service designed to cater to specific needs:

  • Listing Products and Categories: Providing a comprehensive view of what’s available.
  • Detailed Queries: Fetching products by ID or category to cater to specific searches.
  • CRUD Operations: The backbone of any service, allowing for the creation, update, and deletion of products.

This operational blueprint guides our RESTful API design, ensuring our services are not only functional but also intuitive and REST-compliant.

REST API Endpoints

Our service’s accessibility hinges on a set of well-defined RESTful APIs. These endpoints are the conduits through which our users interact with the Catalog:

  • GET /products: A gateway to the vast array of products we offer.
  • GET /products/{id} & /products/category: Tailored endpoints for fetching specific products or categories.
  • POST, PUT, DELETE /products: The tools for managing our catalog’s lifecycle, from creation to deletion.

Each endpoint is meticulously crafted, adhering to ASP.NET Core controller conventions, ensuring clarity and consistency across our service.

Underlying Data Structures

Underlying data structures will be Document Database to Store Catalog JSON data. For data storage, we have 2 options:

  • use MongoDB No-SQL database
  • use Postgres database JSON column feature with Marten library-provide — .NET Transactional Document DB

Each offers a robust way to store our catalog in a JSON format. For our Catalog Microservices, we will utilize PostgreSQL with the Marten library.
Marten is a powerful library that transforms PostgreSQL into a .NET Transactional Document DB. It leverages PostgreSQL’s JSON column features, allowing us to store and query our data as JSON documents.

Technical Analysis of Catalog Microservices

We’re taking a closer look at the tech backbone of our Catalog Microservices. Think of this as the blueprint guiding our coding adventure. We’re talking architecture, patterns, tools, and how we’re organizing our code.

Application Architecture Style

As an Application Architecture Style of Catalog microservice, we will use Vertical Slice Architecture.

This approach organizes our code into feature folders, with each feature encapsulated in a single .cs file containing all necessary classes.

Patterns & Principles

We’re mixing some solid strategies with smart design moves:

  • CQRS: We’re splitting how we read and write data, making things more efficient and easier to scale.
  • Mediator: This is like having a referee to pass messages between parts of our app, cutting down on chaos.
  • Dependency Injection: ASP.NET Core’s secret sauce that lets us plug in components without hard-wiring them.
  • Minimal APIs & Routing: We’re keeping our endpoints neat and easy to manage, thanks to ASP.NET 8’s slick setup.
  • ORM Goodness: We’re bridging our code with the database in a way that feels natural, without the SQL headache.

Libraries & Nuget Packages

We’ve handpicked some awesome tools from the .NET toolbox:

  • MediatR: Our go-to for implementing CQRS, making sure commands and queries find their handlers.
  • Carter: Simplifies setting up our API endpoints, so we can focus on the fun stuff.
  • Marten: This one’s cool; it turns PostgreSQL into a document database, perfect for our needs.
  • Mapster: Zaps data from one shape to another, making our data transformations a breeze.
  • FluentValidation: Keeps our data in check with some seriously smart validation rules.

Project Folder Structure

Here’s how we’re keeping our code neat and tidy:

  • Models: Our data blueprints, defining the shape of things like products.
  • Features: Each feature gets its folder, complete with everything it needs to run the show.
  • Data Handling: We’ve got a special spot for managing our chats with the database.
  • Abstractions: This is where we stash the common stuff, like our CQRS commands and queries, along with any shared logic.

Deployment of Catalog Microservices

We’re packing everything into Docker containers. It’s like giving each part of our service its little home, ready to be shipped out and run anywhere. And yes, that includes hooking up with PostgreSQL, so our data’s always at the ready.

Conclusion

The development of the Catalog.API microservice is not just a technical endeavor but a journey through the best practices of modern software architecture. By embracing Vertical Slice Architecture and CQRS, we ensure our service is ready to meet the demands of a scalable, maintainable, and efficient microservices ecosystem. Join us in this adventure, and let’s build something remarkable together.

I have just published course — .NET 8 Microservices: C# 12, DDD, CQRS, Vertical/Clean Architecture.

This is step-by-step development of reference microservices architecture that include microservices on .NET platforms which used ASP.NET Web API, Docker, RabbitMQ, MassTransit, Grpc, Yarp API Gateway, PostgreSQL, Redis, SQLite, SqlServer, Marten, Entity Framework Core, CQRS, MediatR, DDD, Vertical and Clean Architecture implementation with using latest features of .NET 8 and C# 12.

--

--

Mehmet Ozkaya
Mehmet Ozkaya

Written by Mehmet Ozkaya

Software Architect | Udemy Instructor | AWS Community Builder | Cloud-Native and Serverless Event-driven Microservices https://github.com/mehmetozkaya

No responses yet