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

拜拜、爱过 提交于 2020-01-07 09:20:11

问题


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

Here are the classes that I am using:

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 ...
    }  
}

Then, I use a Ninject module containing the following code:

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");
        }
    } 
} 

Main program:

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>();
        }
    }  
}

NOTE: The only layer that main program can reference is LayerA where AppService class is located and as well as LayerC where the ninject module is found.


回答1:


Add a Configuration class like this:

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

and in your ninject module write this:

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

then in your main method you could write following:

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

Update:

You can use ninject to locate config class also if you don't want use static methods:

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

in module:

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

in main:

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


来源:https://stackoverflow.com/questions/31624116/how-to-bind-classes-with-required-connection-string-in-constructor-using-ninject

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!