Resolving SimpleContainer object from another class

Mar 31, 2011 at 10:34 PM

I'm working on a WP7 app and using SimpleContainer as my IoC. I'm trying to figure out how to get a reference to an object that I'm registering in my bootstrapper:

public class AppBootstrapper : PhoneBootstrapper
{
	PhoneContainer container;

	protected override void Configure()
	{
		container = new PhoneContainer(this);

		container.RegisterSingleton(typeof(EventAggregator), "EventAggregator", typeof(EventAggregator));

		AddCustomConventions();
	}
}

I'd like to be able to grab the EventAggregator in the constructor of one of my model objects so that I can publish messages to it. (I know how to use constructor injection, but I think it would make life difficult in this case because this object gets stored in a list, which gets serialized into XML...)

But I can't figure out which object to use to resolve items from the container by name. The documentation on this is geared toward MEF examples. Any suggestions?

Thanks!

Coordinator
Apr 1, 2011 at 1:10 PM

With the simple container, you can't resolve a named component via ctor injection. The only way to get it is to pull it explicitly from the container using IoC.GetInstance(). If you want to use injection with named components, you should investigate a full fledged IoC container or use MEF.

Apr 1, 2011 at 1:59 PM

That will work perfectly. I don't want to inject a named component--just get a reference to it while the constructor is executing. The IoC.GetInstance() method seems like it should do the trick.