Using conventions with the MEF

By default, Caliburn Micro uses the MEF. And, by default, the MEF uses attributes to identify components.

I’m pretty lazy and, given the choice, I’d rather use conventions and auto-registration. Luckily someone else was way ahead of me! And the MEFContrib project contains all the building blocks you need to create your own conventions.

I was originally planning on implementing a convention to register the first declared interface but, due to the vagaries of reflection, I took the easy way out and registered all interfaces from the same assembly (did I mention I’m lazy?).

    public class AppPartRegistry : PartRegistry
        public AppPartRegistry()

            this.Scan(c => c.Assembly(typeof(AppPartRegistry).Assembly));

    public static class PartConventions
        public static void RegisterAllInterfaces(this PartConventionBuilder<PartConvention> builder)
                .ForTypesMatching(t => true)
                .Exports(e =>
                    e.Export().Members(t => t.GetInterfaces().Where(i => i.Assembly == t.Assembly).ToArray());

Update: Import (greediest) ctor, and make registrations transient.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s