开发者

MVC: Which method should be overridden to cache action result

I'm preparing to Microsoft Certificate exam (70-515), reading Microsoft book for this exam, practicing tests... one tests asks:

You are creating a custom MVC action filter to cache action results.

Which virtual method should you override?

Correct answer (according to test program, that is distributed with a book) is "OnResultExecuting"

And explanation for the answer:

When you create a custom action filter by inheriting from the ActionFilterAttribute class, you can override four virtual methods that run in the following order: OnActionExecuting(), OnActionExecuted(), OnResultExecuting(), and OnResultExecuted(). For output caching, you want to capture the final rendered results. Therefore, you should override the last method to run: OnResultExecuting().

Here is inconsistency: If we need to override the LAST mentioned method, then it should be "OnResultExecuted". But in answer it is told "OnResultExecuting".

So the question is:

  1. What is a CORRECT method to be overridden?
  2. Which option should I choose on exam to get answer considere开发者_StackOverflowd as correct? (Question is valid for case when "correct" answer is actually different from suggested by system.

Thanks.

P.S. I not sure if current question belongs to SO, but at least it is pretty close


After some time for me it made some sense: You should override 'OnResultExecuting' method in order to check if you have already result cached. If "yes" you will just get it from cache, if no, you will really execute functionality of "executing" part and then put it to cache.


The best way would be to look at the source for the built in OutputCacheAttribute. The main guts of it is:

public override void OnResultExecuting(ResultExecutingContext filterContext) {
    if (filterContext == null) {
        throw new ArgumentNullException("filterContext");
    }

    // we need to call ProcessRequest() since there's no other way to set the Page.Response intrinsic
    OutputCachedPage page = new OutputCachedPage(_cacheSettings);
    page.ProcessRequest(HttpContext.Current);
}

private sealed class OutputCachedPage : Page {
    private OutputCacheParameters _cacheSettings;

    public OutputCachedPage(OutputCacheParameters cacheSettings) {
        // Tracing requires Page IDs to be unique.
        ID = Guid.NewGuid().ToString();
        _cacheSettings = cacheSettings;
    }

    protected override void FrameworkInitialize() {
        // when you put the <%@ OutputCache %> directive on a page, the generated code calls InitOutputCache() from here
        base.FrameworkInitialize();
        InitOutputCache(_cacheSettings);
    }
}

So they have implemented it by overriding OnResultExecuting. I personally don't understand why you would wait that long... because the bulk of the time it takes for a request to process would be in the action method with all it's service, repository and whatever calls? No?

Maybe someone much smarter than me can explain.


Honestly, I don't agree with that approach. I, personally, would go with an override of OnActionExecuting and OnResultExecuted myself. It doesn't do much good to only override OnResultExecuted, since you've already executed the action method by the time the filter will be applied. You'd want to intercept the request before the action is executed and return the output cache in OnActionExecuting, and you'd want to capture the final result in OnResultExecuted.


Keep in mind that there is an [OutputCache] attribute built into MVC that handles output caching for you: http://www.asp.net/mvc/tutorials/improving-performance-with-output-caching-cs. For real development, this is likely a better approach than building a custom caching attribute.


The convention says (in relation to event/event executing methods): progressive form = before executing the named action, and past tense = after it happened. So the correct answer should indeed be OnResultExecuted.

However, I would advise you to contact microsoft and ask for clarification.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜