Ninject: Injection of dependency IntPtr into param

2019-09-17 05:37发布

问题:

I got help earlier with this question:

Ninject: Error Activating Strings

@nemensv solved that one but I immediatley got a new exception regarding IntPtr. (see activation path below).

Been looking through the ctor here

Only place IntPtr shows up is in this line:

 Result r = Platform.SQLiteApi.Open(databasePathAsBytes, out handle, (int)openFlags, IntPtr.Zero);

How can I solve this using ninject?

Complete exception:

Activation path:

  6) Injection of dependency IntPtr into parameter method of constructor

  5) Injection of dependency BlobSerializerDelegate+SerializeDelegate into parameter serializeDelegate of constructor

  4) Injection of dependency IBlobSerializer into parameter serializer of constructor

  3) Injection of dependency SQLiteConnection into parameter connection of constructor

  2) Injection of dependency ICarRepository into parameter carRepository of constructor

  1) Request for MainViewModel

My code in Ninject:

 Bind<SQLiteConnection>()
                .ToSelf()
                .WithConstructorArgument("databasePath", path);
            Bind<ISQLitePlatform>().To<SQLitePlatformWinRT>();
            Bind<IBlobSerializer>().To<BlobSerializerDelegate>();

Thank you!

EDIT:

This worked for me:

Bind<SQLiteConnection>().ToMethod(ctx => new SQLiteConnection(new SQLitePlatformWP8(), path));

回答1:

As ninject states it can't created an BlobSerializerDelegate because it doesn't know how to instanciated a BlobSerializerDelegate+SerializeDelegate. You either need to tell ninject how to created BlobSerializerDelegate - if that's even necessary - or tell it how to instanciate the SQLiteConnection. I think for the given situtation the ToMethod binding would be best:

Bind<SQLiteConnection>().ToMethod(ctx => new SQLiteConnection(...));

The ToMethod action should just new the SQLiteConnection as you would without a DI container.



标签: c# ninject