Does Funq IoC Container support property injection?
Asked Answered
A

1

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?

Amyamyas answered 29/6, 2010 at 9:1 Comment(2)
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.Fallingout
Thanks for the advice and generally I agree. I would like to know my options and constraints anyway.Isabelleisac
A
2

Well wouldn't that generally go something like this?

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

        return myRepository;
    });
Addicted answered 29/6, 2010 at 9:19 Comment(4)
someValue in this context is usually a service from the container. How do you get to that service?Papert
myRepository.SomeProperty = myContainer.Resolve<TSomeService>(); ?Addicted
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<IUserRepository>((c)=>new SomeUserRepository(){SomeProperty = c.Resolve<SomeService>()});Isabelleisac
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.Addicted

© 2022 - 2024 — McMap. All rights reserved.