Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li><p>Here is the application that I'm creating. It is open source and available on github, and utilizes all of the required stuff - MVC3, NUnit, Moq, Ninject - <a href="https://github.com/alexanderbeletsky/trackyt.net/tree/master/src" rel="noreferrer">https://github.com/alexanderbeletsky/trackyt.net/tree/master/src</a></p></li> <li><p>Contoller-Repository decoupling is simple. All data operations are moved toward the Repository. Repository is an implementation of some IRepository type. The controller never creates repositories inside itself (with the <code>new</code> operator) but rather receives them either by constructor argument or property.</p></li> </ol> <p>.</p> <pre><code>public class HomeController { public HomeController (IUserRepository users) { } } </code></pre> <p>This technique is called "Inversion of Control." To support inversion of control you have to provide some "Dependency Injection" framework. Ninject is a good one. Inside Ninject you associate some particular interface with an implementation class:</p> <pre><code>Bind&lt;IUserRepository&gt;().To&lt;UserRepository&gt;(); </code></pre> <p>You also substitute the default controller factory with your custom one. Inside the custom one you delegate the call to the Ninject kernel:</p> <pre><code>public class TrackyControllerFactory : DefaultControllerFactory { private IKernel _kernel = new StandardKernel(new TrackyServices()); protected override IController GetControllerInstance( System.Web.Routing.RequestContext requestContext, Type controllerType) { if (controllerType == null) { return null; } return _kernel.Get(controllerType) as IController; } } </code></pre> <p>When the MVC infrastructure is about to create a new controller, the call is delegated to the custom controller factory GetControllerInstance method, which delegates it to Ninject. Ninject sees that to create that controller the constructor has one argument of type <code>IUserRepository</code>. By using the declared binding, it sees that "I need to create a UserRepository to satisfy the IUserRepository need." It creates the instance and passes it to the constructor.</p> <p>The constructor is never aware of what exact instance would be passed inside. It all depends on the binding you provide for that.</p> <p>Code examples:</p> <ul> <li><a href="https://github.com/alexanderbeletsky/trackyt.net/blob/master/src/Web/Infrastructure/TrackyServices.cs" rel="noreferrer">https://github.com/alexanderbeletsky/trackyt.net/blob/master/src/Web/Infrastructure/TrackyServices.cs</a> <a href="https://github.com/alexanderbeletsky/trackyt.net/blob/master/src/Web/Infrastructure/TrackyControllerFactory.cs" rel="noreferrer">https://github.com/alexanderbeletsky/trackyt.net/blob/master/src/Web/Infrastructure/TrackyControllerFactory.cs</a> <a href="https://github.com/alexanderbeletsky/trackyt.net/blob/master/src/Web/Controllers/LoginController.cs" rel="noreferrer">https://github.com/alexanderbeletsky/trackyt.net/blob/master/src/Web/Controllers/LoginController.cs</a></li> </ul>
 

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