Almost all of the Ninject examples I've seen explain how to use it with ASP.NET MVC, which will automatically inject dependencies into controllers. How can I use Ninject manually? Let's say I have a custom ActionResult:
public class JsonResult : ActionResult
{
[Inject] public ISerializer Serializer { get; set; }
public JsonResult(object objectToSerialize)
{
}
}
Then in my controller I use JsonResultin a method like this:
public ActionResult Get(int id)
{
var someObject = repo.GetObject(id);
return new JsonResult(someObject);
}
As you can see, I myself create an object that avoids the Ninject injection, but Serializerwill be null. However, doing this as follows does not seem completely correct to me:
public ActionResult Get(int id)
{
var someObject = repo.GetObject(id);
return IoC.Kernel.Get<JsonResult>(someObject);
}
Ninject, ββNinject /singleton , , , .
- Ninject , , ? new, .