I came across something called the NamingPartsSubsystem (see my post a few days ago with the same name)
I could have made it work with that, but it appears to be removed / broken in castle 3.x > On Dec 8, 2013, at 1:12 PM, Vel Elous <[email protected]> wrote: > > It does appear that we are working on a similar scenario. > > >> On Friday, December 6, 2013 8:41:59 AM UTC, Vel Elous wrote: >> I am experimenting with the Castle Windsor IoC container, in particular >> looking for the "correct" method on how to inject a factory in to a >> constructor. >> >> I have been reading up on the Typed Factory Facility, however, it appears >> that the AsFactory()method automatically generates a factory behind the >> scenes. I would like to make use of my ownfactory implementation. Any class >> which consumes the factory will provide the Create method with some >> arguments which will subsequently be used by the factory to determine which >> objectto return. Below is a contrived example of what I have: >> >> namespace TypedFactory >> { >> using System; >> using Castle.MicroKernel.Registration; >> using Castle.Windsor; >> >> class Program >> { >> static IWindsorContainer container; >> >> static void Main(string[] args) >> { >> container = new WindsorContainer(); >> container.Register( >> >> Component.For<IMyObjectFactory>().ImplementedBy<MyObjectFactory>().LifeStyle.Singleton, >> Component.For<FactoryConsumer>() >> ); >> >> var factoryConsumer = container.Resolve<FactoryConsumer>(); >> // The following Console.WriteLine(...) >> // Prints "ToString() called from MyObjectA" to the console, so >> seems to work >> Console.WriteLine(factoryConsumer.Create("MyObjectA", "Some >> value to go to constructor")); >> Console.ReadKey(); >> } >> } >> >> #region [ MyObject ] >> >> public interface IMyObject >> { >> } >> >> public class MyObjectA : IMyObject >> { >> private readonly string output; >> >> public MyObjectA(string output) >> { >> this.output = output; >> } >> >> public override string ToString() >> { >> return "ToString() called from " + this.GetType().Name; >> } >> } >> >> public class MyObjectB : IMyObject >> { >> private readonly string output; >> >> public MyObjectB(string output) >> { >> this.output = output; >> } >> >> public override string ToString() >> { >> return "ToString() called from " + this.GetType().Name; >> } >> } >> >> #endregion >> >> #region [ MyObject Factory ] >> >> public interface IMyObjectFactory >> { >> IMyObject Create(string make, string output); >> } >> >> public class MyObjectFactory : IMyObjectFactory >> { >> public IMyObject Create(string make, string output) >> { >> IMyObject myObject = null; >> switch (make) >> { >> case "MyObjectA": >> myObject = new MyObjectA(output); >> break; >> case "MyObjectB": >> myObject = new MyObjectB(output); >> ;break; >> } >> >> return myObject; >> } >> } >> >> #endregion >> >> public class FactoryConsumer >> { >> private readonly IMyObjectFactory objectFactory; >> >> public FactoryConsumer(IMyObjectFactory objectFactory) >> { >> this.objectFactory = objectFactory; >> } >> >> public IMyObject Create(string make, string output) >> { >> return this.objectFactory.Create(make, output); >> } >> } >> } >> Is the above acceptable, or is there something else I should be doing? > > -- > You received this message because you are subscribed to a topic in the Google > Groups "Castle Project Users" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/castle-project-users/TiW2LBsxB74/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/castle-project-users. > For more options, visit https://groups.google.com/groups/opt_out. -- You received this message because you are subscribed to the Google Groups "Castle Project Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/castle-project-users. For more options, visit https://groups.google.com/groups/opt_out.
