我有一个WCF服务,它公开了一堆对各种类型的集合执行操作的[OperationContract]。
现在,几乎每个实现都是这样的:
public void PerformSomeOperation(List<SomeType> someCollection) {
// store the exceptions that might be thrown during the operation
var exceptions = new List<Exception>();
foreach (var item in someCollection) {
try {
this.PerformSomeOperation(someParameters);
}
catch (Exception exception) {
exceptions.Add(exception);
}
}
// Throw the exceptions here after the loop completes.
if (exceptions.Count > 0) throw new AggregateException(exceptions);
}因此,虽然this.PerformSomeOperation(...)部分在实际实现之间发生变化,但框架的其余部分保持不变。因此,动态注入this.PerformSomeOperation(...)部件的想法。
最优雅的解决方案是什么?当然,我可以传入要作为字符串在外部方法的参数列表中执行的操作的方法名称,并使用反射调用该方法(.GetMethod(...)),但我希望得到一些优雅的Lambda或Delegate构造。
发布于 2014-04-25 07:05:40
使用辅助方法
public void YourServiceMethod()
{
var collection = GetSomeDate();
DoWork(collection, item => PerformSomeOperation(someParameters));
}
private void DoWork(List<SomeType> collection, Action<SomeType> itemProcessor)
{
var exceptions = new List<Exception>();
foreach (var item in collection)
{
try
{
itemProcessor(someParameters);
}
catch (Exception exception)
{
exceptions.Add(exception);
}
}
// Throw the exceptions here after the loop completes.
if (exceptions.Count > 0) throw new AggregateException(exceptions);
}或者使用扩展方法:
public void YourServiceMethod()
{
var collection = GetSomeDate();
collection.DoWork(item => PerformSomeOperation(someParameters));
}
public class ListExtensions
{
public void DoWork(this List<SomeType> collection, Action<SomeType> itemProcessor)
{
var exceptions = new List<Exception>();
foreach (var item in collection)
{
try
{
itemProcessor(someParameters);
}
catch (Exception exception)
{
exceptions.Add(exception);
}
}
// Throw the exceptions here after the loop completes.
if (exceptions.Count > 0) throw new AggregateException(exceptions);
}
}加入一些泛型(以支持所有类型):
public void YourServiceMethod()
{
var collection = GetSomeDate();
collection.ProcessList(item => PerformSomeOperation(someParameters));
}
public class ListExtensions
{
public void ProcessList<T>(this IEnumerable<T> collection, Action<T> itemProcessor)
{
var exceptions = new List<Exception>();
foreach (var item in collection)
{
try
{
itemProcessor(someParameters);
}
catch (Exception exception)
{
exceptions.Add(exception);
}
}
// Throw the exceptions here after the loop completes.
if (exceptions.Count > 0) throw new AggregateException(exceptions);
}
}发布于 2014-04-25 06:54:46
为什么不使用代表们呢?这些基本上是函数指针。
因此,您有一个通用的DoWork方法和一个动作
public void DoWork(Action<List<SomeType>> myDelegate)
{
var exceptions = new List<Exception>();
foreach (var item in someCollection)
{
try {
myDelegate(someParameters);
}
catch (Exception exception) {
exceptions.Add(exception);
}
}
// Throw the exceptions here after the loop completes.
if (exceptions.Count > 0) throw new AggregateException(exceptions);
}然后用指向您自己的函数PerformSomeOperation的指针调用它。
DoWork(PerformSomeOperation);现在您可以切换可以使用的方法了。
DoWork(PerformOtherOperationWithSameSignature)https://stackoverflow.com/questions/23285995
复制相似问题