Prevent DB Callbacks Due to Object-Oriented Approach in MVC3 Application

We have an MVC3 application in which we have created many small actions and views to handle data placement wherever we need. For example, if it was a blog, and we wanted to show comments, we have the action and presentation of comments, and we can post it wherever we want, view the user profile and view the blog post, etc.

The problem caused by this is that every small view or action needs to be made a call, usually to the same service, several times per page load due to all the other small views that we have in our application. Thus, on a very large page containing these small views, we could have 80 + sql calls, with 40% of them duplicated, and then the page slows down. Currently, the solution is to cache some data and transfer some data to the ViewBag, if we can, if you want, as a user profile, you check if it has its cache or ViewBag if it does not ask for it.

This is really really messy for the design, and the view mode approach seems awful as it needs to be passed down from top to bottom. We added some data to HttpCurrent.Items to do this per request (instead of caching, as this data may change), but should there be some clean solution that doesn't seem wrong and also clean?

EDIT

I was asked to clarify, and although this is an internal business application, I can’t give away most of the specifics.

, . facebook. , MVC facebook, , . , , ( , 4 ), , . , .., , , , , , .

+5
4

, , .

  • . , . , , , . , , , / . .

  • . , , , , . , , AOP WCF. , - .

, WCF- ():

[Caching(300)]
Comment GetComment(int commentId);

WCF , , . (AppFabric) WCF.

Aspect Oriented Framework (AOP): http://en.wikipedia.org/wiki/Aspect-oriented_programming Unity : Unity vs Other IoC Containers

, , .

0

@Html.Action() @Html.Partial(), , , db?

CompositeViewModel, ViewModels . , UserViewModel, BlogPostViewModel Collection<BlogComment>.

, /, . , , , , , DRY .

, , @Html.Partial("~/Path/to/view.cshtml", Model.UserViewModel) , .

+2

ActionFilter, . , (.. ) .

public class SqlConnectionActionFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var sessionController = filterContext.Controller;

            if (filterContext.IsChildAction)
                return;

            //Create your SqlConnection here
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (filterContext.IsChildAction)
                return;

            //Commit transaction & Teardown SqlConnection
        }
    }
0

The fact is that if you make a request 80 times, then you get to db 80 times. The best solution is to use a cache with a request. The most elegant way to implement it is through AOP, so your code does not mind this problem.

0
source

All Articles