-
-
Notifications
You must be signed in to change notification settings - Fork 12
In Memory Services inheritance
anton-martyniuk edited this page Oct 26, 2022
·
2 revisions
Modern generic in memory services are flexible, easily changeable and extendable. New methods can be added to service as well as each method can be overriden.
Inherit from the following interfaces:
IModernCrudInMemoryService<TEntityDto, TId>
IModernQueryInMemoryService<TEntityDto, TEntityDbo, TId>
Or inherit from a combined interface:
IModernInMemoryService<TEntityDto, TEntityDbo, TId> : IModernCrudInMemoryService<TEntityDto, TEntityDbo, TId>, IModernQueryInMemoryService<TEntityDto, TEntityDbo, TId>
Lets have a look at example of inheritance from In Memory Service:
public interface IAirplaneInMemoryService : IModernInMemoryService<AirplaneDto, AirplaneDbo, long>
{
}
public class AirplaneInMemoryService : ModernInMemoryService<AirplaneDto, AirplaneDbo, long>, IAirplaneInMemoryService
{
private readonly IMapper _mapper;
public AirplaneInMemoryService(IModernService<AirplaneDto, AirplaneDbo, long> service, IModernServiceCache<AirplaneDto, long> cache,
ILogger<AirplaneInMemoryService> logger, IMapper mapper)
: base(service, cache, logger)
{
_mapper = mapper;
}
// Override GetEntityId
protected override long GetEntityId(AirplaneDto entityDto) => entityDto.Id;
// Override CreateAsync
public override async Task<AirplaneDto> CreateAsync(AirplaneDto entity, CancellationToken cancellationToken = default)
{
var entityDto = _mapper.Map<AirplaneDto>(entity);
entityDto.CreatedAt = DateTime.Now;
return await Repository.CreateAsync(entity, cancellationToken);
}
}