2012-06-25 51 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); 

要麼,你可以把它變成你的構造。它不太靈活,但對於您知道對於給定對象而言這總是相同的情況,它是完美的。