Automatically Resolve Func<TService>

Dec 29, 2009 at 12:58 AM

A feature I've noticed in one or two other IoC frameworks, and one that would be really handy, is the ability to resolve Func<TService> if TService is registered.

So I could write this:

var cont = new Container();
cont.Register<IFoo>(c => new Foo());

var fooFactory = cont.Resolve<Func<IFoo>>();

This is a contrived example, I know, because LazyResolve would work as a substitute in this case. Where this would be really useful is when it's combined with automatic registration. So if I have a class with a Func constructor parameter, I could let Funq automatically register it and have it work out how to resolve a Func<T> for me.

Would this be hard to implement? I'm thinking that the underlying implementation of Resolve<TService> (the GetEntry method) could first check if TService is registered, and if not, check if TService is itself a Func<T> and check if T is registered.

What do you think, Kzu?

Jan 4, 2010 at 3:56 AM

Well, I had a crack at implementing this but hit a brick wall pretty quickly. I think it requires some hardcore System.Linq.Expressions stuff to pull off, though it doesn't feel like it should.

The beta of Autofac 2 can resolve a Lazy<T>, Func<T> and IEnumerable<T> (as well, I think, as combinations of those, like IEnumerable<Func<T>>) with only a T registered. It's an awesome feature.

Coordinator
Jan 4, 2010 at 5:21 AM
agreed.

/kzu

--
Daniel Cazzulino | Developer Lead | XML MVP | Clarius Consulting | +1 425.329.3471


On Mon, Jan 4, 2010 at 00:56, mabster <notifications@codeplex.com> wrote:

From: mabster

Well, I had a crack at implementing this but hit a brick wall pretty quickly. I think it requires some hardcore System.Linq.Expressions stuff to pull off, though it doesn't feel like it should.

The beta of Autofac 2 can resolve a Lazy<T>, Func<T> and IEnumerable<T> (as well, I think, as combinations of those, like IEnumerable<Func<T>>) with only a T registered. It's an awesome feature.

Read the full discussion online.

To add a post to this discussion, reply to this email (funq@discussions.codeplex.com)

To start a new discussion for this project, email funq@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com