Note that there are some explanatory texts on larger screens.

plurals
  1. POGeneric typed factory in windsor
    primarykey
    data
    text
    <p>Maybe abstract typed factories are not an easy point to start with Windsor (2.5.3 if it matters) but I've got to do it anyway. I'm trying to build a factory giving back processors depending on message type. So far i've scavenged from different places following code:</p> <pre><code>public class Complicato { public static void Do(string[] args) { IKernel kernel = new DefaultKernel(); IWindsorContainer container = new WindsorContainer(); kernel.AddFacility&lt;TypedFactoryFacility&gt;(); container.Install(); container.Register( Component.For&lt;HandlerSelector, ITypedFactoryComponentSelector&gt;(), AllTypes.FromThisAssembly().BasedOn(typeof(ITrier&lt;&gt;)) .WithService.Base().Configure(conf =&gt; conf.LifeStyle.Is(LifestyleType.Transient)), Component.For&lt;Factor&gt;(), Component.For&lt;ITryFactory&gt;().AsFactory(c =&gt; c.SelectedWith&lt;HandlerSelector&gt;()).LifeStyle.Singleton); var factor = container.Resolve&lt;Factor&gt;(); var factory = container.Resolve&lt;ITryFactory&gt;(); } } public class HandlerSelector : DefaultTypedFactoryComponentSelector { protected override Type GetComponentType(MethodInfo method, object[] arguments) { return typeof(ITrier&lt;&gt;).MakeGenericType(arguments[0].GetType()); } } public class Factor { private ITryFactory factory; public void Try(IWhat onto) { factory.GetTrier(onto).Try(onto); } } public interface ITryFactory { ITrier&lt;IWhat&gt; GetTrier(IWhat onto); void Release(object elem); } public interface IWhat { } public interface ITrier&lt;in TWhat&gt; where TWhat : IWhat { void Try(TWhat input); } public class TrierYes : ITrier&lt;WhatYes&gt; { public void Try(WhatYes input) { Console.WriteLine("Yes? " + input.Aye()); } } public class TrierNo : ITrier&lt;WhatNot&gt; { public void Try(WhatNot input) { Console.WriteLine("No? " + input.Naa()); } } public class WhatYes : IWhat { public bool Aye() { return true; } } public class WhatNot : IWhat { public bool Naa() { return false; } } </code></pre> <p>Main problem here is that id doesn't work. First I get Factor with factory of null and then as a consequence trying to resolve factory explicitely gives me ComponentActivator: could not proxy Factories.Complex.ITryFactory with inner message of The interceptor Castle.TypedFactory.Interceptor could not be resolved and "Keys (components with specific keys) - Castle.TypedFactory.Interceptor which was not registered" in container. I don't even know if the Handler selector works, it's not in question so far.</p> <p>If I make ITrier not generic - it suddenly starts working but it's definitely not what I'm trying to achieve.</p> <p>So do I make some silly beginners mistake in Windsor configuration or misunderstand the idea of typed factory?</p> <p>For completeness sake, here's the exception message:</p> <pre><code>Castle.MicroKernel.ComponentActivator.ComponentActivatorException was unhandled Message=ComponentActivator: could not proxy Factories.Complex.ITryFactory Source=Castle.Windsor StackTrace: at Castle.MicroKernel.ComponentActivator.DefaultComponentActivator.CreateInstance(CreationContext context, Object[] arguments, Type[] signature) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\ComponentActivator\DefaultComponentActivator.cs:line 166 InnerException: Castle.MicroKernel.Resolvers.DependencyResolverException Message=The interceptor Castle.TypedFactory.Interceptor could not be resolved Source=Castle.Windsor StackTrace: at Castle.Core.InterceptorReference.Castle.MicroKernel.IReference&lt;Castle.DynamicProxy.IInterceptor&gt;.Resolve(IKernel kernel, CreationContext context) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Core\InterceptorReference.cs:line 142 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload