编程语言
首页 > 编程语言> > 在c#中使用泛型和Func避免代码重复的最佳方法

在c#中使用泛型和Func避免代码重复的最佳方法

作者:互联网

我想知道什么是避免使用Generics Func或任何其他方式重复某些recuring代码结构的最佳方法.
作为一个实际的例子,我需要调用20个不同的WCF方法,但我希望有代码来处理异常.

假设这是wcf代理

 class ClassWithMethodsToCall // say wcf proxy
    {
        public Out1 GetOut1(In1 inParam) { return null; } // would have some spesific implementation 
        public Out2 GetOut2(In2 inParam) { return null; }
        public Out3 GetOut3(In3 inParam) { return null; }
    }

class Out1 { }  // some specific data structure
class In1 { }   // some specific data structure

class Out2 { }  // some specific data structure
class In2 { }   // some specific data structure

class Out3 { }  // some specific data structure
class In3 { }   // some specific data structure

我创建了以下内容以进行单一错误处理

class CallerHelperWithCommonExceptionHandler
    {
        public Tout Call<Tout, Tin>(Tin parameters, Func<Tin,Tout> wcfMethodToCall)
        {
            try
            {
                return wcfMethodToCall(parameters);
            }
            catch (Exception ex)
            {
                // do what ever
                throw;
            }
        }
    }

我用它:

var callerHelper = new CallerHelperWithCommonExceptionHandler();
            var theFunctionsToCall = new ClassWithMethodsToCall();

        var in1 = new In1(); // init as appropriate
        var ou1 = callerHelper.Call<Out1, In1>(in1, theFunctionsToCall.GetOut1);

        var in2 = new In2(); // init as appropriate
        var ou2 = callerHelper.Call<Out2, In2>(in2, theFunctionsToCall.GetOut2);

        // and so on

有更好的更优雅的方式吗?面向对象方式的替代方案,模板设计模式?

谢谢,等等

解决方法:

看起来您正在添加代码以将cross-cutting concern实现到类(例如,记录异常),因此您可能希望使用decorator pattern.

例如:

class Out1 { };  // some specific data structure
class In1 { }   // some specific data structure

class Out2 { }  // some specific data structure
class In2 { }   // some specific data structure

class Out3 { }  // some specific data structure
class In3 { }

internal interface IClassWithMethodsToCall
{
    Out1 GetOut1(In1 inParam);
    Out2 GetOut2(In2 inParam);
    Out3 GetOut3(In3 inParam);
}

class ClassWithMethodsToCallImpl: IClassWithMethodsToCall
{
    public Out1 GetOut1(In1 inParam) { return null; } // would have some spesific implementation 
    public Out2 GetOut2(In2 inParam) { return null; }
    public Out3 GetOut3(In3 inParam) { return null; }
}

class ClassWithMethodsToCall: IClassWithMethodsToCall
{
    private readonly ClassWithMethodsToCallImpl _impl;

    public ClassWithMethodsToCall(ClassWithMethodsToCallImpl impl)
    {
        _impl = impl;
    }

    public Out1 GetOut1(In1 inParam)
    {
        return tryFunc(() => _impl.GetOut1(inParam));
    }

    public Out2 GetOut2(In2 inParam)
    {
        return tryFunc(() => _impl.GetOut2(inParam));
    }

    public Out3 GetOut3(In3 inParam)
    {
        return tryFunc(() => _impl.GetOut3(inParam));
    }

    private static T tryFunc<T>(Func<T> func)
    {
        try
        {
            return func();
        }

        catch (Exception exception)
        {
            // Do something with exception
            throw;
        }
    }
}

客户端代码只使用IClassWithMethodsToCall,您可能会在某处使用工厂方法创建ClassWithMethodsToCallImpl并使用它创建ClassWithMethodsToCall并将ClassWithMethodsToCall作为IClassWithMethodsToCall返回.

或者(可能更好)将使用Aspect-oriented programming.这需要更多的投资,可能需要使用第三方库来支持它,但它可能是长期的方式.

我注意到您正在使用WCF代理.因为它使用MarshalByRefObject,所以您可以利用它来实现AOP. There’s some information about it in a blog here.

标签:c,func,generics,code-duplication
来源: https://codeday.me/bug/20190623/1275471.html