Roadmap for Windows Phone?

Mar 24, 2010 at 2:50 PM

I understand Prism will be retrofitted for C#/Silverlight 4.0 which obviously means that Unity will be there (maybe even MEF).  Curious if any discussion is going on about Silverlight for Windows Phone of if DI will be left to the ContainerModel in http://mobile.CodePlex.com?

 

Mar 24, 2010 at 4:05 PM

Too early to tell at this point. We're completely heads down getting 2.0 out the door, and then we'll take a deep breath, stick our heads up, and see where we should go next.

Ideally, I'd love to have Unity running on the phone, but we haven't done anything yet to investigate effort, features, or cost of the port. If there even is a port, isn't it just Silverlight?

 

Mar 24, 2010 at 4:35 PM

[ctavares] isn't it just Silverlight?

That was my hope - particularly since Silverlight 4.0 has MEF and I need a DI container for Windows Phone.  However I tried to port over MEF Preview 9 and there were to many missing components to consider continuing.  The ContainerModel built under both Silverlight 4.0 and Windows Phone so I'm about to make it look like Unity for my EHR project.  I'll make the WPF code (Unity 2.0) the source and the Sivlerlight/Phone code linked files.

Mar 24, 2010 at 11:25 PM

I had some discussions with Bob Brumfield - he'd already started looking at this. Phone apparently lacks IL Emit, and as usual Reflection APIs are too slow to be usable.

I'm currently thinking we could keep Unity as long as we used InjectionFactory everywhere. May need to beef it up a little, but it could work and avoid the issues.

But, like I said, it's still way too early to start thinking about that.

Jun 18, 2010 at 1:05 PM

How about now? ;)

Jun 18, 2010 at 5:49 PM

Vision-scope for our phone project just happened yesterday. Team has to finish other projects first. So it'll be a while.

 

Jun 18, 2010 at 11:37 PM
ctavares wrote:

Vision-scope for our phone project just happened yesterday. Team has to finish other projects first. So it'll be a while.

 

 That's great news!   I have a good enough hack for my http://EHR.CodePlex.com to get by until a release so no blocking issue here; just great to hear its coming :)

Jun 19, 2010 at 9:28 AM

Is it the ContainerModel.Mobile that you're referring to?  I just might use that for mine, too. :)

Jun 20, 2010 at 12:08 PM
Edited Feb 9, 2011 at 3:54 PM
MichaelDBang wrote:

Is it the ContainerModel.Mobile that you're referring to?  I just might use that for mine, too. :)

Yes - I took the ContainerModel and wrapped it to look like Unity; I then modified (hacked) it to add functionality that Unity/Prism provides which the ContainerModel lacked, such as Buildup and unity extensions.   I ported Prism to Mobile and have 500+ unit test (ContainerModel, Prism as well as my own) passing which exercise the Unity requirements even more - the only code/test I removed are those that would not compile under the WP7 Mobile framework; assumption being - if it compiles - it will work.

I provide two of my test fixtures below.   Perhaps it might save you some leg work. 

DISCLAIMER:  I hacked the ContainerModel code as required to make it work (may not be pretty at times);  the core requirement was to provide compatibility so that I can code the multi-targeted infrastructure (single code base for WPF, SL4 and WP7) "before" the new Prism is released in Sept and the releases of MEF/Unity for WP7 (I could be waiting a while and want to minimize refactor when they are released).    I had wrote an EHR for a small Medical group and keeping their PDA, Website and Desktop apps in sync was a major task (costly for them).  New EHR certification requirements scraped their project so I get to start from scratch using lessons learned...  

[TestClass]
public class UnityContainerFixture
{
	private IUnityContainer _container;

	[TestInitialize]
	public void Initialize()
	{
		_container = new UnityContainer();
		_container.RegisterType<ILoggerFacade, TextLogger>()
			.RegisterType<IError, ErrorBase>()
			.RegisterType<ILoggerFacade, LoggerBase>()
			.RegisterType<IEventAggregator, EventAggregator>()
			.RegisterType<IServiceLocator, ServiceLocator>();
	}

	public class MockBuildupType 
	{
		public MockBuildupType() { }

		[Dependency]
		public IUnityContainer Container { get; set; }

		[Dependency]
		public IEventAggregator EventAggregator { get; set; }

		[Dependency]
		public IError GWNError { get; set; }

		[Dependency]
		public ILoggerFacade Logger { get; set; }

		private IServiceLocator _serviceLocator;
		public IServiceLocator ServiceLocator { get; set; }

		public MockBuildupType(IServiceLocator serviceLocator)
		{
			_serviceLocator = serviceLocator;

		}
	}

	[TestMethod]
	public void CanBuildupType()
	{
		var buildType = _container.Resolve<MockBuildupType>();
		buildType.EventAggregator = _container.Resolve<IEventAggregator>();
		_container.BuildUp(buildType);

		Assert.IsNull(buildType.Container);
		Assert.IsNotNull(buildType.EventAggregator);
		Assert.IsNotNull(buildType.GWNError);
		Assert.IsNotNull(buildType.Logger);
		Assert.IsNotNull(buildType.ServiceLocator);
	}

	/// <summary>
	/// Determines whether this instance [can resolve registered types].
	/// </summary>
	[TestMethod]
	public void CanResolveRegisteredTypes()
	{
		_container.RegisterType<IMockClassInterface, MockClass>();

		var result1 = _container.Resolve<IMockClassInterface>();
		var result2 = _container.Resolve<IMockClassInterface>();

		Assert.IsNotNull(result1);
		Assert.IsNotNull(result2);
		Assert.IsInstanceOfType(result1, typeof (MockClass));
		Assert.IsInstanceOfType(result2, typeof(MockClass));
		Assert.AreNotEqual(result1, result2);
	}

	[TestMethod]
	public void CanResolveRegisterInstance()
	{
		var mockClass = new MockClass();
		_container.RegisterInstance<IMockClassInterface>(mockClass);

		var result = _container.Resolve<IMockClassInterface>();

		Assert.IsNotNull(result);
		Assert.AreEqual(mockClass, result);
	}

}

[TestClass]
public class GWNContainerFixture
{
	public class MockClass2 : IMockClassInterface { }

	IUnityContainer container = new UnityContainer();

	[TestMethod]
	public void CanRegisterSingletonByTypes()
	{
		// Should register a singleton
		RegisterTypeIfMissing(typeof(IMockClassInterface), typeof(MockClass), true);

		// This should be ignored since it is registered above
		RegisterTypeIfMissing(typeof(IMockClassInterface), typeof(MockClass2), true);

		var instance1 = container.Resolve<IMockClassInterface>();
		var instance2 = container.Resolve<IMockClassInterface>();
		Assert.AreEqual(instance1, instance2);
		Assert.IsTrue(instance1 is MockClass);

	}

	[TestMethod]
	public void CanRegisterByTypes()
	{
		// Should register a singleton
		RegisterTypeIfMissing(typeof(IMockClassInterface), typeof(MockClass), false);

		// This should be ignored since it is registered above
		RegisterTypeIfMissing(typeof(IMockClassInterface), typeof(MockClass2), false);

		var instance1 = container.Resolve<IMockClassInterface>();
		var instance2 = container.Resolve<IMockClassInterface>();
		Assert.AreNotEqual(instance1, instance2);
		Assert.IsTrue(instance1 is MockClass);
	}


	/// <summary>
	/// Registers a type in the container only if that type was not already registered.
	/// </summary>
	/// <param name="fromType">The interface type to register.</param>
	/// <param name="toType">The type implementing the interface.</param>
	/// <param name="registerAsSingleton">Registers the type as a singleton.</param>
	private void RegisterTypeIfMissing(Type fromType, Type toType, bool registerAsSingleton)
	{
		if (registerAsSingleton)
			container.RegisterType(fromType, toType, new ContainerControlledLifetimeManager());
		else
			container.RegisterType(fromType, toType);
	}
}

 

Feb 9, 2011 at 11:09 AM

Can you please provide the current status on this?

Feb 9, 2011 at 4:07 PM
Zsolt wrote:

Can you please provide the current status on this?

 If you are needing Unity for Windows Phone 7 (now) I have a ported version - it actually is the ContainerModel but thinks it is Unity; so much so that it has Prism fooled.   I ported Prism (Nov 2010) over to Phone via TDD.  I had dragged over the Prism unit test, as well as many of the Unity unit test, implemented the IUnityContainer on the ContainerModel and kept on coding until all of the unit test passed.   My Unity "like" container supports setter injection but I did not attempt to address constructor injection (for Phone performance reasons); I updated Prism by providing [Dependency] properties for parameters required by constructor injection.

I just released my multi-targeting Password Manager application on Monday; Silverlight, Desktop and Phone all share the same codebase with the Desktop and Silverlight sharing the same "XAML" (with the exception of resources; which are platform specific).   Source code is available that http://PasswordMgr.CodePlex.com.  Note: this solution has 817 passing unit test (many of which were Prism and Unity).

 

Feb 11, 2011 at 3:58 AM

Blah... it sounds like the Phone is really another major release away before it's really worth developing for (sorry, big fan of constructor injection here :)).  I really hope Microsoft is putting efforts into consolidating the platforms, or even just buying out Novell to get Mono (or building their own Mono altogether). 

Billkrat:  I'll take a gander at your codebase in the meantime to see if there's any interesting tricks to keeping things all in one codebase.  Thanks for this!

Feb 11, 2011 at 10:11 AM
MichaelDBang wrote:

Billkrat:  I'll take a gander at your codebase in the meantime to see if there's any interesting tricks to keeping things all in one codebase.  Thanks for this!


Your welcome, I'd be interested in any tricks/tips you have to offer!

Coordinator
Aug 30, 2013 at 3:23 PM
Aug 2013 Update:

We have released a version of Unity for Windows Phone 8:
http://blogs.msdn.com/b/agile/archive/2013/08/29/unity-di-container-now-supports-windows-phone.aspx

Grigori