6

I'm looking for an IoC container to use in my Compact Framework application. Trying out Funq I noticed that I can't find a way to do Property Injection with it.

I've looked through the discussion on the project's site and the unit tests for it, but I can't find any example of Property Injection.

Does Funq support Property Injection?

Johann Gerell
  • 24,991
  • 10
  • 72
  • 122
Michał Drozdowicz
  • 1,232
  • 11
  • 30
  • 3
    I hope you get an answer to your question, but on a side note consider using Constructor Injection instead. Property Injection implies that the Dependency is optional, and that is rarely the case. – Mark Seemann Jun 29 '10 at 09:09
  • Thanks for the advice and generally I agree. I would like to know my options and constraints anyway. – Michał Drozdowicz Jun 29 '10 at 09:24

1 Answers1

2

Well wouldn't that generally go something like this?

myContainer.Register<IUserRepository>(() =>
    {
        var myRepository = new SomeUserRepository();
        myRepository.SomeProperty = someValue;

        return myRepository;
    });
herzmeister
  • 11,101
  • 2
  • 41
  • 51
  • `someValue` in this context is usually a service from the container. How do you get to that service? – Peter Lillevold Jun 30 '10 at 08:50
  • `myRepository.SomeProperty = myContainer.Resolve();` ? – herzmeister Jun 30 '10 at 10:20
  • Got an answer on Funq forum. In Funq you actually get the container in a parameter to the provider method, so you can do: myContainer.Register((c)=>new SomeUserRepository(){SomeProperty = c.Resolve()}); – Michał Drozdowicz Jun 30 '10 at 11:26
  • 1
    The property initializer syntax is surely more concise. That the Funq framework provides the container as a lambda parameter again is a good idea and helps the compiler to avoid capturing which might have negative impact on the lifetime of the container, and helps beginners to avoid unwanted closure side effects. – herzmeister Jun 30 '10 at 11:41