在c#中使用泛型和Func避免代码重复的最佳方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了在c#中使用泛型和Func避免代码重复的最佳方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3108字,纯文字阅读大概需要5分钟。
内容图文
![在c#中使用泛型和Func避免代码重复的最佳方法](/upload/InfoBanner/zyjiaocheng/795/76054a0da07b46589bbbfad44ee0cf58.jpg)
我想知道什么是避免使用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避免代码重复的最佳方法全部内容,希望文章能够帮你解决在c#中使用泛型和Func避免代码重复的最佳方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。