简体   繁体   中英

How to inject dependency to private constructor?

I am working on a module in ASP.net MVC app where I have a singleton class which wants to query from database and then do some processing. I am using repository pattern where my repo is ex: IQueueProcessRepository which I would I want to inject to my singleton classes private constructor.

I am not finding a way to do that. Could someone please explain how to do this?

You could also rectify if what I am trying to do is wrong. Thanks

To answer your question, there is no way. by definition, private constructors are inaccessible by other classes.

If you really need it to be a singleton, bind it in Singleton scope in Ninject. then Ninject will inject your repository into the singleton automatically the first time it's requested.

If you want control over the construction of the singleton, bind using .ToConstant(object) .

var singleton = Kernel.Get<MySingleton>();
Bind<IMySingleton>().ToConstant(singleton);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM