Register your Sharp custom connectivity device

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.

+4
source share
2 answers

One way to register is to add the line below in the Application_Start method in Global.asax

 ModelBinders.Binders.Add(typeof(Teacher), new TeacherModelBinder()); 

This http://www.dominicpettifer.co.uk/Blog/39/dependency-injection-in-asp-net-mvc-2---part-2--modelbinders-viewmodels describes another way to execute it.

To do this through Castle Windsor, you can add the code below to ComponentRegistrar.cs (located in the CastleWindsor folder)

container.Register (AllTypes.Of () .FromAssembly (TypeOf (TeacherBinder) .assembly) .Configure (c => c.LifeStyle.Singleton.Named (c.Implementation.Name.ToLower ())));

+10
source

I am not familiar with what the S # arp architecture provides in this case, but you can do something like this: http://iridescence.no/post/Constructor-Injection-for-ASPNET-MVC-Model-Binders. aspx

Then you simply register all your attachments in the container and register this new binder resolver as the default binder in asp.net mvc.

0
source

All Articles