Part of DKNet.EfCore.Specifications. Use the Quick Start with services.AddSingleton<MapsterMapper.IMapper>(new MapsterMapper.Mapper()); before building the provider. Add the model and specification below, then run the projected query:
// Place these statements before the type declarations in Program.cs.
var models = await repo.ToListAsync<Product, ProductSummary>(new SummarySpec());
Console.WriteLine(models.Single().Name); // Coffee
public sealed class ProductSummary
{
public string Name { get; set; } = "";
}
public sealed class SummarySpec : ModelSpecification<Product, ProductSummary>
{
public SummarySpec()
{
WithFilter(p => p.Price >= 10);
AddOrderBy(p => p.Id);
}
}
ModelSpecification<TEntity, TModel> adds the IModelSpecification<TEntity, TModel> contract and supports the same builders and copy construction as the entity specification. ModelSpecRepoExtensions provides projected First, FirstOrDefault, list, paged-list and buffered enumeration helpers. Query<TEntity, TModel> also accepts an entity specification for a deferred projected query.
DtoGenerator can supply the target type; it does not supply the mapper or its mapping configuration.