Query Projection with ProjectTo
Optimizing Database Queries and Entity-to-DTO Projections using ProjectTo in AUA Services
Models and Mapping Efficiency
Mapping complex software system entities into Data Transfer Objects (DTOs) or custom View Models is a fundamental practice. The AUA framework streamlines this architecture by allowing developers to leverage conversion functions and fetch DTOs directly from the database using powerful mechanisms like ProjectTo right inside services.
Direct Database Projection
Within AUA services, you can seamlessly utilize ProjectTo to transform database query results straight into target structures, optimizing execution performance and memory footprint.
Integrated Mapping Suite
Working hand-in-hand with IMapFrom and IHaveCustomMappings, ProjectTo provides a unified, robust framework ecosystem for handling simple to complex field and collection mappings.
3. Executing Projections in Service Layer (ConvertTo & ProjectTo)
public class AppUserService : GenericEntityService<AppUser, AppUserDto>, IAppUserService
{
public AppUserService(IUnitOfWork unitOfWork) : base(unitOfWork) { }
// Example 1: ConvertTo Execution
public IEnumerable<TestMappingVm> GetTestMappingVmsWithConvertTo()
{
var query = GetAll().Where(p => p.IsActive);
return query
.ConvertTo<TestMappingVm>(MapperInstance)
.ToList();
}
// Example 2: Direct Database Projection using ProjectTo
public IEnumerable<TestMappingVm> GetTestMappingVmsWithProjectTo()
{
var query = GetAll().Where(p => p.IsActive);
return MapperInstance
.ProjectTo<TestMappingVm>(query)
.ToList();
}
}
"If you think good architecture is expensive, try bad architecture."AUA Framework Philosophy