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

时间:2016-04-15 09:22:04

标签: c# switch-statement refactoring

我只是遇到一些切换语句有点麻烦,我觉得有更好的方法来实现最终目标。

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

这是代码

        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语句,但仍然会有一些我能说的嵌套。只是想知道是否有人有任何建议。

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

感谢任何帮助!

5 个答案:

答案 0 :(得分:1)

我不完全确定你被困在哪里 - 但用if / else替换开关几乎就像用switch替换if一样简单。这是转换后的代码:

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 :(得分:1)

我认为没有必要这么复杂。 尝试这个我认为它做同样的工作:

var cachedString = _retryPolicy.ExecuteAction(() => dataCache.StringGet(fullCacheKey));

答案 2 :(得分:1)

如果您只拿出两个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;
}

答案 3 :(得分:0)

我真的会将if....else if与bool条件一起使用。您可以使用有意义的变量名使其更具可读性:

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

    bool modelRepositoryKnown = viewModel.RepositoryId != null;
    bool contractRepositoryKnown = contract.RepositoryId != null;

    if (modelRepositoryKnown)
    {
        if (contractRepositoryKnown)
        {
            // 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;
        }
    }
    else if(contractRepositoryKnown)  // Model-Repository unknown but Contract-Repository Known
    {
        // Unassign Repository
        UpdateRepositoryAssignment(contract.RepositoryId, false);

        // Update properties
        contract.RepositoryId = null;
        contract.ConsumedUnits = null;
        break;
    }
    UpdateContract(contract);
}

答案 4 :(得分:0)

您可以通过将嵌套切换条件组合到使用多个条件的if else语句中来实现它。

if(viewModel.RepositoryId == null && !contract.RepositoryId)
{
    // Unassign Repository
    UpdateRepositoryAssignment(contract.RepositoryId, false);
    // Update properties
    contract.RepositoryId = null;
    contract.ConsumedUnits = null;
}
else if(!viewModel.RepositryId && 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 if(!viewModel.RepositryId && !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;
}