.
, foo, bar
public class Foo
{
public Foo(Bar bar)
{
}
}
bar . , Bar, ?
, IBar , .
, , , , , , IBar, .
Instead, we can use dependency injection like this.
IUnityContainer myContainer = new UnityContainer();
myContainer.RegisterType<IBar, Bar>();
Foo foo = myContainer.Resolve<Foo>();
Here, the unity frame inserted the Bar object into the Foo constructor, if you change the type registration in one place, you will change how Foo objects are resolved everywhere.
When you have complex dependencies that can change, DI is needed!
source
share