Browse Source

Add part-05 of microservice tutorial covering the implementation of the Ordering module and database configuration

pull/21456/head
ahmetfarukulu 2 years ago
parent
commit
7db0121d11
  1. BIN
      docs/en/tutorials/microservice/images/vs-ordering-entity.png
  2. 2
      docs/en/tutorials/microservice/part-03.md
  3. 113
      docs/en/tutorials/microservice/part-05.md

BIN
docs/en/tutorials/microservice/images/vs-ordering-entity.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

2
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);
}

113
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<Guid>
{
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<OrderingServiceDbContext>,
IHasEventInbox,
IHasEventOutbox
{
public const string DbTablePrefix = "";
public const string DbSchema = null;
public const string DatabaseName = "OrderingService";
public DbSet<IncomingEventRecord> IncomingEvents { get; set; }
public DbSet<OutgoingEventRecord> OutgoingEvents { get; set; }
public DbSet<Order> 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<Order>(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.
Loading…
Cancel
Save