Property injection using Autofac

509 views
Skip to first unread message

Simon

unread,
Dec 31, 2009, 5:19:33 AM12/31/09
to Autofac
Hi all,

I've been using the Autofac framework for the last couple of days and
have been unable to get property injection working within the MVC
application I'm developing. I've set Autofac up within the
Application_Start method, as per the instructions on the wiki, but
whenever I debug into my code, the property I'm trying to inject is
always null.

Here's my scenario:
I have a strongly typed view that is bound to a domain object called
Client, via a controller called AgentController. This object has an
IClientData property called DataProvider that resolves to an instance
of ClientData. What do I need to do to get the DataProvider property
to not be null? I'm using Autofac 1.4.5 at the moment but would like
to switch to 2, if I can get this working! I could use constructor
injection but would need to rethink the architecture and maybe make
use of the Controller Factory I've been reading about to register my
controller classes. Because the Client object is tied to the view, the
instance I get is provided to me by the MVC framework, hence it gets
instantiated with an empty constructor. Not yet sure how I'd get
around that one.

Here's the code in my Global.asax file. What have I missed/not done
correctly/misunderstood? I've seen the wiki page on Property Injection
but am none the wiser for reading it! Is it more straightforward to do
this using Autofac 2? I noticed that ActivatingHandler is no longer
present within version 2 - what's the equivalent technique?

/// <summary>
/// Starts the application.
/// </summary>
protected void Application_Start()
{
ContainerBuilder builder = RegisterDependencies();
AutofacControllerModule autofacControllerModule = new
AutofacControllerModule(Assembly.GetExecutingAssembly());
autofacControllerModule.ActivatingHandler +=
ActivatingHandler.InjectProperties;
builder.RegisterModule(autofacControllerModule);
containerProvider = new ContainerProvider(builder.Build());
ControllerBuilder.Current.SetControllerFactory(new
AutofacControllerFactory(this.ContainerProvider));
... other code omitted ...
}

/// <summary>
/// Registers the dependencies.
/// </summary>
/// <returns>A ContainerBuilder with the dependencies registered.</
returns>
private static ContainerBuilder RegisterDependencies()
{
ContainerBuilder builder = new ContainerBuilder();
builder.Register<AgentController>();
builder.Register<Model.Client>().OnActivating
(ActivatingHandler.InjectProperties);
builder.Register<ClientData>().As<IClientData>().OnActivating
(ActivatingHandler.InjectProperties);
return builder;
}


Thanks in advance for any help you can give me

Simon

Nicholas Blumhardt

unread,
Dec 31, 2009, 6:11:36 PM12/31/09
to aut...@googlegroups.com
Hi Simon,

ActivatingHandler.InjectProperties will only work when Autofac creates a component. If Client is being created by the MVC framework via its default ctor, Autofac won't have an opportunity to set its dependencies.

Also, AutofacControllerModule will find your controllers, so there probably isn't any need to register AgentController with it.

If there is absolutely no way to get MVC to create Client via Autofac, you can do something like what's below. I'm assuming Client is a property of AgentController.

autofacControllerModule.ActivatingHandler += (s, e) => {
   var ac = e.Instance as AgentController;
   if (ac != null)
      e.Context.InjectProperties(ac.Client);
};

Autofac 2 will likely be updated to use the new assembly scanning mechanism for controller registration - this should be easier to work with. The syntax to enable property injection is simplified too, along the lines of - builder.Register(x).InjectProperties();

Cheers,

Nick

2009/12/31 Simon <simon...@gmail.com>
--

You received this message because you are subscribed to the Google Groups "Autofac" group.
To post to this group, send email to aut...@googlegroups.com.
To unsubscribe from this group, send email to autofac+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/autofac?hl=en.



Reply all
Reply to author
Forward
0 new messages