After reading the new documentation on NInject v3 and how to use the Factory Extension, apparently I still don't get it fully since my code throws exceptions all over the place...
I get this Exception, i could paste the whole thing if people would like that but i'll try and keep it short for now.
Error activating IDeployEntityContainer No matching bindings are available, and the type is not self-bindable.
Here is my code... The Ninject Bind Module class
class MyNinjectModule : NinjectModule {
public override void Load() {
...
Bind<IDeployEntityFactory>().ToFactory();
Bind<IDeployEntityContainer>().To<DeployEntityContainer>();
...
}
}
The class which uses the factory
class DeployController : IDeployController {
private readonly IDeployEntityFactory _entityFactory;
public DeployController(..., IDeployEntityFactory entityFactory) {
...
}
public void Execute() {
...
//I get the Exception on this line...
_entityFactory.GetDeployEntity<IDeployEntityContainer>();
...
}
}
Factory Interface
public interface IDeployEntityFactory
{
T GetDeployEntity<T>();
}
The Factory Implementation
public class DeployEntityFactory : IDeployEntityFactory
{
private readonly IResolutionRoot _resolutionRoot;
public DeployEntityFactory(IResolutionRoot resolutionRoot)
{
_resolutionRoot = resolutionRoot;
}
public T GetDeployEntity<T>()
{
return _resolutionRoot.Get<T>();
}
}
Behind the scenes Ninject will create a proxy that implements the specified factory interface and intercept all methods so that the proxy behaves like...
I understand that I don't have to actually create the implementation my self if i don't need to do something special/custom in the creation of objects inside the factory.
Source: http://www.planetgeek.ch/2011/12/31/ninject-extensions-factory-introduction/
EDIT1:
Just to make sure i leave you with every bit of information you need to see the problem, i'm adding the DeployEntityContainer class/Interface
public abstract class DeployEntityBase : IDeployEntity
{
...
protected readonly IDeployEntityFactory _entityFactory;
protected DeployEntityBase(..., IDeployEntityFactory entityFactory)
{
...
_entityFactory = entityFactory;
...
}
...
}
public class DeployEntityContainer : DeployEntityBase, IDeployEntityContainer
{
...
public DeployEntityContainer(..., IDeployEntityFactory entityFactory)
: base(..., entityFactory)
{
}
}
I ended up just changing the bindings to normal bindings,
and it worked! My first thought was lol, but it makes sense as well.
With the
ToFactory()
binding it never ever used my implementation of the factory, it just generated one from the defined interface.Now it uses my implementation. The factory is changed a bit: From newing up the kernel in the factory or injecting it in the constructor, now I inject
IResolutionRoot
whichGet<T>();
my objects.Here is the new code just for clarification.
If this is not the right way to do it, I hope somebody can shed light on it and notify me with the right way... I imagine @remogloor would know such a thing. :)