I'm new to MVC (I'm using version 3) and Sharp Architecture, and it's hard for me to figure out how to use a custom mediator.
I have a domain object (not a view model) called a Teacher , and an ITeacherRepository repository executed in the standard Sharp Architecture way. I will register this route:
routes.MapRoute( "Teacher", "Teacher/{tid}/{action}", new { controller = "Teacher", action = "Index" });
and the Index method on the TeacherController as follows:
public ActionResult Index(int tid) { Teacher t = TeacherRepository.Get(tid); if (t == null) throw new InvalidOperationException("No such teacher"); TeacherDisplay display = new TeacherDisplay(t); return View("Index", display); }
Everything works perfectly. Now I want to take the next step and implement a custom binder for Teacher so that the controller method can look like this:
public ActionResult Index(Teacher t) { if (t == null) throw new InvalidOperationException("No such teacher"); TeacherDisplay display = new TeacherDisplay(t); return View("Index", display); }
I wrote a primitive primitive model:
public class TeacherBinder : SharpArch.Web.ModelBinder.SharpModelBinder { private ITeacherRepository teacherRepository = null; public TeacherBinder(ITeacherRepository repo) { this.teacherRepository = repo; } public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { int tid = (int)bindingContext.ValueProvider.GetValue("tid").ConvertTo(typeof(Int32)); Teacher t = teacherRepository.Get(tid); return t; } }
And now I'm stuck. How to register this in a Sharp Architecture project? I assume that I should connect it to Castle Windsor configuration. Should I have an ITeacherBinder interface and register it using Windsor?
EDIT
To clarify my problem: I can't figure out how to register my connecting device so that the MVC infrastructure creates it through Windsor and therefore takes care of passing the required constructor argument. The controllers are created by Windsor, and this is related to this line in global.asax.cs :
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container));
I do not see an equivalent factory model builder.