GlitchedPolygons.DataTransferObject
1.0.3
Prefix Reserved
dotnet add package GlitchedPolygons.DataTransferObject --version 1.0.3
NuGet\Install-Package GlitchedPolygons.DataTransferObject -Version 1.0.3
<PackageReference Include="GlitchedPolygons.DataTransferObject" Version="1.0.3" />
<PackageVersion Include="GlitchedPolygons.DataTransferObject" Version="1.0.3" />
<PackageReference Include="GlitchedPolygons.DataTransferObject" />
paket add GlitchedPolygons.DataTransferObject --version 1.0.3
#r "nuget: GlitchedPolygons.DataTransferObject, 1.0.3"
#:package GlitchedPolygons.DataTransferObject@1.0.3
#addin nuget:?package=GlitchedPolygons.DataTransferObject&version=1.0.3
#tool nuget:?package=GlitchedPolygons.DataTransferObject&version=1.0.3
DTOs
Love 'em or hate 'em
We all use them (hopefully)
This is a lightweight, zero-dependency library for C# that allows you to define your mapping logic once and use it for both EF Core SQL projections and high-performance in-memory mapping.
The problem
Usually, you have to choose between:
- Manual Selects: Efficient SQL, but you duplicate code everywhere.
- AutoMapper/Methods: Cleaner code, but often accidentally results in "Select All" queries or expensive runtime compilation.
The solution
This interface here uses C# 11 Static Abstract Members to create a single source of truth for your DTO's base.
- Define your DTO
public class UserDto : IDataTransferObject<User, UserDto>
{
public int Id { get; init; }
public string? FullName { get; init; }
// This is used by EF Core for SQL Generation via ".Select(UserDto.MapExpression)"
public static Expression<Func<User, UserDto>> MapExpression =>
user => new UserDto
{
Id = user.Id,
FullName = user.FullName
};
}
- Use it in EF Core (SQL)
Pass the expression directly into .Select(). Because it's an Expression, EF Core generates optimized SQL, fetching only the required columns.
IReadOnlyCollection<UserDto> users =
await
databaseContext
.Users
.Select(UserDto.MapExpression)
.ToListAsync()
;
- Use it In-Memory (C#)
When you already have an entity in memory, you can use the Map helper.
The library automatically compiles and caches the expression into a high-speed delegate the first time it is used.
User userEntity = GetUserFromCache();
// This uses the cached delegate (no reflection, no re-compilation).
UserDto dto = Map<User>.To<UserDto>(userEntity);
Requirements
- .NET 10+
- C# 11+
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.