Search code examples
mvvmmefioc-containercaliburn.micro

Resolve Views through IoC or MEF instead of using SelectedAssemblies() method


I use Caliburn.Micro with Spring.net instead of the default simple IoC. My custom Bootstrapper (derrived from Caliburn's BootstrapperBase) is working and I can define the ViewModels within Spring.net. But the the Views are still resolved by reflection (name convention) from the execution assembly. I used the following method of the Bootstrapper to add Assemblies for resolving the Views for the ViewModels.

    protected override IEnumerable<Assembly> SelectAssemblies()
    {
        // hmm, want to change the way how the view is resolved... how to do this?
        // ... use IoC or MEF for this task instead?
        return new[]
                   {
                       // don't want to add every dll here
                       this.GetType().Assembly,
                       Assembly.Load("MyViewModels.Assembly") 
                   };
    }

How to change the behaviour of resolving views and using IoC or MEF for this task? The Problem is that the Bootstrapper has no virtual method to override which resolves a requested view. What is the starting point to change this behaviour? I thought there must exist something like

protected virtual Control ResolveViewForModel(Type modelType) {...}

Thanks for any hints.


Solution

  • First of all, I don't know caliburn.micro so this might be wrong.

    Looking at the ViewLocator method LocateTypeForModelType it seems that it asks the AssemblySource for available types which should be checked against the View-naming conventions.

    Since all of the above are static classes I suspect there is no way to inherit and override that behaviour. Since they are static, one could just add assemblies to the public observable dictionary - which feels a little bit of a hack and SelectAssemblies seems like the proper way.

    However, it seems to me that since there are conventions for resolving Views and ViewModels one could do the same for assemblies which brings us to the question: how do you decide which assemblies to scan for ViewModels/Views. That strategy can be built into the SelectAssemblies method.

    If you want to change how caliburn.micro finds the right views in those assemblies, effectively changing/adding to the exisiting conventions, there is an explanation in their wiki.

    To finally answer your question: "Resolve Views through IoC or MEF instead of using SelectedAssemblies() method": Imo this kind of defeats the philosophy of Caliburn.Micro:

    Caliburn.Micro uses conventions to resolve views from given assemblies - trying to use an IoC container instead of a name / namespace based convention contradicts that approach.