• Backend for Frontend pattern - Challenges and Pitfalls - Exams of ASP.NET - Managing the shopping cart - Revisiting the CQRS pattern

    Challenges and Pitfalls – Modular Monolith

    It is a misconception that monoliths are only suitable for small projects or small teams. A well-built monolith can go a long way, which is what a modular structure and a good analysis bring. Modular Monoliths can be very powerful and work well for different sizes of projects. It is essential to consider the pros and cons of each approach when selecting the application pattern for a project. Maybe a cloud-native, serverless, microservices application is what your next project needs, but perhaps a well-designed Modular Monolith would achieve the same performance at a tremendously lower cost.To help you make those…

  • Challenges and Pitfalls - Exams of ASP.NET - Managing the shopping cart - Revisiting the CQRS pattern - The message broker

    Sending HTTP requests to the API – Modular Monolith

    Now that we have a working modular monolith, we can reuse similar HTTP requests than the previous versions.At the root of the REPR.API project, we can use the following: A new outcome in this API compared to the previous versions is when we try to add a product that does not exist in the catalog to our basket, like the following request: POST https://localhost:7164/basketsContent-Type: application/json{    “customerId”: 1,    “productId”: 5,    “quantity”: 99} Since the product 5 does not exist, the API returns the following: HTTP/1.1 400 Bad RequestContent-Type: application/problem+json{  “type”: “https://tools.ietf.org/html/rfc9110#section-15.5.1”,  “title”: “One or more validation errors occurred.”,  “status”: 400,  “errors”:…

  • Exams of ASP.NET - Managing the shopping cart - Revisiting the CQRS pattern - The message broker - What is a Modular Monolith?

    Inside the aggregator – Modular Monolith

    The aggregator application is like an empty shell that loads the other assemblies and configures the cross-cutting concerns. It references the modules, then assembles and boots the application. Here’s the first part of the Program.cs file: using FluentValidation;using FluentValidation.AspNetCore;using MassTransit;using REPR.API.HttpClient;using REPR.Baskets;using REPR.Products;using System.Reflection;var builder = WebApplication.CreateBuilder(args);// Register fluent validationbuilder.AddFluentValidationEndpointFilter();builder.Services    .AddFluentValidationAutoValidation()    .AddValidatorsFromAssemblies(new[] {        Assembly.GetExecutingAssembly(),        Assembly.GetAssembly(typeof(BasketModuleExtensions)),        Assembly.GetAssembly(typeof(ProductsModuleExtensions)),    });builder.AddApiHttpClient();builder.AddExceptionMapper();builder    .AddBasketModule()    .AddProductsModule();builder.Services.AddMassTransit(x =>{    x.SetKebabCaseEndpointNameFormatter();    x.UsingInMemory((context, cfg) =>    {        cfg.ConfigureEndpoints(context);    });    x.AddBasketModuleConsumers();}); The preceding code registers the following: The container we register those dependencies into is also used for and by the modules because the aggregator is the host. Those dependencies are shared across…

  • ASP.NET Certifications - Exams of ASP.NET - Managing the shopping cart - Revisiting the CQRS pattern - The message broker - What is a Modular Monolith?

    Consuming the events from the basket module – Modular Monolith

    The basket module wants to cache existing products. We can achieve this in different ways. In this case, we create the following Product class that we persist in the database: namespace REPR.Baskets.Data;public record class Product(int Id); To make use of it, we must expose the following property from the BasketContext class: public DbSet<Product> Products => Set<Product>(); Then, we can start to leverage this cache. Firstly, we must populate it when the catalog module creates a product and remove that product when deleted. The ProductEventsConsumers class handles both events. Here’s the skeleton of this class: using REPR.Products.Contracts;namespace REPR.Baskets.Features;public class ProductEventsConsumers :…

  • ASP.NET Certifications - Backend for Frontend pattern - Exams of ASP.NET - Revisiting the CQRS pattern - The message broker - What is a Modular Monolith?

    Sending events from the catalog module – Modular Monolith

    For the catalog to communicate the events that the basket module needs, it must define the following new operations: Here are the API contracts we must create in the REPR.Products.Contracts project to support those two operations: namespace REPR.Products.Contracts;public record class CreateProductCommand(string Name, decimal UnitPrice);public record class CreateProductResponse(int Id, string Name, decimal UnitPrice);public record class DeleteProductCommand(int ProductId);public record class DeleteProductResponse(int Id, string Name, decimal UnitPrice); The API contracts should look very familiar by now and are similar to those from previous chapters. We then need the following two event contracts: namespace REPR.Products.Contracts;public record class ProductCreated(int Id, string Name, decimal UnitPrice);public record…

  • ASP.NET Certifications - Backend for Frontend pattern - Exams of ASP.NET - Managing the shopping cart - Revisiting the CQRS pattern

    Planning the project – Modular Monolith

    Planning is a crucial part of any software. Without a plan, you increase your chances of building the wrong thing. A plan does not guarantee success, but it improves your chances. Overplanning is the other side of this coin. It can lead to analysis paralysis, which means you may never even deliver your project or that it will take you so long to deliver it that it will be the wrong product because the needs changed along the way and you did not adapt.Here’s a high-level way to accomplish planning a Modular Monolith. You don’t have to execute all the…

  • Challenges and Pitfalls - Exams of ASP.NET - Managing the shopping cart - Revisiting the CQRS pattern

    What is a Modular Monolith? – Modular Monolith

    Before you begin: Join our book community on Discord Give your feedback straight to the author himself and chat to other early readers on our Discord server (find the “architecting-aspnet-core-apps-3e” channel under EARLY ACCESS SUBSCRIPTION). https://packt.link/EarlyAccess In the ever-evolving software development landscape, choosing the right architecture is like laying the foundation for a building. The architecture dictates how the software is structured, impacting its scalability, maintainability, and overall success. Traditional monolithic architecture and microservices have long been the dominant paradigms, each with advantages and challenges.However, a new architectural style has been gaining traction—Modular Monoliths. This approach aims to offer the…

  • Exams of ASP.NET - Managing the shopping cart - Revisiting the CQRS pattern - The message broker

    Summary – Introduction to Microservices Architecture

    The microservices architecture is different from everything we’ve covered in this book and how we build monoliths. Instead of one big application, we split it into multiple smaller ones called microservices. Microservices must be independent of one another; otherwise, we will face the same problems associated with tightly coupled classes, but at the cloud scale.We can leverage the Publish-Subscribe design pattern to loosely couple microservices while keeping them connected through events. Message brokers are programs that dispatch those messages. We can use event sourcing to recreate the application’s state at any point in time, including when spawning new containers. We…

  • Exams of ASP.NET - Managing the shopping cart - Revisiting the CQRS pattern - The message broker - What is a Modular Monolith?

    Conclusion – Introduction to Microservices Architecture

    We explored the Microservice Adapter pattern that allows us to connect two elements of a system by adapting one to the other. We explored how to push information from an event broker into an existing system that does not support such capabilities. We also explored how to leverage an adapter to break tight coupling, migrate features into a newer system, and decommission a legacy application seamlessly. We finally connected two event brokers through an adapter microservice, allowing a low-powered IoT device to communicate with a microservices system without draining their battery and without the complexity it would incur to use…

  • ASP.NET Certifications - Exams of ASP.NET - Revisiting the CQRS pattern - The message broker - What is a Modular Monolith?

    Adapting an event broker to another – Introduction to Microservices Architecture

    In this scenario, we are adapting an event broker to another. In the following diagram, we look at two use cases: one that translates events from broker B to broker A (left) and the other that translates events from broker A to broker B (right). Afterwards, we explore a more concrete example:  Figure 19.42: An adapter microservice that translates events from broker B to broker A (left) and from broker A to broker B (right)  We can see the two possible flows in the preceding diagram. The first flow, on the left, allows the adapter to read events from broker…