How come there's no IKernel implementation in Ninject.Portable
Asked Answered
E

1

5

I use and fancy Ninject alot.

I wonder why there is no "BasicKernel" in Ninject.Portable?

Is implementing IKernel require any call that PCLs don't contain?

I'm talking about simple scenario (about: Bind<If1>().To<Class1>() and Get<If1>() )?

Earthy answered 15/7, 2014 at 11:35 Comment(7)
There's no BasicKernel in the standard ninject either. What are you referring to? StandardKernel?Hansiain
StandardKernel can be found in ninject portable, too: github.com/onovotny/ninject/blob/master/src/Ninject/…Hansiain
i'm refering to this nuget package, nuget.org/packages/Portable.NinjectBulley
yes, me too. github.com/onovotny/ninject is the source code for nuget.org/packages/Portable.Ninject. There is no thing called BasicKernel in any Ninject 3.x version. Portable or not. You can use IKernel (as well as IBindingRoot and IResolutionRoot) with Ninject.Portable. Just do IKernel kernel = new StandardKernel().Hansiain
I've just opened Ninject.Common.dll from lib\portable-net4+sl5+wp8+win8 of the nuget.org/packages/Portable.Ninject and there's no StandardKernel in it. That's why i'm posting this. I know the you cant achieve full support of StandardKernel in PCL. The BasicKernel was just some imaginative name, since its more basic than StandardBulley
You're right. It is contained in Ninject.Platform.dll However the platform dll is not available for portable-net4+sl5+wp8+win8, for all other targets it is. I think something went wrong here...Hansiain
I wrote a comment to github.com/ninject/ninject/pull/65Hansiain
H
7

The PCL version of Ninject is split into two libraries, Ninject.dll and Ninject.Common.dll as some of the code is platform-specific. Ninject cannot exist without its platform code.

In order to use Ninject, you need to add the Portable.Ninject Nuget to both your portable library and to your main app/exe. Adding the package into the main app/exe is what brings in both required files.

As an aside, I'm reworking the package to use the PCL "Bait and Switch" technique, so there's only a single Ninject.dll file. That'll enable you to reference any of it within a PCL, but still requires the NuGet to be referenced by the app/exe to get the "real" implementation instead of a façade.

Husking answered 16/7, 2014 at 12:1 Comment(4)
Thanks. (btw. the last part got cut off!). So this means you can only create the kernel and the bindings in the main application/ex (composition root) which must be a specific platform, not a pcl library. Which makes sense. Can we still create NinjectModule's in our PCL libs?Hansiain
Yes, you an definitely create/have NinjectModules in the PCL.Husking
Oren, I suspect such reasonable explanation. Thanks. I want to ask you next thing, what is suggested pattern for ninject usage with PCL? I use singleton pattern (implemented with Lazy<>) to get IKernel's concrete (StandardKernel). So lets consider this case, in PCL with interfaces (but no StandardKernel) i cant have my singleton, but somehow in my other concretes also in this PCL i should do something like KernelInstance.Get<IMyIface>. I could use IKernel and set the bindings in every application entrypoint. Is this recommended pattern?Bulley
In general, I would try to avoid using an IKernel reference outside of wherever you construct it. That leaks the container into your objects, which is something that's usually best to avoid. Instead, most people rely on constructor injection to get dependencies injected. When your classes use constructor injection, they don't need the kernel reference.Husking

© 2022 - 2024 — McMap. All rights reserved.