diff --git a/docs/en/tutorials/microservice/images/vs-ordering-entity.png b/docs/en/tutorials/microservice/images/vs-ordering-entity.png new file mode 100644 index 0000000000..f81a60cb60 Binary files /dev/null and b/docs/en/tutorials/microservice/images/vs-ordering-entity.png differ diff --git a/docs/en/tutorials/microservice/part-03.md b/docs/en/tutorials/microservice/part-03.md index c265f6806b..949c8bb42f 100644 --- a/docs/en/tutorials/microservice/part-03.md +++ b/docs/en/tutorials/microservice/part-03.md @@ -96,7 +96,7 @@ Lastly, we need to configure the use of a static HTTP client for the `CatalogSer ```csharp public override void ConfigureServices(ServiceConfigurationContext context) { - // Abbreviated code for brevity + // Code omitted for brevity context.Services.AddStaticHttpClientProxies( typeof(CloudCrmCatalogServiceContractsModule).Assembly); } diff --git a/docs/en/tutorials/microservice/part-05.md b/docs/en/tutorials/microservice/part-05.md new file mode 100644 index 0000000000..64224f5757 --- /dev/null +++ b/docs/en/tutorials/microservice/part-05.md @@ -0,0 +1,113 @@ +# Microservice Tutorial Part 05: Building the Ordering module + +````json +//[doc-nav] +{ + "Previous": { + "Name": "Creating the initial Ordering Microservice", + "Path": "tutorials/microservice/part-04" + }, + "Next": { + "Name": "Integrating the modules: Implementing Integration Services", + "Path": "tutorials/microservice/part-06" + } +} +```` + +In the previous part, we created the Ordering microservice. In this part, we will implement the functionality of the Ordering microservice manually. We will not use ABP Suite to generate the code. Instead, we will create the necessary code step by step to understand the details better. + +## Creating the Order Entity + +We will start by creating the `Order` entity, which will represent an order in our system. We'll add this entity to the `CloudCrm.OrderingService` project. Open the `Entities` folder and place `Order.cs` into that folder. + +```csharp +using CloudCrm.OrderingService.Enums; +using Volo.Abp.Domain.Entities.Auditing; + +namespace CloudCrm.OrderingService.Entities; + +public class Order : CreationAuditedAggregateRoot +{ + public Guid ProductId { get; set; } + public string CustomerName { get; set; } + public OrderState State { get; set; } +} +``` + +To keep this example simple, we allow users to include only a single product within an order. The `Order` entity inherits from the [CreationAuditedAggregateRoot<>](../../framework/architecture/domain-driven-design/entities.md) class. This class, provided by the ABP Framework, includes common properties like `Id`, `CreationTime`, `CreatorId`, etc. + +### Adding the OrderState Enum + +We also need to define the `OrderState` enum. Open an `Enums` folder in the `CloudCrm.OrderingService.Contracts` project and create an `OrderState.cs` file inside it: + +```csharp +public enum OrderState : byte +{ + Placed = 0, + Delivered = 1, + Canceled = 2 +} +``` + +The final solution structure should look like this: + +![vs-ordering-entity](images/vs-ordering-entity.png) + +## Configuring the Database Mapping + +First, we need to add the `Order` entity to the `OrderingServiceDbContext` class. Open the `OrderingServiceDbContext` class in the `CloudCrm.OrderingService` project, located in the `Data` folder, and add the following code to the `DbSet` properties: + +```csharp +using CloudCrm.OrderingService.Entities; +using Microsoft.EntityFrameworkCore; +using Volo.Abp.Data; +using Volo.Abp.EntityFrameworkCore; +using Volo.Abp.EntityFrameworkCore.DistributedEvents; +using Volo.Abp.EntityFrameworkCore.Modeling; + +namespace CloudCrm.OrderingService.Data; + +[ConnectionStringName(DatabaseName)] +public class OrderingServiceDbContext : + AbpDbContext, + IHasEventInbox, + IHasEventOutbox +{ + public const string DbTablePrefix = ""; + public const string DbSchema = null; + + public const string DatabaseName = "OrderingService"; + + public DbSet IncomingEvents { get; set; } + public DbSet OutgoingEvents { get; set; } + public DbSet Orders { get; set; } // NEW: ADD DBSET PROPERTY + + // Code omitted for brevity +} +``` + +Next, we need to configure the database mapping for the `Order` entity. We'll use the [EF Core Fluent API](https://docs.microsoft.com/en-us/ef/core/modeling/relational/tables) for this configuration. In the `OrderingServiceDbContext` class add the following code to the `OnModelCreating` method: + +```csharp +protected override void OnModelCreating(ModelBuilder builder) +{ + base.OnModelCreating(builder); + + builder.ConfigureEventInbox(); + builder.ConfigureEventOutbox(); + + builder.Entity(b => + { + // Configure table name + b.ToTable("Orders"); + + // Always call this method to set base entity properties + b.ConfigureByConvention(); + + // Properties of the entity + b.Property(q => q.CustomerName).IsRequired().HasMaxLength(120); + }); +} +``` + +In this code snippet, we configure the `Order` entity to use the `Orders` table in the database. We also specify that the `CustomerName` property is required and has a maximum length of 120 characters. \ No newline at end of file