Jac*_*kal 8 c# if-statement asp.net-core
我有多个验证布尔属性,可以需要也可以不需要。如果需要它们,则需要对其进行检查以进行验证。因此我不得不构建多个 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;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑
制作此代码时出现问题。目的是验证所有选项,它们必须是必要的,如果只有一个属性满足条件,则无法返回。
谢谢大家,我会在评论中尝试一些建议的方法,然后我会发布结果
更新
我现在有一个简短的版本,根据每个人的评论更具可读性,直到我可以尝试每一种方法。根据@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;
}
Run Code Online (Sandbox Code Playgroud)
该方法的正确实现应该如下所示:
public bool IsProductionReadyToStart()
{
bool isValid = true;
isValid &= !IsComponentsRequired || ValidatedComponents;
isValid &= !IsGeometriesRequired || ValidatedGeometries;
isValid &= !IsPokayokesRequired || ValidatedPokayokes;
isValid &= !IsTechnicalFileRequired || ValidatedTechnicalFile;
isValid &= !IsStandardOperationSheetRequired || ValidatedStandardOperationSheet;
isValid &= !IsOperationMethodRequired || ValidatedOperationMethod;
return isValid;
}
Run Code Online (Sandbox Code Playgroud)
当不使用 &= 时,您将删除您检查过的所有先前结果,而不是将它们合并。
它看起来像一个集合
public class Validation
{
public bool Required { get; set; }
public bool IsValid { get; set; }
}
var validations = new[]
{
new Validation { Required = true, IsValid = true },
new Validation { Required = false, IsValid = true },
new Validation { Required = true, IsValid = false },
};
// return true only when all required validations are valid
public bool IsProductionReadyToStart()
{
return _validations.Where(v => v.Required).All(v => v.IsValid);
}
Run Code Online (Sandbox Code Playgroud)
我会去:
if (IsComponentsRequired && !ValidateComponents) return false;
if (IsGeometriesRequired && !ValidatedGeometries) return false;
...
return true;
Run Code Online (Sandbox Code Playgroud)
这更像是一个清单。
您可以将您的条件累积到ValueTuple集合中,然后一起检查
var conditions = new[]
{
(IsComponentsRequired, ValidatedComponents),
(IsGeometriesRequired, ValidatedGeometries),
(IsPokayokesRequired, ValidatedPokayokes)
};
return conditions.Where(c => c.Item1).All(c => c.Item2);
Run Code Online (Sandbox Code Playgroud)
您还可以使用命名元组语法来提高可读性
var conditions = new (bool isRequired, bool validated)[]
{
(IsComponentsRequired, ValidatedComponents),
(IsGeometriesRequired, ValidatedGeometries),
(IsPokayokesRequired, ValidatedPokayokes)
};
return conditions.Where(c => c.isRequired).All(c => c.validated);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
361 次 |
| 最近记录: |