Skip to content

Latest commit

 

History

History
129 lines (104 loc) · 3.32 KB

README.md

File metadata and controls

129 lines (104 loc) · 3.32 KB

Stand With Ukraine

Raffinert.Proj

NuGet version (Raffinert.Proj)

Usage

Full examples see in Integration Tests

Basic Projection Example

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();

Integration Tests

This project includes integration tests to verify the functionality of projections:

  1. Enumerable Projection: Test linking two projections for IEnumerable.
  2. Queryable Projection: Test linking two projections for IQueryable.
  3. Nested Select Projection: Test nested projections within LINQ Select calls.

Run the tests using the following command:

dotnet test

Example 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);
}

Debugging

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;

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your feature or bugfix.
  3. Submit a pull request for review.

License

This project is licensed under the MIT License.