2

I have a class that currently takes an IIndex in order to select an indexed dependency. I'd like to change that to somehow using an attribute on the constructor argument, in order to directly specify which one to use.

I'm pretty sure Autofac doesn't support this natively, but I'm not afraid to write my own attribute for this. I've messed around with the source side of Autofac before (custom registration sources, etc.) but I've never seen a way of tailoring the injection side. Can anyone help?

P.S.: I've seen this question but it turned into a discussion on the benefits of attribute-based injection. OTOH, I already decided I don't want to have to manually register each one of my types that depends on this particular dependency one-by-one, so I'd prefer it.

2
  • 1
    Does this blogpost help you? If not could you add a small example how the IIndex is currently used, and some pseudo-code of how you would like it to become?
    – wimh
    Commented Jan 12, 2013 at 22:01
  • [I posted something like this in a previous question. Give it a try.][1] [1]: stackoverflow.com/a/14124961/540663
    – Jim Bolla
    Commented Jan 14, 2013 at 14:45

2 Answers 2

2

You can achieve this by scanning your class metadata on resolving an interface. When you get info about its parameters you can resolve actual implementation of the interface. See my answer here.

0

I noticed http://code.google.com/p/autofac/wiki/WebFormsIntegration says we can create a Custom Dependency Injection Models by implementing IInjectionBehaviour, I think it worth a try.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.