Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create a new instance of a class with Ninject, inside of a dependency project? (not in the bootstrapped project)
    text
    copied!<p>I'm working on a project that has multiple layers of architecture. These layers are in their own class library projects, but all in the same solution. At the lower layers, I've got the repository pattern going, with my models, repositories and dbcontext. On top of my repositories sits a service layer, which contains all of my business logic. Right now, my code for instantiating and using these various components looks like this, in my unit tests:</p> <pre><code>using (var context = m_kernel.Get&lt;IPortalContext&gt;()) { var accountRepository = m_kernel.Get&lt;IAccountRepository&gt;(new ConstructorArgument("context", context)); var eventRepository = m_kernel.Get&lt;IEventRepository&gt;(new ConstructorArgument("context", context)); var accounts = m_kernel.Get&lt;IAccountService&gt;(new ConstructorArgument("repository", accountRepository)); var events = m_kernel.Get&lt;IEventService&gt;(new ConstructorArgument("repository", eventRepository)); var account = accounts.GetByUsername("test@test.com"); } </code></pre> <p>You can see I create a context, then I create my repositories and give them the existing context, finally I create my services and give them the repository. Then, I am free to use the services.</p> <p>That code is very verbose, though. I can make it cleaner by creating the repository instances inside of the services, instead of outside.</p> <p>My confusion comes while trying to do this, though. I use Ninject for my DI, and I want to create an instance of the class bound to the <code>IAccountRepository</code> interface inside of my <code>IAccountService</code> constructor. On top of that, it needs to know to use the provided <code>IPortalContext</code> as well.</p> <p>Do I have to add Ninject as dependency to my service layer project and create a new Kernel in there and use Kernel.Get() inside for the <code>IAccountService</code> constructor? Or is there a way to tell Ninject to do this all from my unit test, for example?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload