Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Unity inject dependencies when the class is resolved through Unity. A static class can not be created, so Unity can not inject dependencies.</p> <p>Instead of having a Static class, use Unity to resolve a pseudo-singleton class (<code>ContainerControlledLifetimeManager</code>) of ClassTwo. This way Unity injects <code>IClass</code> to <code>ClassTwo</code> when <code>ClassTwo</code> is created (resolved throug Unity container) and, as is configured as singleton, you always have the same instace of <code>ClassTwo</code> in the whole lifecicle of your application.</p> <p>You must resolve ClassTwo through Unity.</p> <pre><code>Container.RegisterType&lt;IClass, ClassImplOne&gt;("ImplOne"); Container.RegisterType&lt;IClass, ClassImplTwo&gt;("ImplTwo"); Container.RegisterType&lt;InterfaceImplemetedByClassTwo, ClassTwo&gt;(); //Simple example. Don't forget to use ContainerControlledLifetimeManager for ClassTwo to simulate sigleton. </code></pre> <p>And when you need ClassTwo:</p> <pre><code>Container.Resolve&lt;InterfaceImplemetedByClassTwo&gt; </code></pre> <p>Having the config in ClassTwo: </p> <pre><code>public class ClassTwo : InterfaceImplemetedByClassTwo { [Dependency("ImplOne")] //inject ClassImplOne public IClass SomeProperty { get; set; } </code></pre> <p>BUT this is not a great solution, I think your problem is with the phylosophy of DI. You need to cascade dependencies from the top layer classes of your app. Resolve the top layer classes in a explicit way. (<code>Container.Resolve</code>) and dependecies injection cascade down thanks to the magic of Unity. When 2 classes (top layer or not) need to use the same instance of <code>ClassTwo</code> Unity do the dirty work if you configured <code>ClassTwo</code> with <code>ContainerControlledLifetimeManager</code>.</p> <p>In other words, you don't need static class, you inject the same instance of a class in other classes than need it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload