Ninject implementation in Xamarin Cross Platform with NullReferenceException
Asked Answered
F

1

6

I am a new happy Xamarin developer and unfortunately I got stuck on my first project problem. When I was working with MVC I used to work with Ninject. So, I decided to use this tool as my IoC and DI in Xamarin project as well. My solution contains IOS project, Android project and PCL for shared datas. In my PCL project I created my NinjectModule (very simple implementation so far..:) )

public class NinjectModuleImplementation : NinjectModule
{
    public override void Load()
    {
        this.Bind<IMapPoint>().To<MapPoint>();
    }
}

And the other static class where I create my container:

public static class Startup
{
    public static StandardKernel Container { get; set; }

    public static void BuildContainer()
    {
        var kernel = new Ninject.StandardKernel(new NinjectModuleImplementation());           
        Startup.Container = kernel;
    }
}

In my native project I call Startup.BuildContainer();

Android:

[Application]
public class App : Application
{
    public App(IntPtr h, JniHandleOwnership jho) : base(h, jho)
    {
    }

    public override void OnCreate()
    {
        Startup.BuildContainer();
    }
}

And iOS

[Register ("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
    public override UIWindow Window {
        get;
        set;
    }

    public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
    {
        Startup.BuildContainer();
        return true;
    }

Now, when I was trying resolve my dependencies "explicitly", there is no problem - it works.

IMapPoint point = Startup.Container.Get<IMapPoint>();

However, when I try inject my dependence by constructor - like that :

public class SomeClass
{
    public static SomeClass Instance { get; private set; }
    public IMapPoint point;

    public SomeClass(IMapPoint _point)
    {
        Instance = this;
        point = _point;
    }
}

NullReferenceException is thrown... What am I doing wrong ? I will be grateful for any suggestions :)

Regards,

Mariusz

Franciscofranciska answered 31/3, 2016 at 21:2 Comment(0)
A
0

Do you have a module per each platform?

Application.cs

[Application(Theme = "@style/Base.Theme", Icon = "@mipmap/ic_launcher")]
public class ConquerApplication : Application
{
    public static IKernel Container { get; set; }

    public ConquerApplication(IntPtr handle, JniHandleOwnership ownerShip)
        : base(handle, ownerShip)
    {
    }

    public override void OnCreate()
    {
        var kernel = new Ninject.StandardKernel(new ConquerModule());

        Container = kernel;

        base.OnCreate();
    }
}

Android Module

public class ConquerModule : NinjectModule
{
    public override void Load()
    {
        //Bind<IThing>().To<Thing>();
        this.Bind<ISQLite>().To<AndroidSQLite>();
        this.Bind<IPersonRepository>().To<PersonRepository>();
        this.Bind<ICycleRepository>().To<CycleRepository>();
        this.Bind<IConquerDatabase>().To<ConquerDatabase>();
        this.Bind<IntroViewModel>().ToSelf().InSingletonScope();
    }
}

There's a fairly simple example of this that you can view here: https://github.com/RobGibbens/Xamarin.IoC/tree/master/NinjectDemo

Aristotelian answered 31/3, 2016 at 21:16 Comment(2)
Hello Jon. My module is stored in PCL Project. But, I have a reference from natives project to Module by static Startup class and prop Container. I thought that it is possible to create one module in PCL... Am I right ? Otherwise, how to use my IoC container in PCL if I will put my module to native project ? PCL doesn't have reference to iOS/Android..Franciscofranciska
I'd recommend trying to follow along the tutorial at the bottom of my answer and see if that helps clarify some items.Aristotelian

© 2022 - 2024 — McMap. All rights reserved.