Ninject, Providers and Activator.CreateInstance

I'm new to Ninject, but I managed to use it for DI using a custom provider.

The binding is initialized as follows

kernel = new StandardKernel();

kernel.Bind<IPatientRecordLocator>().ToProvider<PatientRecordLocatorProvider>();

and in the user provider I call Activator.CreateInstance like this

protected override IPatientRecordLocator CreateInstance(IContext context)
{
    var name = ConfigurationManager.AppSettings["PatientRecordLocator"];
    var typeName = name.Split(',')[0];
    var assemblyName = name.Split(',')[1];
    return Activator.CreateInstance(assemblyName, typeName).Unwrap() as IPatientRecordLocator;
}

(yes, I know that there is no error handling in the code, etc. :))

and it all works like a charm.

Now the problem I am facing is when I introduce a new class that I want to inject into IPatientRecordLocator instances. The problem occurs when I add a constructor similar to the following, for example, one of these classes

[Inject]
public MockPatientRecordLocator (IContactAdapter contactAdapter)
{
    ...
}

Then, for Activator.CreateInstance to work, I also need to add a constructor without parameters to the MockPatientRecordLocator class, i.e.

public MockPatientRecordLocator() 
{
}

, : Ninject , IContactAdapter , . MockPatientRecordLocator? , .

, , , , PatientRecordSummary MockPatientRecordLocator ( ), MockPatientRecordLocator IContactAdapter ( , ( )). , - .

+5
1

!

Bind(Type), , Load(), - , (.. Type) . Ninject ( - .ctor)

IIRC / , .

( [Inject], )

+1

All Articles