Bongia.Generators
8.0.3-alpha
dotnet add package Bongia.Generators --version 8.0.3-alpha
NuGet\Install-Package Bongia.Generators -Version 8.0.3-alpha
<PackageReference Include="Bongia.Generators" Version="8.0.3-alpha" />
<PackageVersion Include="Bongia.Generators" Version="8.0.3-alpha" />
<PackageReference Include="Bongia.Generators" />
paket add Bongia.Generators --version 8.0.3-alpha
#r "nuget: Bongia.Generators, 8.0.3-alpha"
#:package Bongia.Generators@8.0.3-alpha
#addin nuget:?package=Bongia.Generators&version=8.0.3-alpha&prerelease
#tool nuget:?package=Bongia.Generators&version=8.0.3-alpha&prerelease
Bongia.Generators
Bongia.Generators is a Roslyn source generator that automatically creates projection classes and mapping expressions between a source type and one or more target types.
For example, you can easily map from an entity to a DTO, but the library itself is generic and works with any types.
How it works
By decorating your source classes with the [GenerateProjection<TTarget>] attribute, Bongia.Generators will:
Generate target properties automatically
Every property from the source type is copied to the target type, unless explicitly excluded with[NotProjected]or[NotProjected<TTarget>].Generate projection methods
For each[GenerateProjection<TTarget>], a static method is added to the source type, e.g.:GetUserDtoProjection()GetExtendedUserDtoProjection()
These methods return an
Expression<Func<TSource, TTarget>>(wrapped in aSpecification<TSource, TTarget>), ready to be used in LINQ queries or frameworks like EF Core.Support custom properties
You can extend target types with computed properties by providing generator methods.
The generator methods must bepublic staticand return aSpecification<TSource, TValue>.
During projection, Bongia.Generators will call these methods and populate the additional properties.Provide a LINQ extension method
Bongia.Generators adds an extension methodProjectTo<TTarget>()onIQueryable<TSource>.- It searches for a generated projection method from
TSourcetoTTarget. - If found, it applies a
.Select(...)internally and returnsIQueryable<TTarget>. - If not found, it throws a runtime error.
- It searches for a generated projection method from
Example
[GenerateProjection<UserDto>]
[GenerateProjection<ExtendedUserDto>(
"FullName", nameof(GetFullName)
)]
internal sealed partial class User
{
[NotProjected]
public string? Password { get; set; }
[NotProjected<UserDto>]
public string FirstName { get; set; } = null!;
[NotProjected<UserDto>]
public string LastName { get; set; } = null!;
public Guid Id { get; set; }
public string Username { get; set; } = null!;
// Custom property generator for ExtendedUserDto
public static Specification<User, string> GetFullName() =>
new(user => $"{user.FirstName} {user.LastName}");
}
internal sealed partial class UserDto;
internal sealed partial class ExtendedUserDto;
What gets generated
UserDto (target type)
Will contain all properties fromUserexcept those marked with[NotProjected<UserDto>]or[NotProjected].ExtendedUserDto (target type)
Will contain all the allowed properties plus the extraFullNameproperty, populated using theGetFullNamemethod.Projection methods on
User(source type)public static Expression<Func<User, UserDto>> GetUserDtoProjection(); public static Expression<Func<User, ExtendedUserDto>> GetExtendedUserDtoProjection();Queryable extension method
IQueryable<UserDto> dtos = dbContext.Users.ProjectTo<UserDto>();If the projection method exists, the query is translated. Otherwise, a runtime exception is thrown.
Excluding properties
[NotProjected]→ Excludes the property from all projections.[NotProjected<TTarget>]→ Excludes the property only for the given target type.
Adding custom properties
To add extended properties to a target type:
Pass pairs of arguments to the
[GenerateProjection]attribute:[GenerateProjection<ExtendedUserDto>( "FullName", nameof(GetFullName), "OtherProp", nameof(GetOtherProp) )]Implement a static generator method for each property:
public static Specification<User, string> GetOtherProp() => new(user => user.Username.ToUpper());The target type will get a new property:
public string FullName { get; set; } public string OtherProp { get; set; }
Requirements
- Source and target types must be declared as
partial. - Generator methods must be
public staticand return aSpecification<TSource, TValue>.
Visual Overview
Source Type (User)
|
| [GenerateProjection<UserDto>]
| [GenerateProjection<ExtendedUserDto> + custom generators]
v
Projection Methods Generated
|
v
Target Types (UserDto, ExtendedUserDto)
|
| IQueryable<TTarget> ProjectTo<TTarget>()
v
LINQ Queryable results
Why use Bongia.Generators?
- Eliminate repetitive mapping code.
- Get compile-time safety for projections.
- Generate LINQ- and EF Core–friendly expressions automatically.
- Keep target types clean and partial, only defining what really matters.
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- Microsoft.CodeAnalysis.Common (>= 4.11.0)
- Microsoft.CodeAnalysis.CSharp (>= 4.11.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 8.0.3-alpha | 168 | 9/9/2025 |
| 8.0.2-alpha | 154 | 9/8/2025 |
| 8.0.1-alpha | 152 | 9/8/2025 |