DryIoc icon indicating copy to clipboard operation
DryIoc copied to clipboard

DryIoc.Syntax.Ninject package and the Ninject migration FAQ

Open dadhi opened this issue 4 years ago • 17 comments

Faster drop-in Ninject replacement keeping the API surface intact on top of the DryIoc implementation.

Similar to the DryIoc.Syntax.Autofac.

dadhi avatar Sep 24 '20 17:09 dadhi

Ninject Module

Reference doc: https://github.com/ninject/Ninject/wiki/Modules-and-the-Kernel#modules

Ninject example:

public class WarriorModule: NinjectModule
{
    public override void Load() 
    {
        Bind<IWeapon>().To<Sword>();
        Bind<Samurai>().ToSelf().InSingletonScope();
    }
}

A similar DryIocModule may be approached like this:

public abstract class DryIocModule
{
    public IRegistrator R { get; set };
    public abstract void Load(); 
}

public class WarriorModule: DryIocModule
{
    public override void Load() 
    {
        R.Register<IWeapon, Sword>();
        R.Register<Samurai>(Reuse.Singleton);
    }
}

// Registering the modules. You may provide the assemblies instead of implementation types.
container.RegisterMany(
    implTypes: new[] { typeof(WarriorModule, /* others */ ) }
    made: PropertiesAndFields.Of.Name(nameof(DryIocModule.R)));

dadhi avatar Oct 15 '20 06:10 dadhi

I wish to replace my NInject configuration. Can I try DryIocModule for size or do I have to wait for a future release ?

sigmarsson avatar Dec 22 '20 16:12 sigmarsson

You may do it know. Even better if you have some problems I may help with improvements, or at least track this here for documentation and future improvements.

dadhi avatar Dec 22 '20 17:12 dadhi

I can't find this package even among pre-releases in Nuget.

sigmarsson avatar Dec 22 '20 17:12 sigmarsson

Huh, it is not ready yet (I am just starting). I mean that you can grab the code from example and start experimenting with it and I may help with questions, etc.

dadhi avatar Dec 22 '20 18:12 dadhi

No stress. Thanks for offering your help. Please estimate your package release plan and I will test it.

sigmarsson avatar Dec 23 '20 08:12 sigmarsson

Hello @dadhi

4.7 is the one shipping this?

sigmarsson avatar Jan 08 '21 11:01 sigmarsson

No, there is no specific ETA yet. I am using the Ninject on my production project, so I am still planning to work on this.

dadhi avatar Jan 08 '21 13:01 dadhi

I need some help/suggestions on how can i make migrate from Ninject which is used in my project to DryIOC for Dotnet Framework. I tried but unable to handle Kernel part. @dadhi @sigmarsson @wjrogers

jainashish1711 avatar Jan 28 '21 12:01 jainashish1711

@jainashish1711

What's your setup or the example code?

dadhi avatar Jan 28 '21 12:01 dadhi

Please find sample Startup.cs class below

public partial class Startup { public static HttpConfiguration HttpConfiguration; public void Configuration(IAppBuilder app) { app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

        var kernel = CreateNinjectKernel();
        app.UseNinjectMiddleware(() => kernel);
        app.UseOwinExceptionHandler();

        ConfigureAuth(app, kernel);
        ConfigureWebApi(app);

        GlobalConfiguration.Configuration.Formatters
            .JsonFormatter.SerializerSettings.ReferenceLoopHandling =
            Newtonsoft.Json.ReferenceLoopHandling.Ignore;
    }

    public IKernel CreateNinjectKernel()
    {
        return new StandardKernel(
            NinjectBaseDomainModule.Instance,
            new NinjectDISDomainModule(),
            new NinjectBaseRepositoryModule(),
            new NinjectDISRepositoryModule(),
            new NinjectWebModule());

    }

    private static void ConfigureWebApi(IAppBuilder app)
    {
        HttpConfiguration = new HttpConfiguration();
        WebApiConfig.Register(HttpConfiguration);
        app.UseNinjectWebApi(HttpConfiguration);
    }
}

Also having trouble binding for these type of codes: Bind<ILockingService>().To().WhenInjectedInto();

@dadhi

jainashish1711 avatar Jan 28 '21 13:01 jainashish1711

Here there is a lot of things to digest because you're using Web extensions.

I won't answer it right away, maybe I look later into it when I need to convert my own project. Or you may dig it yourself, and asking some concrete questions when you stuck.

Regarding WhenInjectedInto...

WhenInjectedInto

The example may be translated like this:

Ninject:

Bind<IInterface>().To<ClassA>()
    .WhenInjectedInto<ClassB>();

DryIoc:

container.Register<IInterface, ClassA>(
    setup: Setup.With(condition: r => r.Parent.ImplementationType == typeof(ClassB)));

dadhi avatar Jan 29 '21 07:01 dadhi

Hello @dadhi , has it bogged down, cancelled or just starving of human resource ? Will you introduce modules as NInjectModule works, so that IoC configurations could be reusable ?

sigmarsson avatar Oct 14 '21 10:10 sigmarsson

@sigmarsson

Yes, now it is time/hr needed. Plus to start things up I would love to see a sample project or tests with minimal useful set of Ninject API.

I had a big project myself before, but no longer have it now. And eating a big elephant is harder than a small rabbit..

Still I am poking random people in Ninject issues what features do they like and using, etc. Almost no life is there :(

dadhi avatar Oct 14 '21 13:10 dadhi

@dadhi , Then let them go. Maybe DryIoc could just copy the entire NinjectModule concept as you drew up earlier;

public class WarriorModule: DryIocModule
{
    public override void Load() 
    {
        R.Register<IWeapon, Sword>();
        R.Register<Samurai>(Reuse.Singleton);
    }
}

What is registered in this method, is so appearing in the Kernel into which the respective module will be injected. No magic, just it shall provide a single purpose; reuse the configuration.

sigmarsson avatar Oct 21 '21 11:10 sigmarsson

@dadhi is it yet on your road map ?

sigmarsson avatar Jul 05 '22 09:07 sigmarsson

Open question. I am currently not working with NInject myself. So it is hard to get with MVP number of features and the examples to convert. So any help here is appreciated.

dadhi avatar Jul 05 '22 09:07 dadhi