dadhi / DryIoc

DryIoc is fast, small, full-featured IoC Container for .NET
MIT License
1.02k stars 124 forks source link

DryIoc.Syntax.Ninject package and the Ninject migration FAQ #326

Open dadhi opened 4 years ago

dadhi commented 4 years ago

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

Similar to the DryIoc.Syntax.Autofac.

dadhi commented 4 years ago

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)));
sigmarsson commented 3 years ago

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

dadhi commented 3 years ago

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.

sigmarsson commented 3 years ago

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

dadhi commented 3 years ago

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.

sigmarsson commented 3 years ago

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

sigmarsson commented 3 years ago

Hello @dadhi

4.7 is the one shipping this?

dadhi commented 3 years ago

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.

jainashish1711 commented 3 years ago

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

dadhi commented 3 years ago

@jainashish1711

What's your setup or the example code?

jainashish1711 commented 3 years ago

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().To().WhenInjectedInto();

@dadhi

dadhi commented 3 years ago

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)));
sigmarsson commented 3 years ago

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 ?

dadhi commented 3 years ago

@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 :(

sigmarsson commented 3 years ago

@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 commented 2 years ago

@dadhi is it yet on your road map ?

dadhi commented 2 years ago

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.