C#重构开关语句与空检查

鹰嘴豆

我确实在使用几个switch语句时遇到了麻烦,并且我觉得有一种更好的方法来实现最终目标。

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

这是代码

        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语句,但是它们似乎并没有涵盖空检查。

任何帮助表示赞赏!

尼丁

如果只取出两个布尔值,则可以简化整个代码:

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;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章