2012-06-25 59 views
2

我希望能够在我的缓存更新回调中传入我的实际解析函数。如何使用代表优化我的代码重复?由于Cache.Insert的委托

//intial setup code 
public void getJSONContent() //can I pass itemUpdateCallback in here? Does it make sense? 
{ 

    Content = (String)HttpContext.Current.Cache[Path]; 

    if (Content == null) 
    {     
     Content = parseXMLContent(); 

     HttpContext.Current.Cache.Insert(
     key, 
     Content, 
     new CacheDependency(Path), 
     Cache.NoAbsoluteExpiration, 
     Cache.NoSlidingExpiration,  
     jsonUpdateCallback); //callback in the event of my file in cache has changed 
     ^^^^^^^^^^^^^^^^^^ 

    } 
} 

private void jsonUpdateCallback(string key, CacheItemUpdateReason reason, out object value, out CacheDependency dependency, out DateTime exipriation, out TimeSpan slidingExpiration) 
{ 
    dependency = new CacheDependency(key); 
    exipriation = Cache.NoAbsoluteExpiration; 
    slidingExpiration = Cache.NoSlidingExpiration; 
    value = jsonXMLContent(); //how can pass this function into here, so I can can have different parse functions using the same code? 
    ^^^^^^^^^^^^^^^^^^^^^^^^^^ 
} 

//intial setup code 
public void getXMLContent() //can I pass itemUpdateCallback in here? Does it make sense? 
{ 

    Content = (String)HttpContext.Current.Cache[Path]; 

    if (Content == null) 
    {     
     Content = parseXMLContent(); 

     HttpContext.Current.Cache.Insert(
     key, 
     Content, 
     new CacheDependency(Path), 
     Cache.NoAbsoluteExpiration, 
     Cache.NoSlidingExpiration,  
     xmlUpdateCallback); //callback in the event of my file in cache has changed 
     ^^^^^^^^^^^^^^^^^^ 

    } 
} 

private void xmlUpdateCallback(string key, CacheItemUpdateReason reason, out object value, out CacheDependency dependency, out DateTime exipriation, out TimeSpan slidingExpiration) 
{ 
    dependency = new CacheDependency(key); 
    exipriation = Cache.NoAbsoluteExpiration; 
    slidingExpiration = Cache.NoSlidingExpiration; 
    value = parseXMLContent(); //how can pass this function into here, so I can can have different parse functions using the same code? 
    ^^^^^^^^^^^^^^^^^^^^^^^^^^ 
} 
+0

那么,有什么实际问题? –

+0

此外,请参阅“[堆栈溢出不允许标题中的标记](http://meta.stackexchange.com/a/130208)”。 –

+0

thx。我已更新问题。 – River

回答

1

像这样:

public void getXMLContent() 
{ 
    getContent(parseXmlContent); 
} 

public void getContent(Func<string> parseContent) 
{ 

    Content = (String)HttpContext.Current.Cache[Path]; 

    if (Content == null) 
    {     
     Content = parseContent(); 

     HttpContext.Current.Cache.Insert(
     key, 
     Content, 
     new CacheDependency(Path), 
     Cache.NoAbsoluteExpiration, 
     Cache.NoSlidingExpiration,  
     delegate(string key2, CacheItemUpdateReason reason, out object value, out CacheDependency dependency, out DateTime expiration, out TimeSpan slidingExpiration) { 
      itemUpdateCallback(key2, reason, parseContent, out value, out dependency, out expiration, out slidingExpiration); 
     }); 
    } 
} 

private void itemUpdateCallback(string key, CacheItemUpdateReason reason, Func<string> parseContent, out object value, out CacheDependency dependency, out DateTime exipriation, out TimeSpan slidingExpiration) 
{ 
    dependency = new CacheDependency(key); 
    exipriation = Cache.NoAbsoluteExpiration; 
    slidingExpiration = Cache.NoSlidingExpiration; 
    value = parseContent(); 
} 
+0

Thx很多。函数 parseContent - 你能否改变这个语法来接受一个通用的逆向类型T?我试过但不可以 – River

+0

@河:内容= parseContent()'从通用的角度来看是没有意义的,除非内容本身是一个通用类型。 – StriplingWarrior

1

这绝对是有一定道理的呼叫传回,你在你的代码中的注释提及。

只是改变:

public void getContent() {...} 

到:

public void getContent(Func<TypeOfValue> parsecallback) {...} 

,并更改itemUpdateCallBack采取Func<TypeOfValue> parsecallback作为参数太多。

从外面,你需要的东西是这样的:

Func<TypeOfValue> func =() => 
{ 
    MethodCall1(); 
    MethodCall2(); 
    return MethodCall3(); 
}; 
myObj.getContent(func); 

要么,你可以把它变成你的构造。它不太灵活,但对于您知道对于给定对象而言这总是相同的情况,它是完美的。