Search code examples
dryioc

Automatically registering all ViewModel and View files


I am looking into using DryIoc with Caliburn.Micro, and I could like to automatically register all ViewModels and Views.

In AutoFac you can do something like this

builder.RegisterAssemblyTypes(AssemblySource.Instance.ToArray())
    .Where(type => type.Name.EndsWith("ViewModel"))
    .Where(type => type.GetInterface(ViewModelBaseType.Name, false) != null)
    .AsSelf()
    .InstancePerDependency();

Is there a similar way in DryIoc?


Solution

  • Equivalent code in DryIoc:

    container.RegisterMany(AssemblySource.Instance.ToArray()
       .SelectMany(Portable.GetAssemblyTypes)
       .Where(type => type.Name.EndsWith("ViewModel"))
       .Where(type => type.GetInterface(ViewModelBaseType.Name, false) != null));
    

    The default reuse will be Transient which is the same as InstancePerDependency.

    Update:

    There is also a bit simpler and more idiomatic alternative:

    container.RegisterMany(AssemblySource.Instance,
        serviceTypeCondition: type => 
            type.Name.EndsWith("ViewModel") &&
            type.GetInterface(ViewModelBaseType.Name, false) != null);