如何使用 Ninject 在构造函数中将 类 与所需的连接字符串绑定?

How to bind classes with required connection string in constructor using Ninject?

如何使用 Ninject 在构造函数中将 classes 与所需的连接字符串绑定?

这是我正在使用的 classes:

AppService class:

using SomeProject.LayerB;

namespace SomeProject.LayerA;
{
    public class AppService
    {
        private readonly ISomeRepository someRepository;

        public LocationManagementService(ISomeRepository someRepository)
        {
            this.someRepository = someRepository;
        }

        // other codes ...
    }  
}

SomeRepository class:

namespace SomeProject.LayerB;
{
    public class SomeRepository : ISomeRepository
    {
        private readonly SomeDbContext context;

        public SomeRepository(SomeDbContext context)
        {
            this.context = context;
        }

        // other codes ...
    }
}

SomeDbContext class:

namespace SomeProject.LayerB;
{
    public class SomeDbContext : DbContext
    {
        public SomeDbContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {
        }

        // other codes ...
    }  
}

然后,我使用包含以下代码的Ninject模块

namespace SomeProject.LayerC;  
{
    public class SomeModule : NinjectModule
    {
        public override void Load()
        {
            Bind<ISomeRepository>().To<SomeRepository>();

            // My problem is on this part.. I want to provide the connection string on the
            // main program, not here on this class.
            // Bind<SomeDbContext>().ToSelf().WithConstructorArgument("nameOrConnectionString", "the connection string I want to inject");
        }
    } 
} 

主程序:

using SomeProject.LayerA;
using SomeProject.LayerC;

namespace SomeProject.LayerD;
{
    public class MainProgram
    {
        public MainProgram()
        {
            IKernel kernel = new StandardKernel(new SomeModule());

            AppService appService = kernel.Get<AppService>();
        }
    }  
}

注意:主程序唯一可以引用的层是AppServiceclass所在的LayerA和ninject模块所在的LayerC成立。

像这样添加配置class:

public class Config
{
    public static string ConnectionString { get; set; }
}

并在您的 ninject 模块中写入:

Bind<SomeDbContext>().ToSelf()
   .WithConstructorArgument("nameOrConnectionString", 
       c => Config.ConnectionString);  

然后在你的主要方法中你可以这样写:

public class MainProgram
{
    public MainProgram()
    {
        IKernel kernel = new StandardKernel(new SomeModule());
        Config.ConnectionString = "The connection string";
        AppService appService = kernel.Get<AppService>();
    }
}  

更新:

如果您不想使用静态方法,您也可以使用 ninject 来定位 config class:

class Config2
{
    public string ConnectionString { get; set; }
}

在模块中:

Bind<Config2>().ToSelf().InSingletonScope();
Bind<SomeDbContext>().ToSelf()
   .WithConstructorArgument("nameOrConnectionString",
       c=>c.Kernel.Get<Config2>().ConnectionString);

主要内容:

IKernel kernel = new StandardKernel(new SomeModule());
var conf = kernel.Get<Config2>();
conf.ConnectionString = "The connection string";
AppService appService = kernel.Get<AppService>();