Glenn Block

Sponsors

The Lounge

Wicked Cool Jobs

News

  • View Glenn Block's profile on LinkedIn

    Me

Advertisement

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!

 

 

 


Posted Thu, Dec 31 2009 1:46 AM by Glenn Block
Filed under:

[Advertisement]

Comments

Nicholas Blumhardt wrote re: Autofac 2 is on the way, now in Beta!
on Thu, Dec 31 2009 5:04 AM

Glad you like it :)

Func<> is still jealous of PartCreator<T,M>'s support for metadata.

uberVU - social comments wrote Social comments and analytics for this post
on Thu, Dec 31 2009 5:25 AM

This post was mentioned on Twitter by gblock: blogged: "Autofac 2 is on the way, now in Beta!" http://twurl.nl/076u4h

Adam J Wolf wrote re: Autofac 2 is on the way, now in Beta!
on Thu, Dec 31 2009 10:44 AM

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<T> 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?

alwin wrote re: Autofac 2 is on the way, now in Beta!
on Fri, Jan 1 2010 2:00 PM

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

Nicholas Blumhardt wrote re: Autofac 2 is on the way, now in Beta!
on Fri, Jan 1 2010 3:45 PM

@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.

pligg.com wrote Autofac 2 is on the way
on Wed, Jan 27 2010 11:49 PM

ALT.NET dotnet .NET C# Agile BizTalk ASP.NET

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Devlicio.us