Unity with Compact Framework

Apr 8, 2008 at 7:23 PM
Hello!

I have been looking for any clues as to whether Unity is supported by Compact Framework, but have been unable to find any information. What is the status for Unity on .NETCF? We are about to make big changes to an existing embedded MFC application (possibly moving to .NETCF), and I thought this would be a great opportunity to start using an IoC container.
Thanks in advance!
Apr 8, 2008 at 7:24 PM
Unity is not supported on the Compact Framework, and there are no plans to do so. We lean heavily on reflection and dynamic IL generation, which are either VERY expensive or nonexistent on the Compact Framework.
Apr 20, 2008 at 8:24 PM
I read what they did to port Unitiy for SL and sounds easy, not sure if it would be that easy to do the same thing for the CF.
Anyway, one option right now would be to use Object Builder which is part of the Mobile Client Software Factory and you can get at it from the MSDN site http://msdn.microsoft.com/MobileClientFactory/ if you are still working with VS 2005 or from www.codeplex.com/mobileblocks if you need the 2008 version.
Isn't not the same but can suite your needs.

Ctaveres is right, Reflection is expensive in CF, but you can use ObGen to optimize it to do things statically (not everything, but you will get a performance boost)
Nov 5, 2008 at 5:00 PM
Hello

I know that you said you don't plan to support CF, but - just in case - there is a possiility to get rid of reflective invocations using PostSharp to wire up the container at compile time. I've done some experiments with this technique and Micro Framework 3.0: here is my MicroContainer which is the minimal implementation of a DI container targeting MF. Key to success is generating resolving methods during PostSharp post-compilation.

If you are interested I could do some research if transforming Unity to generate build plans during compilation is possible. 
Nov 5, 2008 at 6:24 PM
I would love to play with PostSharp, but it's GPL so MS Legal won't let me anywhere near it.

That's an interesting idea; if you want to go ahead with it, I'm sure that the folks over at the Unity contrib project would be happy to include it.
Nov 6, 2008 at 1:07 PM
You might want to look at Ninject. I know they support CF, not sure about MF
Nov 20, 2008 at 2:36 AM
I highly agree the need for Unity for Compact Framework. Specifically CF2 because this is what comes on devices in ROM currently out in the market.

I don't like the other alternatives, they don't seem as smooth to use as Unity however Unity doesn't seem to support CF for phones, SL etc but some of the others do like Ninject. I like Unity however.
Nov 20, 2008 at 6:48 AM
I really appreciate the enthusiasm for Unity. We'll certainly include the desire for a CF version in our planning going forward. It all depends on how that balances out against everything else we want to do and the cost of implementation.

Unfortunately, like I said above, the CF is missing a major feature (dynamic IL generation) which makes it impossible to port Unity directly. Silverlight does have that feature, so the port was fairly painless. We'd have to rewrite a large chunk of the internals of the container. At that point, you're probably better off building a new, CF specific container. I think that "It's .NET, we can use the same thing on the desktop and on the CF" is a fallacy anyway. The embedded device environment is SO different from the desktop you're better served by specifically targetting that environment rather than trying to shoehorn a desktop library onto it.

Nov 20, 2008 at 11:28 AM
Synced,

First of all. I think Ninject is worth a look.
Also, I know two guys that did 'lightweight containers' for the CF. In case you're interested I can ask them if they're willing to share their code.

Back to the Unity and CF team. I'm not familiar, so I might be wrong, but I think Unity is built on top of Object Builder and there's and implementation of Object Builder for the CF as part of the Mobile Client Software Factory project, you can get a verson that works for 3.5 and 2.0 at www.codeplex.com/mobileblocks.

Ctavares,
I know there're some fundamental pieces for the CF implementation, but the fact that the internals are different doesn't mean that it couldn't be compatible with the .NET version. You're right the environment is SO different and we see that even in the different implementations for controls or streams, but for all the users of those libraries is the same and we can even write common code that targets both platforms. :)
Nov 20, 2008 at 7:35 PM
First off thanks everyone so much for the reply to my post.

Ctavares,

I am fully aware the challenges one faces sometimes when porting to the CF. We are a mobility company who develops several "ports" of our solutions whether it be NET20, NET35, NETCF10 (finally dropping now that devices are coming stock NETCF20 in ROMs), NETCF20, SL20 and using libraries such as Winforms, WPF, etc.

One thing we try to keep in our frameworks is the platform dependency sections easily modular. What we have done is applied a MVP (or MVC for people unaware to the lesser known MVP terminology) design in place with the mindset of the UI toolkits being entirely different on several platforms.

Doing so we are looking to replace some of our current system with a DI, IoC etc better frameworks to leverage. We really liked Unity when looking at it's howtos etc.

That being said, platform compatability is huge for us. Yes we very well know the porting challenges. We deal with it every day. However if its localized to 1 section in your Unity code, then it could be replaced with an alternate method for the CF, while still leveraging all of your Unity framework and APIs?

If so then it gives people like us who are targetting .NET as a overall company strategy 1 DI system for all our technologies.

Otherwise our MVP/MVC systems will need seperate DI frameworks for each platform. I hope you can appreciate the mess that would cause with our developers using several platforms.

Ninject supports pretty much all the .NET platforms I can think of this is true. However I like the Unity syntax and what I saw in the howtos much more. Perhaps I am being picky but so far I am liking the Unity APIs and Microsoft makes it difficult when things don't support their other platforms.

Like I said I am sure I am being overly critical however if one wants to understand why sometimes people choose alternates, these kinds of things sway our choices from one choice to another.

Again thanks so much. I really like the Unity API and hope that I can leverage it sometime in the future.
Nov 20, 2008 at 10:00 PM
Let me ask you to do this: Please create a work item here about making a CF port of Unity. Those who want it, please vote on it.

This is one of the most important ways we track what the community wants as we plan for the future.

-Chris
Nov 21, 2008 at 4:32 AM
Chris,

Thanks so much. I have created the work item. The link can be found at the following:
http://www.codeplex.com/unity/WorkItem/View.aspx?WorkItemId=3350

Anyone interested in Unity on Compact Framework please vote on the work item :)

Thanks and take care!
Nov 25, 2008 at 3:44 AM
I've been doing some experimenting with Ninject and I have to say the IoC/DI situation on the Compact Framework is very poor to say the least. Ninject took 6 seconds on my device to do a kernel.Get(). This is pretty unacceptable.

I know you guys aren't related to that project but I think there can be a true leader here for multiple platforms / architectures with Unity.

As the fellow mentioned earlier, There are articles about obgen or something I believe I read on an older version of the ObjectBuilder I think.
http://msdn.microsoft.com/en-us/library/aa480471.aspx

I don't think IoC/DI has to be runtime dynamic via reflection does it? The concept is to decouple your code so its flexible to change, what happens behind the scenes in IL is not too relevant I believe correct?

Mamadero,

I would be interested to see how that API from someone you know is. An IoC/DI cannot take 6 seconds like I have found in Ninject. JIT'ing already takes awhile to load a NETCF application, adding 6 seconds per service your trying to locate is making it way worse!

Thanks everyone for listening.
Nov 25, 2008 at 5:27 AM
Hi,

This is amazing. 6 seconds is unacceptable. I've used Ninject only on Silverlight without performance issues. I saw you raised that question on the Ninject group, so I hope someone there can help with this matter.

I know of two more IoC's for the .NET CF. I've not used any of those.
1. Coincedentally I'm attending to an event today and I'm expecting to see the guy that mentioned me about the lightweight framework. I'll ask him.
2. I know another person who built something similar. I e-mail him the reference to this thread.

Have you tried OB? It didn't have the best performance, but it wasn't as bad as you mention Ninject is.
Nov 26, 2008 at 11:09 PM
Edited Nov 26, 2008 at 11:11 PM
Synced,

                I just public the project mamadero is talking about. The page is: http://www.codeplex.com/compactcontainer . If you have any questions, please contact me.

Regards,

Mariano Vicario

Mar 19, 2009 at 7:16 PM
I'm using Mariano's DI container and am starting to tweak it to look like Unity http://mobile5framework.codeplex.com/