Search code examples
c#.net-coredependency-injectionninject.net-4.6

What is the DI equivalent for NInject DI?


I am currently migrating from .net4.6 which uses NInject modules. My target is to use .net6 core and its native DI. While doing so I came across a statement from NInject for which I am wondering if there is an equivalent for the same in .net6 native DI ?

this.Kernel.Bind(typeof(ICachePoolProvider<>)).To(typeof(CachePoolProvider<>));

Should I register for each type of ICachePoolProvider as shown below?

services.AddSingleton<ICachePoolProvider<string>,CachePoolProvider<string>);
services.AddSingleton<ICachePoolProvider<QueueClient>,CachePoolProvider<QueueClient>);
services.AddSingleton<ICachePoolProvider<NamespaceManager>,CachePoolProvider<NamespaceManager>);


Solution

  • I never used NInject but from your code I expect the method you are searching for is:

    services.AddSingleton(typeof(ICachePoolProvider<>), typeof(CachePoolProvider<>));
    

    You can find a good example of an IGenericRepository here