Singelton for Property Injection

Mar 3, 2008 at 2:52 AM
This is probably a simple question, but I am just getting started with Unity and would really appreciate a point in the right direction.

How do I specify that a property (marked with the <Dependency> attribute) should be created as a singleton?

Thanks
Chris
Mar 3, 2008 at 4:25 AM
You don't specify it on the property, you specify it on the type when you configure the container.

If you have:

{
class Foo {{
[Dependency]
public Bar MyBar {{ get {{ ... }} set ... }}}}}

Then when you configure the container, you tell it that you want Bar's to be singletons:

{
container.SetSingleton<Bar>();
}

Then from that point on every time the container resolves a Bar instance, it'll return the same one.

Hope this helps,

-Chris