3

We used ModelBinders.Binders on our project to make resolving of controller action parameters easier and make code more testable.

ModelBinders.Binders[typeof(SomeType)] = new SomeTypeModelBinder();

Model binder resolves object:

public class SomeTypeModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
       var someTypeResolver = DependencyResolver.Current.GetService<ISomeTypeResolver>();
       return someTypeResolver.GetCurrentSomeType();
    }
}

Then it becomes easy to use this type as model in our controllers:

public ActionResult SomeAction(SomeType someType)
{
    return View(someType);
}

The problem is that it becomes hard to measure performance of action using Sitecore Debug. It looks like binding of action parameter in this way causes that it is not summed up to time of rendering controller action(this assumptions is done, because time of rendering is too small):

Finished rendering "Controller: SomeController. Action: SomeAction". 26.62 ms

The questions are:

  • Does Sitecore Debug mode take into account time that is spent on execution of model binders?
  • If not, then how to force Sitecore Debug mode to start add this time to total time of execution(and remain code testable)?

1 Answer 1

2

I looked how Sitecore timers for debug mode are implemented, and modify my code:

public class SomeTypeModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
       Tracer.Info("Starting BindModel for SomeType");
       Tracer.Indent++;
       Profiler.StartOperation("BindModel for SomeType.");
       var someTypeResolver = DependencyResolver.Current.GetService<ISomeTypeResolver>();
       var object = someTypeResolver.GetCurrentSomeType();
       Profiler.EndOperation("BindModel for SomeType.");
       return object;
    }
}

That is not exact thing that I wanted, but adds more information in debug mode about timings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.