如何重构多余的try catch代码?

时间:2015-09-22 17:48:33

标签: c# refactoring

我正在尝试确定重构此代码的最佳方法。正如你所看到的,try ... catch .. catch .. catch ...代码行是完全相同的。两种方法之间的唯一区别是一种是异步,而另一种则不是。

public override int SaveChanges()
{
    try
    {
        return base.SaveChanges();
    }
    catch (DbEntityValidationException ex)
    {
        var entityValidationErrors = ex.EntityValidationErrors
            .SelectMany(e => e.ValidationErrors)
            .Select(x => string.Format("{0} - {1}", x.PropertyName, x.ErrorMessage));

        var fullErrorMessage = string.Join(Environment.NewLine, entityValidationErrors);
        var exceptionMessage = string.Concat(ex.Message, " Entity validation errors: ", fullErrorMessage);

        throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
    }
    catch (DbUpdateConcurrencyException ex)
    {
        Debug.WriteLine(ex.Message);
        throw;
    }
    catch (DbUpdateException ex)
    {                
        var sqlException = ex.GetBaseException() as SqlException;

        if (sqlException == null || sqlException.Errors.Count <= 0) 
            throw;

        var errors = new List<string>();

        for (var i = 0; i < sqlException.Errors.Count; i++)
        {
            errors.Add(string.Format("{0} - {1}", sqlException.Errors[i].Number, sqlException.Errors[i].Message));
        }

        throw new DbUpdateException(string.Join(Environment.NewLine, errors));
    }
}
public override async Task<int> SaveChangesAsync()
{
    try
    {
        return await base.SaveChangesAsync();
    }
    catch (DbEntityValidationException ex)
    {
        var entityValidationErrors = ex.EntityValidationErrors
            .SelectMany(e => e.ValidationErrors)
            .Select(x => string.Format("{0} - {1}", x.PropertyName, x.ErrorMessage));

        var fullErrorMessage = string.Join(Environment.NewLine, entityValidationErrors);
        var exceptionMessage = string.Concat(ex.Message, " Entity validation errors: ", fullErrorMessage);

        throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
    }
    catch (DbUpdateConcurrencyException ex)
    {
        Debug.WriteLine(ex.Message);
        throw;
    }
    catch (DbUpdateException ex)
    {
        var sqlException = ex.GetBaseException() as SqlException;

        if (sqlException == null || sqlException.Errors.Count <= 0)
            throw;

        var errors = new List<string>();

        for (var i = 0; i < sqlException.Errors.Count; i++)
        {
            errors.Add(string.Format("{0} - {1}", sqlException.Errors[i].Number, sqlException.Errors[i].Message));
        }

        throw new DbUpdateException(string.Join(Environment.NewLine, errors));
    }
}

2 个答案:

答案 0 :(得分:1)

同时执行Sync和Async方法的一种方法是使用Sync方法和Async方法中的实际逻辑将第一个逻辑包装到Task中。

public override Task<int> SaveChangesAsync()
{
    return new Task(SaveChanges);
}

答案 1 :(得分:-1)

我最终使用委托(Func)解决了这个问题。这是两个&#34;重复&#34; .SaveChanges方法现在。

app.controller('myController', [ '$scope', 
function ( $scope) {
..

 $scope.addClient = function () {
   $scope.$close(); // or $scope.$dismiss();
 }
}

这是包装器。

public override int SaveChanges()
{
    return SaveChangesWrapper<int>(() => base.SaveChanges());
}
public override async Task<int> SaveChangesAsync()
{
    return await SaveChangesWrapper<Task<int>>(async () => await base.SaveChangesAsync());
}