views:

968

answers:

1

I want to configure my model binders with Nhibernate:

So I have:

<object id="GigModelBinder" type="App.ModelBinders.GigModelBinder, App.Web"  singleton="false"  >
<property name="VenueManager" ref="VenueManager"/>
<property name="ArtistManager" ref="ArtistManager"/>

I have an attribute which marks controller actions so that they use the correct model binder i.e.

[AcceptVerbs("POST")]
    public ActionResult Create([GigBinderAttribute]Gig gig)
    {
        GigManager.Save(gig);
        return View();
    }

This works fine and my GigModelBinder has the correct VenueManger and ArtistManager injected

However if in application Start I add:

System.Web.Mvc.ModelBinders.Binders.Add(typeof(App.Shared.DO.Gig), new GigModelBinder());

and in a controller action use :

UpdateModel<Gig>(gig);

for example:

[AcceptVerbs("POST")]
    public ActionResult Update(Guid id, FormCollection formCollection)
    {
        Gig gig = GigManager.GetByID(id);

        UpdateModel<Gig>(gig);

        GigManager.Save(gig);
        return View();
    }

The VenueManger and ArtistManager has NOT been injected into the GigModelBinder.

Any ideas what I'm doing wrong?

+1  A: 

In the first example you go via Spring.NET to retrieve your object. That means it'll look for all the dependencies and stick them into your object and all works well.

In the second example you forget about Spring.NET all along and just create an ordinary instance of a class.

The line where you register your binder should look like this:


System.Web.Mvc.ModelBinders.Binders[typeof(App.Shared.DO.Gig)] = context.GetObject("GigModelBinder");

where context is either an IApplicationContext or a IObjectFactory instance from Spring.NET package.

Best regards, Matthias.

Matthias Hryniszak