2012-07-25 80 views
2

接口签名我有缓存的东西一个简单的接口,它是这样的无法确定功能

public interface ICacheService 
{ 

     T Get<T>(string cacheId, Func<T> getItemCallback) where T : class; 
} 

,这适用于简单的回调函数,但在我来说,我需要添加一些更复杂一点。我认为这是一个匿名类型...

在控制器我注入运行一个查询,这是这样一种服务:

this.queryContainer.Get<ObjectQuery>().Execute(new ObjectParameters(id)); 

当然这个的,但类型是Func键的不那么如果编译器抱怨说,我尝试使用我的缓存服务。

我需要什么样的接口才能让缓存工作? 理想我想这样做:

this.cachingService.Get<ObjectResult>(id, this.queryContainer.Get<ObjectQuery>().Execute(new ObjectParameters(id))); 

它甚至有可能?

任何帮助,非常感谢!

非常感谢

+0

'... Execute(new ObjectParameters(id))。Single()'? – mellamokb 2012-07-25 17:16:32

回答

2

你只需要它表现为Func<T>? 试试这个:

this.cachingService.Get<ObjectResult>(id,()=> this.queryContainer.Get<ObjectQuery>().Execute(new ObjectParameters(id))); 
+0

这样做。很简单! – Nick 2012-07-26 09:20:40

1

你可以使用lambda函数。

() => this.queryContainer... 

它将您的代码包装在一个没有参数的函数中返回一个值。因此,它满足了Func的合同