c# 使用空检查重构 switch 语句

标签 c# switch-statement refactoring

我只是在处理几个 switch 语句时遇到了一些麻烦,我觉得有更好的方法来实现最终目标。

所以基本上我将 viewmodel 传递给一个方法。该方法首先从数据库中检索 View 模型相关的对象,然后 switch 语句对特定属性进行空检查。基于该结果,另一个 switch 语句对 View 模型进行另一个空检查。在每个点,值都从数据库分配给对象,然后数据库更新发生在最后。

这是代码

        public async Task UpdateContractWithRepository(ViewModel viewModel)
    {
        // Get the contract from db
        Contract contract = GetContract(viewModel.Id);

        switch (viewModel.RepositoryId == null)
        {
            case true:
                switch (contract.RepositoryId == null)
                {
                    case true:
                        // nothing to do
                        // no change
                        break;
                    case false:
                        // Unassign Repository
                        UpdateRepositoryAssignment(contract.RepositoryId, false);

                        // Update properties
                        contract.RepositoryId = null;
                        contract.ConsumedUnits = null;
                        break;
                }
                break;
            case false:
                switch (contract.RepositoryId == null)
                {
                    case true:
                        // assign repository
                        UpdateRepositoryAssignment(viewModel.RepositoryId, true);

                        // Get repository
                        Repository repository = GetRepository(viewModel.RepositoryId);

                        // Update properties
                        contract.RepositoryId = repository.Id;
                        contract.ConsumedUnits = repository.Units;
                        break;
                    case false:
                        // assign repository
                        UpdateRepositoryAssignment(viewModel.RepositoryId, true);

                        // Get repository
                        Repository repository = GetRepository(viewModel.RepositoryId);

                        // Update properties
                        contract.RepositoryId = repository.Id;
                        contract.ConsumedUnits = repository.Units;
                        break;
                }
                break;

        }

        UpdateContract(contract);
    }

我想我可能会取消 switch 语句并改用 if 语句,据我所知,仍然会有一些嵌套。只是想知道是否有人有任何建议。

我看过这里的重构 switch 语句,但它们似乎并没有真正涵盖 null 检查。

感谢任何帮助!

最佳答案

如果你只取出两个 bool 值,整个代码可以简化:

bool IsVMRepoNull = viewModel.RepositoryId == null;
bool IsContractRepoNull = contract.RepositoryId == null;

if(IsVMRepoNull && !IsContractRepoNull )
{
  UpdateRepositoryAssignment(contract.RepositoryId, false);

 // Update properties
  contract.RepositoryId = null;
  contract.ConsumedUnits = null;
}
else if(!IsVMRepoNull)
{
  UpdateRepositoryAssignment(viewModel.RepositoryId, true);

  // Get repository
  Repository repository = GetRepository(viewModel.RepositoryId);

  // Update properties
  contract.RepositoryId = repository.Id;
  contract.ConsumedUnits = repository.Units;
}

关于c# 使用空检查重构 switch 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36643203/

相关文章:

c# - NLog MappedDiagnosticsLogicalContext 不工作在异步/等待与 ConfigureAwait(false)

java - 类似于Java中C#的Process.Close事件?

c# - 仅使用 Linq 简化 foreach 和 Any()

loops - 在React中使用for循环和switch case来动态渲染不同的组件

c++ - Eclipse 的命名空间重构工具?

Git:大规模重构保持更改日志

c# - AnonymousType0#1`6 中的 # 符号是什么意思?

PHP switch 语句位于另一个 switch 语句中

c# - 开关语句

.net - 您对 .NET 使用哪些重构工具?