Note that there are some explanatory texts on larger screens.

plurals
  1. POCan Ninject resolve abstract dependencies after the object is initialised?
    text
    copied!<p>Does anyone know if it's possible to use Ninject to resolve any unresolved abstract dependencies outside of the instantiation process? I've just been looking into constructor injection vs property/method/field injection, but it looks to me as though Ninject is still expecting to be the creator of the type using the IKernel.Get&lt;>() method.</p> <p>Basically, we're using MVC3 to build our product, and we've come up against a situation where we want the default ModelBinder to map form values to an instance of the object, and then be able to call a method on the submitted ViewModel that is dependent on an abstract interface e.g.</p> <pre><code>public class InviteFriend { [Required] public string EmailAddress { get; set; } public void Execute() { var user = IUserRepository.GetUser(this.EmailAddress); if (user == null) { IUserRepository.SaveInvite(this.EmailAddress); } MailMessage toSend = new MailMessage(); // Obviously some logic to prepare the body, subject and other mail properties SmtpClient.Send(toSend); } } </code></pre> <p>where the controller action would receive InviteFriend as the method argument. We want Ninject to be able to resolve that IUserRepository dependency, but I can't quite work out how to since the object itself is instantiated by the MVC ModelBinder rather than Ninject IKernel.Get&lt;>().</p> <p>Maybe the solution is a Ninject-based ModelBinder, or does that seem a really bad idea?</p> <p><strong>EDIT TO ADD:</strong> After the comments below, I realise that my hastily mocked-up code sample doesn't really reflect what we're facing. I've updated the code sample to reflect that the logic for InviteFriend.Execute() is more complex than just calling a method on one repository. Potentially, this is logic representing a discrete task that could co-ordinate interactions between multiple different domain objects and multiple repositories. The repositories are defined abstractly, and ideally would be resolved by Ninject.</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