I started to use Ninject , on this relatively small project and i have run into a problem: i have this class
class SomeService : ISomeService
that depends on
class BizLogicModule : IBizLogicModule
that in turn depends on
class DataRepository : IDataRepository
the DataRepository
has a ctor that looks like:
DataRepository(BizEntityModel context)
Now, i need to be able to use a single instance of BizEntityModel
across more than one IDataRepository
instance.
I also need to create IDataRepository
's along the life of a IBizLogicModule
. The IBizLogicModule
does not know about Ninject and i want to keep it that way.
so my problem is: how to wire all that up, using the Ninject kernel, while:
not having to pass the kernel instance around the layers.
leaving the code readable close to what it was prior Ninject (i was just new'ing using a factory method).
The simple part of the wiring i got so far is:
Bind<SomeService>().To<ISomeService>();
Bind<BizLogicModule>().To<IBizLogicModule>();
Bind<DataRepository>().To<IDataRepository>();
Bind<BizEntityModel>().To<BizEntityModel>(); //ToSelf()
// .WithConstructorArgument(context => Kernel.Get<BizEntityModel>)
Your guidance is very much appreciated
EDIT: Thanks for your answers!
here's some more data that was requested:
BizEntityModel
is registered with Ninject (code updated).
if i understand correctly: i can create instances of IDataRepository
in IBizLogicModule
using a 'factory method'. but that leaves me with:
1) i need to pass a BizEntityModel
to the factory method, some times its bran new and sometimes its an existing instance. using the factory method, it will create anew one every time.
2) is this a problem that SomeService
is in another assembly, and only it has a ref to Ninject.dll ?