Dependency injection, inject with parameters

前端 未结 5 1577
盖世英雄少女心
盖世英雄少女心 2020-12-08 03:48

I\'m using vNext implementation of DI. How to pass parameters to constructor? For example, i have class:

public class RedisCacheProvider : ICacheProvider
{
         


        
相关标签:
5条回答
  • 2020-12-08 04:42

    You can either provide a delegate to manually instantiate your cache provider or directly provide an instance:

    services.AddSingleton<ICacheProvider>(provider => new RedisCacheProvider("myPrettyLocalhost:6379"));
    
    services.AddSingleton<ICacheProvider>(new RedisCacheProvider("myPrettyLocalhost:6379"));
    

    Please note that the container will not explicitly dispose of manually instantiated types, even if they implement IDisposable. See the ASP.NET Core doc about Disposal of Services for more info.

    0 讨论(0)
  • 2020-12-08 04:42

    You can use like below. Tried to explain with an example.

    Manager class.

    public class Manager : IManager
    {
        ILogger _logger;
        IFactory _factory;
        public Manager(IFactory factory, ILogger<Manager> logger)
        {
            _logger = logger;
            _factory = factory;
        }
    }
    

    Startup.cs class

            public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton<IFactory, Factory>(sp =>
            {
                var logger = sp.GetRequiredService<ILogger<Factory>>();
                var dbContext = sp.GetRequiredService<MyDBContext>();
                return new Factory(dbContext, logger);
            });
            services.AddTransient<IManager, Manager>(sp =>
            {
                var factory = sp.GetRequiredService<IFactory>();
                var logger = sp.GetRequiredService<ILogger<Manager>>();
                return new Manager(factory, logger);
            });
        }
    

    You can read full example here.

    DI in Startup.cs in .Net Core

    0 讨论(0)
  • 2020-12-08 04:43

    A bit late to the party, but you could DI inject a factory that creates and exposes an instance of your provider class.

    0 讨论(0)
  • 2020-12-08 04:45

    You can use :

     services.AddSingleton<ICacheProvider>(x =>
          ActivatorUtilities.CreateInstance<RedisCacheProvider>(x, "myPrettyLocalhost:6379"));
    

    Dependency Injection : ActivatorUtilities will inject any dependencies to your class.

    0 讨论(0)
  • 2020-12-08 04:50

    If the constructur also has dependencies that should be resolved by DI you can use that:

    public class RedisCacheProvider : ICacheProvider
    {
        private readonly string _connectionString;
        private readonly IMyInterface _myImplementation;
    
        public RedisCacheProvider(string connectionString, IMyInterface myImplementation)
        {
            _connectionString = connectionString;
            _myImplementation = myImplementation;
        }
        //interface methods implementation...
    }
    
    

    Startup.cs:

    services.AddSingleton<IMyInterface, MyInterface>();
    services.AddSingleton<ICacheProvider>(provider => 
        RedisCacheProvider("myPrettyLocalhost:6379", provider.GetService<IMyInterface>()));
    
    0 讨论(0)
提交回复
热议问题