Unity lazy loading of assemblies for large applications

Oct 8, 2009 at 6:30 AM

Hi,

We have been using ObjectBuilder1 with our own custom container and mapping file for a while now and are thinking about moving over to Unity.

One of the problems we fixed with our implementation was to not load assembly type mappings until that assembly is actually needed by the runtime, this is because our application is getting pretty large and loading all the assemblies up front is heavy on memory usage especially if the users were not going to be using that assembly.

In unity I cannot figure out how to prevent the loading of an assembly up front and for unity to only register the type mappings when the interface types are requested.

Does anyone know how we can prevent assemblies being loaded unnecessarily with unity?

Dec 18, 2009 at 12:38 AM

Unity is highly Type object based, and getting a Type object requires that the assembly defining it be loaded. You could probably do some trickery by deriving from Type, but that's some pretty serious low-level work and I'm not sure how it would work.

You might want to consider MEF for your design instead of Unity to managing these "on-demand loaded" kinds of types, it's very good at this sort of thing.

 

Dec 18, 2009 at 4:49 AM

I managed to create a work around to the problem, by using a custom configuration file for storing the mappings (not as flexible as unit'y's configuration file) and listening to the assembly load event, when and assembly loads I check to see if the custom configuration file contains any types in the loading assembly and if so I register the mappings (loading the paired assembly if need be)

This way if a user does not use a section of our application they assemblies are not loaded and no mappings are created (we are using SCSF)