Almas1997
2008-06-16 07:56:27 UTC
Hi guys,
I have a question about applying of DI in this case.
I wrote a class (its a pseudocode :) and want to create an object in its
some method using container.
class ClassA
{
public ClassA()
{
}
public void SomeMethod()
{
// want to create an instance of classB here using container
}
}
class ClassB : IDisposable
{
}
I could find only two solutions how to implement it.
// 1
class ClassA
{
private PicoContainer pico;
public SomeClass(PicoContainer pico)
{
this.pico = pico;
}
public void SomeMethod()
{
using (var b = pico.getComponent(typeof(classB))
{
}
}
}
In that case i should inject container, but it's anti-pattern according to
the documentation :(
// 2
class ClassA
{
private IClassBFactory factory;
public SomeClass(IClassBFactory factory)
{
this.factory = factory;
}
public void SomeMethod()
{
using (var b = factory.CreateInstance())
{
}
}
}
In that case i can inject class factory for class B. But i want to use a
container to create instances, not factory.
So both my solutions have limitations.
And I'd like to know what will be the most correct solving of this problem.
Thank you in advance.
I have a question about applying of DI in this case.
I wrote a class (its a pseudocode :) and want to create an object in its
some method using container.
class ClassA
{
public ClassA()
{
}
public void SomeMethod()
{
// want to create an instance of classB here using container
}
}
class ClassB : IDisposable
{
}
I could find only two solutions how to implement it.
// 1
class ClassA
{
private PicoContainer pico;
public SomeClass(PicoContainer pico)
{
this.pico = pico;
}
public void SomeMethod()
{
using (var b = pico.getComponent(typeof(classB))
{
}
}
}
In that case i should inject container, but it's anti-pattern according to
the documentation :(
// 2
class ClassA
{
private IClassBFactory factory;
public SomeClass(IClassBFactory factory)
{
this.factory = factory;
}
public void SomeMethod()
{
using (var b = factory.CreateInstance())
{
}
}
}
In that case i can inject class factory for class B. But i want to use a
container to create instances, not factory.
So both my solutions have limitations.
And I'd like to know what will be the most correct solving of this problem.
Thank you in advance.
--
View this message in context: http://www.nabble.com/How-to-apply-DI-to-this-situation-tp17859690p17859690.html
Sent from the NanoContainer - PicoContainer - Users mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email
View this message in context: http://www.nabble.com/How-to-apply-DI-to-this-situation-tp17859690p17859690.html
Sent from the NanoContainer - PicoContainer - Users mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email