Registering WPF “Views” with Windsor Fluent API

I’ve had this nagging issue for some time now with WPF views that are registered for an interface.  The Views themselves are WPF UserControls:

namespace SomeApp.Views
{
    public partial class SearchView : UserControl, ISearchView
    {
        public SearchView()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            throw new NotImplementedException();
        }
    }
}

 

I would love to have registered all views in one go by simply adding:

container.Register(AllTypes
                       .FromAssembly(assembly)
                       .Where(x => x.Name.EndsWith("View"))
                       .WithService.FirstInterface());

 

Unfortunately, this registers my Views for System.Windows.Media.Composition.DUCE+IResource implementations which I really couldn’t care less about.  The interface I’m looking for is the “nearest” interface for lack of a better term.  Fortunately, Windsor lets us provide a delegate selector for the interface.  Here’s what I’m using now:

container.Register(AllTypes
                       .FromAssembly(assembly)
                       .Where(x => x.Name.EndsWith(suffix))
                       .WithService.Select((t, bt) =>
                                               {
                                                   var serviceType = t
                                                       .GetInterfaces()
                                                       .Where(x => x.FullName.StartsWith("SomeApp"))
                                                       .FirstOrDefault();

                                                   if (serviceType != null)
                                                       return new[] {serviceType};
                                                   else
                                                       return new[] {t};
                                               }));

 

Use Select versus FirstInterface allows us to provide back the Service type(s?) that this class is implementing.  The StartsWith is obviously fragile, but I’m green right now, so I’ll see you on the other side.