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!
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!