A lightweight library for rewriting Expression<Func<TDestination, bool>> predicates into Expression<Func<TSource, bool>> predicates. Generates SQL-translatable expressions for use where you want to expose generic higher-order functions without leaking your internal persistence model.
Define a mapper for each source/destination pair by subclassing EntityMapper<TSource, TDestination> and implementing Configure():
public class UserMapper : EntityMapper<UserEntity, UserDto>
{
protected override void Configure()
{
Map(src => src.UserId, dst => dst.Id);
Map(src => src.FullName, dst => dst.Name);
Map(src => src.IsEnabled, dst => dst.IsActive);
}
}Use it in your repository:
public IEnumerable<UserDto> Find(Expression<Func<UserDto, bool>> predicate)
{
var mapper = new UserMapper();
var mappedPredicate = mapper.Map(predicate);
return _dbSet.Where(mappedPredicate).Select(src => MapToDto(src));
}When a property is itself a mapped type, pass a mapper instance for the nested type:
public class UserMapper : EntityMapper<UserEntity, UserDto>
{
protected override void Configure()
{
Map(src => src.UserId, dst => dst.Id);
Map(src => src.FullName, dst => dst.Name);
Map(src => src.Address, dst => dst.Address, new AddressMapper());
Map(src => src.OrderEntities, dst => dst.Orders, new OrderMapper());
}
}For self-referential types, pass a factory to defer construction:
public class CategoryMapper : EntityMapper<CategoryEntity, CategoryDto>
{
protected override void Configure()
{
Map(src => src.Id, dst => dst.Id);
Map(src => src.Name, dst => dst.Name);
Map(src => src.Children, dst => dst.Children, () => this);
}
}These are the expression node types the library rewrites. All are verified to produce valid SQL via EF Core.
| Pattern | Example |
|---|---|
| Member access equality | dst.Id == id |
| Comparison operators | dst.Age > 18 |
Boolean binary (&&, ||) |
dst.IsActive && dst.Age > 18 |
| Unary negation | !dst.IsActive |
| Null checks | dst.Address != null |
| Navigation property chaining | dst.Address.City == "London" |
| String methods | dst.Name.StartsWith("A") |
Collection: Any |
dst.Orders.Any(o => o.Total > 100) |
Collection: All |
dst.Orders.All(o => o.IsPaid) |
Collection: Contains |
dst.Tags.Contains("vip") |
| Captured primitive closures | dst.Id == capturedVar |
All destination members must be mapped. The library validates this at construction time — misconfigured mappers throw InvalidMappingException before any predicate is ever rewritten:
// Throws InvalidMappingException: unmapped destination members: Name, IsActive
public class IncompleteMapper : EntityMapper<UserEntity, UserDto>
{
protected override void Configure()
{
Map(src => src.UserId, dst => dst.Id);
// Name and IsActive not mapped — caught immediately on instantiation
}
}Unmapped source members are safe to omit — they will never appear in a destination expression.
UnsupportedExpressionException at map time
- Composite source expressions (e.g. mapping
src.First + " " + src.Lasttodst.FullName) are not supported in v1 — both sides of a mapping must be simple member access - Closures over complex captured objects should be tested against your ORM — captured primitives and local collections are handled correctly
This library sits inside repository implementations, invisible to consumers. The service layer works exclusively with DTO expressions. The repository is the only location that knows a persistence model exists.
Service Layer → Expression<Func<UserDto, bool>>
Repository → EntityMapper rewrites to Expression<Func<UserEntity, bool>>
ORM / Database → SQL