Оптимизация проверки аналогичных свойствC#

Место общения программистов C#
Anonymous
Оптимизация проверки аналогичных свойств

Сообщение Anonymous »

У меня есть следующая команда для создания экземпляра модели предметной области:

Код: Выделить всё

public class CreateCPUCommand : IRequest
{
public int Article { get; set; }
public int Garanty { get; set; }
public int ServiceLife { get; set; }
public Model Model { get; set; }
public Socket Socket { get; set; }
public int GeneralCoresCount { get; set; }
public int HighPerformanceCoresCount { get; set; }
public int EnergyEfficientCoresCount  { get; set; }
public int StreamsCount { get; set; }
public int BaseFrequency { get; set; }
public int MaxFrequencyInTurbo { get; set; }
public bool HasFreeMultiplier { get; set; }
public RAMType RAMType { get; set; }
public int RAMMaxSize  { get; set; }
public int MaxTemperature { get; set; }
public bool HasGraphicCore { get; set; }
public int Price { get; set; }
}
Я написал для нее следующий валидатор, используя FluentValidation:

Код: Выделить всё

public class CreateCPUCommandValidator : AbstractValidator
{
public CreateCPUCommandValidator()
{
RuleFor(createCpuCommand => createCpuCommand.GeneralCoresCount)
.Equal(createCpuCommand =>
createCpuCommand.EnergyEfficientCoresCount + createCpuCommand.HighPerformanceCoresCount);
RuleFor(createCpuCommand => createCpuCommand.Garanty)
.GreaterThan(0);
RuleFor(createCpuCommand => createCpuCommand.ServiceLife)
.GreaterThan(0);
RuleFor(createCpuCommand => createCpuCommand.GeneralCoresCount)
.GreaterThan(0);
RuleFor(createCpuCommand => createCpuCommand.HighPerformanceCoresCount)
.GreaterThan(0);
RuleFor(createCpuCommand => createCpuCommand.EnergyEfficientCoresCount)
.GreaterThan(0);
RuleFor(createCpuCommand => createCpuCommand.StreamsCount)
.GreaterThan(0);
RuleFor(createCpuCommand => createCpuCommand.BaseFrequency)
.GreaterThan(0);
RuleFor(createCpuCommand => createCpuCommand.MaxFrequencyInTurbo)
.GreaterThan(0);
RuleFor(createCpuCommand => createCpuCommand.RAMMaxSize)
.GreaterThan(0);
RuleFor(createCpuCommand => createCpuCommand.MaxTemperature)
.GreaterThan(0);
RuleFor(createCpuCommand => createCpuCommand.Price)
.GreaterThan(0);
}
}
Как видите, существует правило, согласно которому все свойства типа int должны быть больше 0. Но я не могу избавиться от ощущения, что должен быть лучший способ сделать это, чем писать RuleFor для каждого свойства. Я пробовал читать про PropertyValidator и методы Must и Custom, но кажется, что они мне не совсем помогают решить задачу, или у меня не хватает мозгов, чтобы правильно применить эти инструменты. Как я могу лучше реализовать эти правила?

Подробнее здесь: https://stackoverflow.com/questions/798 ... properties

Вернуться в «C#»