In Ninject there's automatic implicit self-binding for concrete types. So without further configuration I can resolve every type in my application, like:
Foo foo = Kernel.Get(typeof(Foo));
Now if I need an Array of Foo
, how would I do that?
Foo[] foos = Kernel.Get(typeof(Foo[])); // does not work
EDIT:
For clarification, here is, what I'm actually trying to achieve: In an ASP.NET MVC application I have an AutoMapViewResult (like Jimmy Bogard is talking about in this great(!) video: http://www.viddler.com/explore/mvcconf/videos/1/ or in ASP.NET MVC 2 in Action). The difference is, that I need to inject some service to the constructor of my view model before mapping the entity to it with AutoMapper. If the source type is an array I also must instantiate an array of the view model.
So here finally some (simplified) code :-)
public class EventsEditModel
{
// some properties here
public Location[] Locations { get; set; }
public EventsEditModel(ILocationService locationService)
{
Locations = locationService.GetAll().ToArray();
}
}
public class EventsListModel
{
// some properties here
}
and here my AutoMapViewResult:
public class AutoMapViewResult : ViewResult
{
public AutoMapViewResult(object model, Type sourceType, Type destinationType)
{
if (model != null)
{
var viewModel = IoC.Resolve(destinationType);
ViewData.Model = Mapper.Map(model, viewModel, sourceType, destinationType);
}
}
}
This works great with EventsEditModel
. My Index view requires an EventsListModel[]
, so I need to instantiate an array, which throws a System.ArgumentNullException
: Value cannot be null. Parameter name: source (in the line var viewModel = IoC...
)
Note: IoC.Resolve(Type serviceType)
is just a wrapper for Kernel.Get(Type serviceType)