Full examples see in Integration Tests
Define a projection for a product:
private class ProductProj : Proj<Product, ProductDto>
{
public override Expression<Func<Product, ProductDto>> GetExpression()
{
return p => new ProductDto
{
Id = p.Id,
Name = p.Name,
Price = p.Price
};
}
}
Use it in a LINQ query:
var productsQuery = _context.Products.Select(new ProductProj());
var projectedProducts = await productsQuery.ToArrayAsync();
This project includes integration tests to verify the functionality of projections:
- Enumerable Projection: Test linking two projections for
IEnumerable
. - Queryable Projection: Test linking two projections for
IQueryable
. - Nested Select Projection: Test nested projections within LINQ
Select
calls.
Run the tests using the following command:
dotnet test
private class CategoryProj : Proj<Category, CategoryDto>
{
public override Expression<Func<Category, CategoryDto>> GetExpression()
{
return category => new CategoryDto { Name = category.Name, IsFruit = category.Name == "Fruit" };
}
}
[Fact]
public void QueryableLinkTwoProjectionsByMap()
{
var categoryProj = new CategoryProj();
var productProj = Proj<Product, ProductDto>.Create(p => new ProductDto
{
Id = p.Id,
Name = p.Name,
Price = p.Price,
Category = categoryProj.Map(p.Category)
});
var projectedArray = _context.Products.Select(productProj).ToArray();
Assert.Equivalent(new[]
{
new ProductDto
{
Id = 1,
Name = "Apple",
Price = 10.0m,
Category = new CategoryDto
{
Name = "Fruit",
IsFruit = true
}
},
new ProductDto
{
Id = 2,
Name = "Banana",
Price = 15.0m,
Category = new CategoryDto
{
Name = "Fruit",
IsFruit = true
}
},
new ProductDto
{
Id = 3,
Name = "Cherry",
Price = 8.0m,
Category = new CategoryDto
{
Name = "Fruit",
IsFruit = true
}
}
}, projectedArray);
}
The Proj<T>
class includes built-in debugging support with a custom debugger display, giving developers an immediate view of the underlying expression while debugging.
See also Raffinert.Spec library;
Contributions are welcome! Please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bugfix.
- Submit a pull request for review.
This project is licensed under the MIT License.