Possible Duplicate:
Is it better to create a singleton to access unity container or pass it through the application?
I am introducing IoC container into the system. The natural question is, should it be a singleton or an instance passed to a class to use? I lean towards having it as a singleton instance because:
Here is how it looks:
class Main
{
public static void main(params string[] args)
{
IoCContaner.Intance.Add<IBar>();
IoCContaner.Intance.Add<IBaz>();
IoCContaner.Intance.Add<IQux>();
var foo = new Foo();
Foo.DoBarStuff();
}
}
class Bar : IBar
{
public Bar(IBaz, IQuz) {}
public void DoBazStuff() { _baz.DoStuff(); }
}
class Foo
{
public void DoBarStuff()
{
var bar = IoCContaner.Intance.Resolve<IBar>();
bar.DoBazStuff();
}
}
Is there anything I am missing and instead I should actually have something like:
class Foo
{
IoCContainer _c;
public Foo(IoCContainer c) { _c = c; }
...
private void DoBarStuff()
{
var bar = _c.Resolve<IBar>();
bar.DoBazStuff();
}
}
Of course with the second approach I may always to fall back to the first one by passing around a singleton container instance.
EDITED: updated code examples
Neither: both of those approaches hide your dependencies and make your classes hard to use. Instead, Foo
should require an IBar
in its constructor:
class Foo {
private bar;
public Foo(IBar bar) { this.bar = bar; }
private void DoBarStuff() {
this.bar.DoStuff();
}
}
The only things that should know about your container are your application entry points.
See Dependency Injection Myth: Reference Passing and Service Locator is an Anti-Pattern for additional in-depth discussion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With