This post has been imported from my previous blog. I did my best to parse XML properly, but it might have some errors.
If you find one, send a Pull Request.
Recently I was debating with my friend about using RenderAction methods instead of rendering partial views. One could say, what the fuzz is all about. It’s all about simplicity. Rendering some part of your page with partial, makes you create big, fat model, passing its parts all around (for instance Model.SthForFirstPartial, Model.SthForSecondPartial). It can bring you to one model per one view or even action and this is baaaad. You have an alternative: you can simply render actions results in you parent view using mentioned methods. Advantages:
It seems, that this would be all, but it is not. Using NHibernate, my favourite ORM, and having it instantiated once per request (for Windsor fans: LifestyleType.PerWebRequest) I can easily fetch needed data in the parent action, loading it in a session first level cache. The child action then, will simply get data already fetched, having no problems with getting to many hits on your db.
I do like this paradigm.
(the people, which overuse Reflector, or its free replacements, to read code), there’s a few lines of code, which passes the original HttpContext with all its I tems (yeah, that’s the base for having LifestyleType.PerWebRequest working), an excerpt from ChildActionExtensions class:
var httpContext = htmlHelper.ViewContext.HttpContext;
var context = new RequestContext(httpContext, data2); // whoa! I'll get my request items back!
var httpHandler = new ChildActionMvcHandler(context);
httpContext.get_Server().Execute(HttpHandlerUtil.WrapForServerExecute(httpHandler), textWriter, true);