Property Injection in Asp.Net Core

前端 未结 3 656
北恋
北恋 2020-12-01 10:31

I am trying to port an asp.net application to asp.net core. I have property injection (using ninject) on my UnitOfWork implementation like this.

[Inject]
pu         


        
3条回答
  •  无人及你
    2020-12-01 11:14

    It's supported with .Nurse Injector: https://github.com/enisn/DotNurseInjector#propertyfield-injection

    You can make it in 3 steps:

    • Install package DotNurse.Injector.AspNetCore
    • Call following method at Program.cs
     public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                    .UseDotNurseInjector() // <-- Add this method
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup();
                    });
    
    • Then you can use [InjectService] attribute instead of constructor injection:
    [InjectService] public IBookRepository BookRepository { get; set; }
    

提交回复
热议问题