Simple strongly typed interface for IQueryable-to-object mappings optimized for Entity Framework without reflection usages.
$ dotnet add package DevTeam.QueryMappingsQuery mappings provide simple strongly typed interface optimized for Entity Framework and IQueryable.
As we use Entity Framework and IQueryable interface for a tons of different projects, we needed some naturally adaptable mapping interface to IQueryable interface. This interface should support possibility to pass arguments for more complicated mapping cases. Also there are cases when we can't have Navigation Property between objects or when we want to write LINQ syntax for queries, so we needed possibility to inject EF Context inside of the mapping.
_mappingsList.Add<Address, AddressModel>(x => new AddressModel
{
Id = x.Id,
BuildingNumber = x.BuildingNumber,
City = x.City,
State = x.State,
Country = (Countries)x.Country,
Street = x.Street,
ZipCode = x.ZipCode
});
_mappingsList.Add<Building, BuildingStatisticsModel, BuildingArguments, IDbContext>(args =>
{
return (query, context) =>
from building in query
join review in context.Set<Review>() on new { EntityId = building.Id, EntityTypeId = (int)EntityType.Building }
equals new { EntityId = review.EntityId, EntityTypeId = review.EntityTypeId }
into reviews
let address = building.Address
select new BuildingStatisticsModel
{
Id = building.Id,
Address = address.BuildingNumber + ", " + address.Street + ", " + address.City,
AppartmentsCount = building.Appartments.Count(),
Size = building.Appartments.Sum(app => app.Size),
ResidentsCount = building.Appartments.SelectMany(app => app.Residents).Where(r => r.Age > args.TargetResidentsAge).Count(),
AverageBuildingRating = reviews.Average(r => r.Rating)
};
});
_mappingsList.Add<Appartment, AppartmentModel, AppartmentsArguments>(args =>
{
return x => new AppartmentModel
{
Id = x.Id,
Badrooms = x.Badrooms,
Bathrooms = x.Bathrooms,
Floor = x.Floor,
IsLodge = x.IsLodge,
Number = x.Number,
Size = x.Size.ToString() + args.UnitOfMeasure
};
});
Strobgly typed interface. If types of your models have changed you will see compile-time errors.
No any reflection is used in runtime. Only one place when reflection is used - start of application.
You can find documentation in the wiki to this repository.
If you found any bug, please submit bug in our [issue tracker] (https://github.com/DevTeamHub/QueryMappings/issues). If you can submit [pull request] with fix (https://github.com/DevTeamHub/QueryMappings/pulls) - even better! And we will be happy to hear different suggestions to improve this project.
Main features in the near future is reusable mappings and object-to-object mappings. You can find roadmap to the project here.
Please reference to quick start in our wiki.