EFQueryMapper 1.0.0
See the version list below for details.
dotnet add package EFQueryMapper --version 1.0.0
NuGet\Install-Package EFQueryMapper -Version 1.0.0
<PackageReference Include="EFQueryMapper" Version="1.0.0" />
paket add EFQueryMapper --version 1.0.0
#r "nuget: EFQueryMapper, 1.0.0"
// Install EFQueryMapper as a Cake Addin #addin nuget:?package=EFQueryMapper&version=1.0.0 // Install EFQueryMapper as a Cake Tool #tool nuget:?package=EFQueryMapper&version=1.0.0
EFQueryMapper
EFQueryMapper is a powerful library for seamless type mapping, including support for Entity Framework's IQueryable interface. It allows for efficient mapping between entities and DTOs, streamlining the data transformation process in .NET applications.
Installation
You can install the package via NuGet Package Manager:
Install-Package EFQueryMapper
Or via .NET CLI:
dotnet add package EFQueryMapper
Usage
You can add it to DI this way:
services.AddQueryMapper<BookMapper>()
Your custom mapper must be a subclass of QueryMapper. It is a MUST since QueryMapper itself already covers all the heavy stuff and implements IQueryMapper readily with a flexible way. Let's take a look at an example:
public class BookMapper : QueryMapper
{
public BookMapper()
{
Configure<Person, PersonDTO>(config =>
{
config
.Match(x => x.Firstname + " " + x.Lastname, dto => dto.Fullname)
.UsingConstructor(x => new PersonDTO(x.Firstname, x.Lastname))
;
});
Configure<Book, ReadBookResponse>(config =>
{
config
.Match(x => x.Author.FirstName + " " + x.Author.LastName, y => y.AuthorName)
.Match(x => x.CreatedBy.FirstName + " " + x.CreatedBy.LastName, y => y.CreatedByName)
;
});
Configure<Note, ReadNoteResponse>(config =>
{
config
.Match(x => x.User.FirstName + " " + x.User.LastName, y => y.UserName)
.Match(x => x.User.ShareId, y => y.ShareId)
;
});
}
}
You see we have "Configure" method. It has a couple methods to extend mapping abilities:
UsingConstructor: Allows you to use the constructor you wish. If destination class has multiple constructors and you want to pick a certain one, this method would be useful. If you don't use and map straight, first constructor found will be used. Private constructors are NOT prioritized while picking up constructor automatically.
Lets say you have only one constructor and it has parameters. You don't need to use this method if parameter names are matched to related property/field. Imagine you have only this constructor:
private PersonDTO(string firstname)
{
Firstname = firstname;
}
Even if it is private, it is easily used with mapper. If you have multiple constructors, the ones who are not private have priority to be picked up by mapper. Since member names are matched, you don't have to make any configurations. If namings don't match just use the method.
Example
You can easily use Map method directly with IQueryable interface. Here's a quick example:
IQueryable<Person> peopleQuery = dbcontext.Person.Where(x=> x.Age >= 18);
List<PersonDTO> extensionQueryPeople = peopleQuery.Map<PersonDTO>(mapper).ToList();
Or you can go this way:
List<PersonDTO> value = mapper.Map<Person, PersonDTO>(peopleQuery).ToList();
Features
- Supports Entity Framework IQueryable interface
- Seamless mapping between entities and DTOs
- Optimized for performance with the help of Expression API
License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Microsoft.EntityFrameworkCore (>= 8.0.8)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on EFQueryMapper:
Package | Downloads |
---|---|
QueryBase
This library provides a powerful generic repository base, leveraging the capabilities of Entity Framework and QueryMapper. It simplifies CRUD operations, making them easily extendable. Pagination, filtering, ordering, and bulk operations are all implemented in a generic and flexible way. Most methods can be overridden for customization. |
GitHub repositories
This package is not used by any popular GitHub repositories.