public class Product { public string Name { get; set; } public decimal Price { get; set; } }
public interface ISpecification{ bool IsSatisfiedBy(T item); }
public class PriceGreaterThanSpecification : ISpecification{ public bool IsSatisfiedBy(Product product) { return product.Price > 100; } }
var products = new ListPackage library: There are different package libraries available to implement the Specification pattern in C#, such as Ardalis.Specification or NRules.{ ... } // list of products var spec = new PriceGreaterThanSpecification(); // create the specification var filteredProducts = products.Where(prod => spec.IsSatisfiedBy(prod)); // apply the specification to filter the products