Skip to content

Dependency Injection Ninject

Daniel edited this page Aug 16, 2020 · 4 revisions

CodeMonkeys.DependencyInjection.Ninject

Nuget

The CodeMonkeys.DependencyInjection.Ninject package is our framework abstraction for the widely known Ninject dependency injection container.

How to work with it?

Using the abstraction is pretty straightforward. You just need to call the factory method CreateInstance to get a container instance. Like this:

// Will return a instance which uses DryIoc internally
var container = NinjectFactory.CreateInstance();

// Will return a instance which uses DryIoc internally and writes log messages with the given logger
var logger = LogServiceFactory.Instance.Create(nameof(Program));
var container2 = NinjectFactory.CreateInstance(logger );

Important: By default, we disable the use of System.Reflection.Emit because on iOS it will not work. The linker is striping the entire namespace when building.

You can control this behavior with the optional parameter allowReflectionEmit:

CreateInstance(
    bool allowReflectionEmit = false)

CreateInstance(
    ILogService logService, 
    bool allowReflectionEmit = false)
Clone this wiki locally