Specifications
Installation
dotnet add package Structr.SpecificationsUsage
// Some model.
public class Book
{
public string Title { get; set; }
public string Author { get; set; }
public int Year { get; set; }
public int Price { get; set; }
}
// Specification matching only books written after some year.
public class BookYearIsGreaterThanSpec : Specification<Book>
{
public int Year { get; }
public BookYearIsGreaterThanSpec(int year)
=> Year = year;
// The only method to be overridden - ToExpression().
// It should return expression that gives needed condition.
public override Expression<Func<Book, bool>> ToExpression()
{
return x => x.Year > Year;
}
}
// Specification matching only books which have price less than some value.
public class BookPriceIsLessThanSpec : Specification<Book>
{
public int Price { get; }
public BookPriceIsLessThanSpec(int price)
=> Price = price;
public override Expression<Func<Book, bool>> ToExpression()
{
return x => x.Price < Price;
}
}Method name
Description
Name
Description
Entity Framework
Last updated