unitycontainer / registration-by-convention

Unity.RegistrationByConvention package (Requires Core Implementation. Any contributors?)
Apache License 2.0
4 stars 12 forks source link

Add support for searching registrations in assemblies #10

Open ArnaudB88 opened 5 years ago

ArnaudB88 commented 5 years ago

Support for searching 'registration classes' would be helpful when configuring registrations in a big solution. Registration-by-convention is a big help for general registrations, but custom ones should be written separately. In our solution, we have written such custom registrations in each project in a separate class. To avoid calling those classes manually, auto-search for these 'registration classes' would be helpful. This functionality can be found in StructureMap: http://structuremap.github.io/registration/auto-registration-and-conventions/ (See 'Scan for Registries')

How should it look like for us (the users)?

  1. We can create classes which inherit from a unity class (eg. Registry) with one method (or constructor). Within that method, container registrations can be written. This can be useful if registrations-by-convention should be overwritten with a custom registration (for eg. decorators/interceptors) Example:

    public class FooRegistry : Registry
    {
        public override void Register(IUnityContainer container, Func<LifetimeManager> createLifetimeManager)
        {
            container.RegisterType<IBar, Bar>(createLifetimeManager());
        }
    }
  2. It is possible to specify that unity would search for these type of classes. Example:

    //separate method
    container.SearchForRegistries(lifetimeManager);
    //or expanding existing method
    container.RegisterTypes(....., SearchForRegistries: true);
ENikS commented 5 years ago

I been thinking about adopting Microsoft's convention from either MEF, MEF2 or both. It has all the necessary classes and attributes already built into the framework and should be easily adopted. I am trying to avoid inventing new ways of doing well known things.

Perhaps I could add an attribute to find and execute a static method on static class to do something like you propose. I need more info on that.

ArnaudB88 commented 5 years ago

I don't have any experience with MEF, but I agree that this functionality should not be 'a new invention'. I got my thoughts from the (deprecated) library StructureMap which we also use on some projects. A possible implementation for this functionality can also be found there.