Question

I am building out a basic ninject container to replace phone container in Caliburn.Micro as I want to stick with one IoC container for multiple projects. Plus its fun...

One of the methods is register with phone services which looks like this:

 public void RegisterPhoneServices(bool treatViewAsLoaded = false)
    {
        var _phoneService = new PhoneApplicationServiceAdapter(_rootFrame);
        var _navigationService = new FrameAdapter(_rootFrame, treatViewAsLoaded);

        _kernel.Bind<INavigationService>().ToMethod(x => _navigationService);
        _kernel.Bind<IPhoneService>().ToMethod(x => _phoneService);

        _kernel.Bind<IEventAggregator>().To<EventAggregator>().InSingletonScope();
        _kernel.Bind<IWindowManager>().To<WindowManager>().InSingletonScope();

        _kernel.Bind<TaskController>().ToSelf().InSingletonScope();

        var _taskController = Kernel.Get<TaskController>();
        _taskController.Start();
    }

The part I do not understand is why the INavigationService and the IPhoneService are binded to anonymous methods like they are, rather than a standard binding.

I THINK the reason is because this is the correct way to bind to an object that you already have defined an instance for (as is the case in the lines above) but I hate seeing code and not knowing what it does. Am I correct in my assumption?

Was it helpful?

Solution

deanvmc,

in short: yes, you're right.

you're using a Ninject factory method (see http://bit.ly/AB9Eg9). The factory could return any kind of INavigationService in your case you're simply choosing to return your existing PhoneApplicationServiceAdapter

Cheers!

OTHER TIPS

A better way to do this would be to use _kernel.Bind<INavigationService>().ToConstant(_navigationService) This will allow the binding to be cached as opposed to having to call a Func which just returns the same object every time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top