This is a simple library for that focuses on a range of values, in particular struct values.
The class RangeFilter<T>
contains properties To
and From
of type T
. This can be used in models and ValueObjects mostly as request objects where there is need to have data entered with a certain range.
Supposing you have a list of items in a database, each with property DateCreated:
public class Item
{
public Guid Id {get; set;}
public string Name {get; set;}
public DateTime DateCreated {get; set;}
}
A method with to query for items in a range could be written as below;
public IList<Items> GetItems(DateTime from, DateTime to)
{
//write code here...
}
With the RangeFilter however, these two parameters can now be encapsulated in one call as follows;
public IList<Items> GetItems(RangeFilter<DateTime> dateRange)
{
DateTime from = dateRange.From;
DateTime to = dateRange.To;
//more code here...
}
For those interested in having a validaiton on the Range filter, FluentValidation has been used to ensure that the To value is greater than From value.
The validation can be implemented by calling on the RangeFilterValidator method as follows;
public IList<Items> GetItems(RangeFilter<DateTime> dateRange)
{
RangeFilterValidator validator = new RangeFilterValidator<DateTime>();
FluentValidation.Results.ValidationResult validation = validator.Validate(dateRange);
if(!validation.IsValid()){
//deal with the invalid scenario here
}
//otherwise continue here
DateTime from = dateRange.From;
DateTime to = dateRange.To;
//more code here...
}