Friday, March 18, 2011

Silverlight: MEF and other approaches

Hi all.

Today I'd like to concentrate to the discussion about the MEF: a relatively new framework for dynamically loading Silverlight content.
It was amazing from the first point of view, and we have decided to use it. The process started.
Of course it's easy: use some simple attributes, mark the exportable types you want in your .xap packages with those attributes and that's it - you'll have them whenever you'll need them, and everything will be ok.
Yes, all this is right, BUT ...
There is a big BUT about this MEF: when you application will come a bit bigger - and it will come to this stage, because you are already using MEF, so that was your intention to avoid loading such a big application, you'll find out that instead of downloading each library/assembly only once from the server, you'll download it as many times, as you have it in each downloadable .XAP.
This became a big problem, when my app with all those xaps grown to ~14Mb. What to do?
Of course there is always a better way to go. And here one, which I have implemented and is satisfied with:
The .Net Framework is such a good thing :) I have created a simple attribute, which was aimed to mark the assembly references for the assembly it's applied to. And the second part, is that I've created a small asynchronous Assembly Loader, which is using this information from the described attribute, and downloads all those dependencies, when you ask it to download an assembly. If this assembly is already downloaded, you'll just get that instance, otherwise it will download it, it's references, and when everything is ready to be used, it fires this amzaing "AssemblyLoaded" event, which to I was able to react. So finally, avoiding this dll duplication (another .dll hell) I've reduced the size of the app from 13.6Mb to 3.8Mb. And one more thing - it works excellent and much faster.