I have multiple validation boolean properties which can be required or not. If they are required they need to be checked for validation purposes. Therefore I had to build multiple if statements to handle each property. My question is if is there a better way to maintain this instead of having to write if for every new property.
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
There was a problem while making this code. The intended is to validate all options, they must be necessary, it cannot return if just one property meets the condition.
Thanks everyone, I will try some of the suggested approaches on the comments and I'll post the results after
UPDATE
I have come with a short version for now and more readable based on everyones comments until I can try every approach. Edited to combine all expressions as per @Alexander Powolozki answer.
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;
}