Код: Выделить всё
public abstract class BaseItem
{
// Base class implementation...
}
public class FooItem : BaseItem
{
// Derived Class A implementation...
}
public class BarItem : BaseItem
{
// Derived Class B implementation...
}
Код: Выделить всё
public class BaseItemValidator : AbstractValidator
{
public BaseItemValidator()
{
// Rules for Base Item
}
}
public class FooItemValidator : AbstractValidator
{
public FooItemValidator()
{
Include(new BaseItemValidator());
RuleSet("FooRules", () =>
{
// Rules for derived FooItem instances
})
}
}
public class BarItemValidator : AbstractValidator
{
public BarItemValidator()
{
Include(new BaseItemValidator());
}
}
Код: Выделить всё
public class BarItemService
{
private IValidator _validator;
public BarItemService(IValidator validator)
{
_validator = validator;
}
public async Task AddItem(T item, CancellationToken token = default)
{
var result = await _validator.ValidateAsync(item, opt => opt.IncludeRuleSets("FooRules"), token);
if (!result.IsValid)
{
// Indicate the item is not valid
}
// Rest of the method...
}
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... le-set-tha