Resolving classes without registering them using Castle Windsor

后端 未结 3 778
温柔的废话
温柔的废话 2020-12-01 16:13

Take the following useless program:

class Program
{
    static void Main(string[] args)
    {
        IUnityContainer unityContainer = new UnityContainer();
         


        
相关标签:
3条回答
  • 2020-12-01 16:47

    Krzysztof don't be afraid to link to your own blog here :) http://devlicious.com/blogs/krzysztof_kozmic/archive/2009/11/16/castle-windsor-lazy-loading.aspx

    Also, I found this simple implementation useful in my WPF app, remove the string contraint and you are close to the general case

    public class ViewModelLoader : Castle.MicroKernel.Resolvers.ILazyComponentLoader {
        public IRegistration Load(string key, Type service)
        {
            if (service == null)
                return null;
            if (service.Name.EndsWith("ViewModel", StringComparison.CurrentCultureIgnoreCase))
                return Component.For(service).Named(key);
            else
                return null;
        }
    }
    
    0 讨论(0)
  • 2020-12-01 16:51

    Windsor doesn't support that out of the box, but you can create extension methods to do this:

    static class WindsorExtensions
    {
        public static object ResolveType(this IWindsorContainer container, Type type)
        {
            if ( type.IsClass && !container.Kernel.HasComponent(type) )
                container.Kernel.AddComponent(type.FullName, type, LifestyleType.Transient);
            return container.Resolve(type);
         }
    
         public static T ResolveType<T>(this IWindsorContainer container)
         { return (T)ResolveType(container, typeof(T)); }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            IUnityContainer unityContainer = new UnityContainer();
            IWindsorContainer windsorContainer = new WindsorContainer();
    
            Program unityProgram = unityContainer.Resolve<Program>();
            Program castleProgram = windsorContainer.ResolveType<Program>();
        }
    }
    
    0 讨论(0)
  • 2020-12-01 17:01

    Windsor currently does not support that, and it's by design. The reasoning is that you should explicitly register types you need so that you dont get misconfigured object.

    There is however a possibility that there will be added a hook to create non-registered type at some point in the near future, as this is needed by the WCF integration facility. (Edit - it was added in v2.1 - take a look at ILazyComponentLoaders)

    Anyway, regardless of lazy component loaders, the best you can do is to use fluent API to batch register all types from an assembly matching your needed criteria upfront. It's not much more code and you'll sleep better at nights.

    Use lazy loaders only if you have really not enough information at startup (in your composition root) to determine what components you'll need.

    0 讨论(0)
提交回复
热议问题