Pergunta

I want to be able to pass in my actual parsing function in my cache update callback. How can I optimize my code duplication below using delegates? Thanks

//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?
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
}
Foi útil?

Solução

Like this:

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();
}

Outras dicas

It absolutely does make sense to pass in the call back as you mention in the comment in your code.

Just change:

public void getContent() {...}

to:

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

And change itemUpdateCallBack to take Func<TypeOfValue> parsecallback in as an argument too.

From outside, you need something like this:

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

Either that, or you could pass it into your constructor. It is less flexible but is perfect for situations where you know that this will always be the same for a given object.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top