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.