Skip to content

Client module

The Spares service provides a Client Module for other services that need to integrate with Spare, can do it easily.

The SparesClientModule provides a materialized representation of a Spare, the SpareMetaData. Also, it brings to the table an internal API that maintains up to date the Spare Metadata with the changes made on the Spares Service.

Currently, the SpareMetaData includes:

  • Spare correlation id.
  • Spare display name.
  • Spare code.
  • Maker display name.
  • Maker reference.
  • Spare price.
  • In stock quantity.
  • Unit of Measure.

How to use it

The main idea behind the SparesClientModule is that, when you create your own entity (i.e Component), you can add relations to SpareMetaData, which is provided by the module.

The DB structure is included as your own service's migrations, and it is handled as another regular entity. The only difference is that we can share the class and it's mapping so that you don't need to write them.

Defining an Entity with a SpareMetaData

First we need to reference the SparesClientModule's Abstraction assembly from our Domain layer.

XML
<ProjectReference Include="$(ServicesPath)Spares/ITsynch.Suite.Spares.Abstractions/ITsynch.Suite.Spares.Abstractions.csproj" />

This will allow us to create an entity which has a Spare:

C#
using ITsynch.Suite.Spares.Domain;

namespace ITsynch.Suite.Components.Domain
{
    public class Component : BaseSagaStateMachineInstance
    {
        // [..]
        public SpareMetaData Spare { get; private set; } = null!;
    }
}

We then need to add an EntityTypeConfiguration for this entity on our Persistence layer as usual:

C#
namespace ITsynch.Suite.Components.Persistence
{
    internal class ComponentConfiguration
        : IEntityTypeConfiguration<ComponentsDbContext, Component>
    {
        public void Configure(EntityTypeBuilder<Component> builder)
        {
            // [..]
            builder.HasOne(x => x.Spare)
                .WithMany()
                .IsRequired()
                .OnDelete(DeleteBehavior.NoAction);
        }
    }
}

After doing that, we must reference and configure the SparesClientModule on our Application Module before we can generate migrations.

Configuring the Application Module

First, we need to reference the SparesClientModule's assembly from in our Application layer project:

XML
<ProjectReference
    Include="$(ServicesPath)Spares/ITsynch.Suite.Spares.ClientModule/ITsynch.Suite.Spares.ClientModule.csproj" />

Then we need to depend on the SparesClientModule and also configure the DbContext where we want the Spare Metadata entity added to. This is usually the only DbContext that you have, which is the one generated by the microservice template:

C#
public class MyServiceApplicationModule : SuiteAspNetApplicationModule {

    public override void SetupModule(IModuleBuilder builder)
    {
        builder.DependsOn<SparesClientModule, SparesClientModuleOptions>(
            opts =>
            {
                opts.SetDbContext<ComponentsDbContext>();
            });
    }
}

This is all that you need in order for the module to register the EntityTypeConfiguration for SpareMetaData against the DbContext you've just specified.

We can now generate migrations for our service as usual, and we should see the one to one relation we just added, and the table for SpareMetaData being created.