首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >带空检查的c#重构开关语句

带空检查的c#重构开关语句
EN

Stack Overflow用户
提问于 2016-04-15 09:22:04
回答 5查看 679关注 0票数 3

我只是有点麻烦,几个开关声明真的,我觉得有一个更好的方式来实现最终目标。

从本质上说,我是将视图模型传递到一个方法中。该方法首先从数据库中检索视图模型所涉及的对象,然后由switch语句对特定属性执行空检查。基于该结果,另一条switch语句将对视图模型执行另一次空检查。在每个点从数据库中将值分配给对象,然后在结束时进行数据库更新。

这是代码

代码语言:javascript
代码运行次数:0
运行
复制
        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);
    }

我想我也许可以取消开关语句,转而使用if语句,从我所能知道的情况来看,仍然会有一些嵌套。只是想知道有没有人有什么建议。

在这里,我已经看过重构开关语句,但它们似乎并没有真正涵盖空检查。

任何帮助都是非常感谢的!

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2016-04-15 09:31:50

只要去掉两个bools,整个代码就可以简化:

代码语言:javascript
代码运行次数:0
运行
复制
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;
}
票数 1
EN

Stack Overflow用户

发布于 2016-04-15 09:28:53

我不完全确定您的位置--但是用if/else替换开关就像用if替换switch一样简单。以下是转换后的代码:

代码语言:javascript
代码运行次数:0
运行
复制
public async Task UpdateContractWithRepository(ViewModel viewModel)
{
    // Get the contract from db
    Contract contract = GetContract(viewModel.Id);

    if (viewModel.RepositoryId == null)
    {
        if (contract.RepositoryId == null)
        {
            // nothing to do
            // no change
        } else {
            // Unassign Repository
            UpdateRepositoryAssignment(contract.RepositoryId, false);

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

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

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

            // assign repository
            UpdateRepositoryAssignment(viewModel.RepositoryId, true);

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

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

    UpdateContract(contract);
}
票数 1
EN

Stack Overflow用户

发布于 2016-04-15 09:30:06

我认为没有必要这么复杂。试试看,我认为它的作用是一样的:

代码语言:javascript
代码运行次数:0
运行
复制
public async Task UpdateContractWithRepository(ViewModel viewModel){
        Contract contract = GetContract(viewModel.Id);

        if (viewModel.RepositoryId == null)
        {
            if(contract.RepositoryId != null){
                UpdateRepositoryAssignment(contract.RepositoryId, false);
                contract.RepositoryId = null;
                contract.ConsumedUnits = null;
            }
        }
        else
        {
            if (contract.RepositoryId == null)
            {
                UpdateRepositoryAssignment(viewModel.RepositoryId, true);
                Repository repository = GetRepository(viewModel.RepositoryId);
                contract.RepositoryId = repository.Id;
                contract.ConsumedUnits = repository.Units;
            }
            else
            {
                UpdateRepositoryAssignment(viewModel.RepositoryId, true);
                Repository repository = GetRepository(viewModel.RepositoryId);
                contract.RepositoryId = repository.Id;
                contract.ConsumedUnits = repository.Units;
            }
        }
        UpdateContract(contract);
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36643203

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档