Autofac 2 is on the way, now in Beta!

A while ago, I joined the ranks of those whose favorite new container became Autofac. Why do I like it? it’s FAST, easy to use, and very powerful due to it’s functional nature. You might say I am biased because the guy who wrote it was on my team, but anyway…

I am exited to hear that Nick has a new blog and is at it again and well on his way to the next version. Yes folks, Autofac 2 is coming!

If you head over to Google Code, you’ll see a lot of cool stuff coming in version 2. My favorite feature is below, ripped from the wiki.  If our new PartCreator<T> was reading this it would be seriously jealous!

Auto-Generated Factories

When one component needs to dynamically create instances of another, it can take a dependency on a delegate that returns the type of component it requires:

class UsesServer
{
  Func<IServerConnection> _connectionFactory;

  public UsesServer(Func<IServerConnection> connectionFactory)
  {
    _connectionFactory = connectionFactory;
  }

  public void OnNewWork()
  {
    IServerConnection connection = _connectionFactory();
    // …
  }
}


No explicit configuration is required to use this feature:

builder.RegisterType<FtpConnection>().As<IServerConnection>();
builder.RegisterType<UsesServer>();


By registering IServerConnection, Autofac 2 will automatically inject delegates that return type IServerConnection.

If you are gaga over Autofac, go check it out. If not, you should be!

 

 

 

This entry was posted in Autofac. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://nblumhardt.com Nicholas Blumhardt

    @Adam, you got it! The benefits really are:

    * T is a fully-fledged component, that can have dependencies etc
    * No infrastructure code needs to be written
    * If the delegate type accepts parameters these are handled smartly and make their way down to T

    All of these little features add up to making container use a low-friction experience, reducing the amount of time you have to think about how to manage a container instance and interact with it.

  • alwin

    Isn’t Hamilton, the creator of Castle Windsor on your team too? :)

  • http://www.adamjwolf.com Adam J Wolf

    I am a little confused about this feature. As I see it, you have removed the need for a developer to create and register a factory of type T in the container. Usually this factory will just new up a type of T and return it to the caller.

    This should allow your constructer injected code to take a Func that will allow it to create a dependent type on demand by using the delegate instead of a type like ServerConnectionFactory.Create().

    So we get factories for free. No code or configuration of factories — if we use delegates for creating dependent types.

    Cool, I think, am I missing something?

  • http://nblumhardt.com Nicholas Blumhardt

    Glad you like it :)

    Func<> is still jealous of PartCreator‘s support for metadata.