AlephMapper 0.1.4
See the version list below for details.
dotnet add package AlephMapper --version 0.1.4
NuGet\Install-Package AlephMapper -Version 0.1.4
<PackageReference Include="AlephMapper" Version="0.1.4" />
<PackageVersion Include="AlephMapper" Version="0.1.4" />
<PackageReference Include="AlephMapper" />
paket add AlephMapper --version 0.1.4
#r "nuget: AlephMapper, 0.1.4"
#:package AlephMapper@0.1.4
#addin nuget:?package=AlephMapper&version=0.1.4
#tool nuget:?package=AlephMapper&version=0.1.4
Terms of use<sup>?</sup>
By using this project or its source code, for any purpose and in any shape or form, you grant your implicit agreement to all the following statements:
- You condemn Russia and its military aggression against Ukraine
- You recognize that Russia is an occupant that unlawfully invaded a sovereign state
- You support Ukraine's territorial integrity, including its claims over temporarily occupied territories of Crimea and Donbas
- You reject false narratives perpetuated by Russian state propaganda
To learn more about the war and how you can help, click here. Glory to Ukraine! πΊπ¦
AlephMapper
AlephMapper is a Source Generator that automatically creates projectable companion methods from your mapping logic. It enables you to write mapping methods once and use them both for in-memory objects and as expression trees for database queries (Entity Framework Core projections).
π Features
- Expression Tree Generation - Automatically converts method bodies to expression trees
- Null-Conditional Operator Support - Configurable handling of
?.operators - Updateable Methods - Generate update methods that modify existing instances (now is in a very early development stage)
π¦ Installation
dotnet add package AlephMapper
πββοΈ Quick Start
1. Mark your mapping class or method with [Expressive]. Please ensure that your class is static and partial.
[Expressive]
public static partial class PersonMapper
{
public static PersonDto MapToPerson(Employee employee) => new PersonDto
{
Id = employee.EmployeeId,
FullName = GetFullName(employee),
Email = employee.ContactInfo.Email,
Age = CalculateAge(employee.BirthDate),
Department = employee.Department?.Name ?? "Unknown"
};
public static string GetFullName(Employee employee) =>
$"{employee.FirstName} {employee.LastName}";
public static int CalculateAge(DateTime birthDate) =>
DateTime.Now.Year - birthDate.Year;
}
2. Use generated expression methods in Entity Framework queries
// The source generator creates PersonMapper.MapToPersonExpression() automatically
var personDtos = await dbContext.Employees
.Select(PersonMapper.MapToPersonExpression())
.ToListAsync();
3. Use the original methods for in-memory operations
var employee = GetEmployee();
var personDto = PersonMapper.MapToPerson(employee);
var fullName = PersonMapper.GetFullName(employee);
π§ Advanced Features
Null-Conditional Operator Handling
AlephMapper provides flexible handling of null-conditional operators (?.):
[Expressive(NullConditionalRewrite = NullConditionalRewrite.Rewrite)]
public static partial class SafeMapper
{
// This method uses null-conditional operators
public static PersonSummary GetSummary(Person person) => new PersonSummary
{
Name = person.Name,
Age = person.BirthInfo?.Age,
City = person.BirthInfo?.Address?.City,
HasAddress = person.BirthInfo?.Address != null
};
}
Rewrite Options:
None- Don't allow null-conditional operators (throws compile error)Ignore- Remove null-conditional operators (may cause NullReferenceException)Rewrite- Convert to explicit null checks:person.BirthInfo?.Agebecomesperson.BirthInfo != null ? person.BirthInfo.Age : null
Updateable Methods
Generate update methods that modify existing instances instead of creating new ones:
[Expressive]
public static partial class PersonMapper
{
[Updateable]
public static PersonDto UpdatePerson(Employee employee) => new PersonDto
{
Id = employee.EmployeeId,
FullName = GetFullName(employee),
Email = employee.ContactInfo.Email
};
}
// Usage:
var existingDto = new PersonDto();
var employee = GetEmployee();
// Generated method signature: UpdatePerson(Employee source, PersonDto target)
PersonMapper.UpdatePerson(employee, existingDto);
// existingDto is now updated with employee data
π How It Works
AlephMapper uses Roslyn Source Generators to analyze your mapping methods at compile time and generates corresponding expression tree methods.
For each method marked with [Expressive]:
MapToPersonDto(Employee employee)β generatesMapToPersonDtoExpression()returningExpression<Func<Employee, PersonDto>>- Method calls are inlined - Calls to other methods in the same class are automatically inlined into the expression tree
- Null-conditional operators are handled according to your specified rewrite policy
- Update methods generate overloads that accept a target instance to modify
β οΈ Limitations
- Methods must be static and be member of a partial static class
- Supported only simple lambda-bodied or return methods
- Does not have circilar method call detection and protection. Be careful.
- Update methods are WIP and do not support inlining yet
π Migration from Other Mappers
From AutoMapper
// AutoMapper
CreateMap<Employee, PersonDto>()
.ForMember(dest => dest.FullName, opt => opt.MapFrom(src => src.FirstName + " " + src.LastName));
// AlephMapper
[Expressive]
public static partial class PersonMapper
{
public static PersonDto MapToPerson(Employee employee) => new PersonDto
{
FullName = employee.FirstName + " " + employee.LastName,
// ... other mappings
};
}
From Manual Expression Trees
// Manual Expression Trees
Expression<Func<Employee, PersonDto>> expression = e => new PersonDto
{
FullName = e.FirstName + " " + e.LastName
};
// AlephMapper - Same result, but with method reusability
[Expressive]
public static partial class PersonMapper
{
public static PersonDto MapToPerson(Employee employee) => new PersonDto
{
FullName = GetFullName(employee) // This gets inlined in the expression
};
public static string GetFullName(Employee employee) =>
employee.FirstName + " " + employee.LastName;
}
π€ Contributing
We welcome contributions!
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run the test suite
- Submit a pull request
π License
This project is licensed under the MIT License - see the LICENSE file for details.
π Acknowledgments
- Inspired by EntityFrameworkCore.Projectables and Expressionify
- Thanks to all contributors
π Related Projects
- EntityFrameworkCore.Projectables - Similar concept with different approach
- Expressionify - Similar concept with different approach
- AutoMapper - Popular object-to-object mapper
- Mapster - Fast object mapper
Star β this repository if AlephMapper helps you build better applications!
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.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.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.5.2 | 87 | 1/4/2026 |
| 0.5.1 | 88 | 1/4/2026 |
| 0.5.0 | 219 | 12/14/2025 |
| 0.4.4 | 211 | 11/22/2025 |
| 0.4.3 | 307 | 11/21/2025 |
| 0.4.2 | 365 | 11/21/2025 |
| 0.4.1 | 401 | 11/20/2025 |
| 0.4.0 | 164 | 11/15/2025 |
| 0.3.9 | 162 | 11/15/2025 |
| 0.3.8 | 169 | 11/15/2025 |
| 0.3.7 | 186 | 11/15/2025 |
| 0.3.6 | 285 | 11/11/2025 |
| 0.3.5 | 146 | 11/8/2025 |
| 0.3.4 | 138 | 11/8/2025 |
| 0.3.3 | 145 | 11/8/2025 |
| 0.3.2 | 202 | 11/2/2025 |
| 0.3.1 | 193 | 11/2/2025 |
| 0.3.0 | 132 | 11/1/2025 |
| 0.2.9 | 174 | 10/26/2025 |
| 0.2.8 | 183 | 10/26/2025 |
| 0.2.7 | 144 | 10/25/2025 |
| 0.2.6 | 190 | 10/23/2025 |
| 0.2.5 | 178 | 10/23/2025 |
| 0.2.4 | 173 | 10/23/2025 |
| 0.2.3 | 185 | 10/23/2025 |
| 0.2.2 | 189 | 10/21/2025 |
| 0.2.1 | 175 | 10/21/2025 |
| 0.2.0 | 180 | 10/19/2025 |
| 0.1.7 | 187 | 10/19/2025 |
| 0.1.6 | 123 | 10/18/2025 |
| 0.1.5 | 125 | 10/18/2025 |
| 0.1.4 | 131 | 10/11/2025 |
| 0.1.3 | 126 | 10/11/2025 |
| 0.1.2 | 181 | 10/6/2025 |
| 0.1.1 | 184 | 10/6/2025 |
| 0.1.0 | 172 | 10/5/2025 |