Mapper example
Service can be used as a simple mapper.
// Create car factory
Action<IQuery<CarRequest, Car>> carFactory = query =>
{
if (query.Handled() || query.Request.License is not string license) return;
query.Response.SetValue(new Car(license));
};
// Create service
IService service = Services.Create(carFactory);
// Type-cast
IService<CarRequest, Car> carService = service.Cast<CarRequest, Car>();
// Get-or-create car record
Car carRecord1 = carService.GetRequired("DKG-313");
// Assign owner
carRecord1.Owner = "Aku";
// Get record again from cache
Car carRecord = carService.GetRequired("DKG-313");
// Print record
Console.WriteLine(carRecord);
// Car { Register = DKG-313, Owner = Aku }
/// <summary>Car record</summary>
public record struct CarRequest([Request] object License) : IRequestFor<Car>, IRequestToBeCached
{
public static implicit operator CarRequest(string license) => new CarRequest(license);
}
/// <summary>Car record</summary>
public sealed record Car
{
/// <summary>Register license number, immutable</summary>
[Key]
public string License { get; init; }
/// <summary>Owner of the car, mutable</summary>
public string? Owner { get; set; }
/// <summary>Create car for <paramref name="license"/>.</summary>
public Car(string license) => License = license;
}
Entry be disconnected from cache.
// Disconnect car record from cache
bool ok = (carService as IEntryProvider).GetEntryCache().TryDisconnect(new CarRequest("DKG-313"));
Full Example
Full example
using System;
using System.ComponentModel.DataAnnotations;
using Avalanche.Service;
public class example_mapper
{
public static void Run()
{
// <01>
// Create car factory
Action<IQuery<CarRequest, Car>> carFactory = query =>
{
if (query.Handled() || query.Request.License is not string license) return;
query.Response.SetValue(new Car(license));
};
// Create service
IService service = Services.Create(carFactory);
// Type-cast
IService<CarRequest, Car> carService = service.Cast<CarRequest, Car>();
// Get-or-create car record
Car carRecord1 = carService.GetRequired("DKG-313");
// Assign owner
carRecord1.Owner = "Aku";
// Get record again from cache
Car carRecord = carService.GetRequired("DKG-313");
// Print record
Console.WriteLine(carRecord);
// Car { Register = DKG-313, Owner = Aku }
// </01>
// <04>
// Disconnect car record from cache
bool ok = (carService as IEntryProvider).GetEntryCache().TryDisconnect(new CarRequest("DKG-313"));
// </04>
}
// <02>
/// <summary>Car record</summary>
public record struct CarRequest([Request] object License) : IRequestFor<Car>, IRequestToBeCached
{
public static implicit operator CarRequest(string license) => new CarRequest(license);
}
// </02>
// <03>
/// <summary>Car record</summary>
public sealed record Car
{
/// <summary>Register license number, immutable</summary>
[Key]
public string License { get; init; }
/// <summary>Owner of the car, mutable</summary>
public string? Owner { get; set; }
/// <summary>Create car for <paramref name="license"/>.</summary>
public Car(string license) => License = license;
}
// </03>
}