У меня есть несколько логических свойств проверки, которые могут потребоваться или нет. Если они необходимы, их необходимо проверить для целей валидации. Поэтому мне пришлось создать несколько операторов if для обработки каждого свойства. Мой вопрос: есть ли лучший способ сохранить это, вместо того, чтобы писать if для каждого нового свойства.
public class ProductionNavbarViewModel
{
public bool IsProductionActive { get; set; }
public bool ValidatedComponents { get; set; }
public bool ValidatedGeometries { get; set; }
public bool ValidatedPokayokes { get; set; }
public bool ValidatedTechnicalFile { get; set; }
public bool ValidatedStandardOperationSheet { get; set; }
public bool ValidatedOperationMethod { get; set; }
public bool IsComponentsRequired { get; set; }
public bool IsGeometriesRequired { get; set; }
public bool IsPokayokesRequired { get; set; }
public bool IsTechnicalFileRequired { get; set; }
public bool IsStandardOperationSheetRequired { get; set; }
public bool IsOperationMethodRequired { get; set; }
public bool IsProductionReadyToStart()
{
if (IsComponentsRequired)
{
return ValidatedComponents;
}
if (IsComponentsRequired && IsGeometriesRequired)
{
return ValidatedComponents && ValidatedGeometries;
}
if (IsComponentsRequired && IsGeometriesRequired && IsPokayokesRequired)
{
return ValidatedComponents && ValidatedGeometries && ValidatedPokayokes;
}
if (IsComponentsRequired && IsGeometriesRequired && IsPokayokesRequired && IsTechnicalFileRequired)
{
return ValidatedComponents && ValidatedGeometries && ValidatedPokayokes && ValidatedTechnicalFile;
}
if (IsComponentsRequired && IsGeometriesRequired && IsPokayokesRequired && IsTechnicalFileRequired && ValidatedStandardOperationSheet)
{
return ValidatedComponents && ValidatedGeometries && ValidatedPokayokes && ValidatedTechnicalFile && ValidatedStandardOperationSheet;
}
if (IsComponentsRequired && IsGeometriesRequired && IsPokayokesRequired && IsTechnicalFileRequired && IsStandardOperationSheetRequired && IsOperationMethodRequired)
{
return ValidatedComponents && ValidatedGeometries && ValidatedPokayokes && ValidatedTechnicalFile && ValidatedStandardOperationSheet && ValidatedOperationMethod;
}
return false;
}
}
EDIT
Была проблема во время создание этого кода. Он предназначен для проверки всех параметров, они должны быть необходимы, он не может быть возвращен, если только одно свойство соответствует условию.
Спасибо всем, я попробую некоторые из предложенных подходов в комментариях и опубликую результаты после
ОБНОВЛЕНИЕ
Я пришел с короткой версией на данный момент и более удобочитаемой на основе всех комментариев, пока я не смогу попробовать все подходы. Отредактировано для объединения всех выражений в соответствии с ответом @Alexander Powolozki.
public bool IsProductionReadyToStart()
{
bool isValid = true;
isValid &= !IsComponentsRequired || ValidatedComponents;
isValid &= !IsGeometriesRequired || ValidatedGeometries;
isValid &= !IsPokayokesRequired || ValidatedComponents;
isValid &= !IsTechnicalFileRequired || ValidatedTechnicalFile;
isValid &= !IsStandardOperationSheetRequired || ValidatedStandardOperationSheet;
isValid &= !IsOperationMethodRequired || ValidatedOperationMethod;
return isValid;
}