Hi,
What you want to use is the typed factory facility
http://docs.castleproject.org/Default.aspx?Page=Typed-Factory-Facility-interface-based-factories&NS=Windsor&AspxAutoDetectCookieSupport=1
So first, add the facility to your container:
var container = new WindsorContainer();
container.AddFacility<TypedFactoryFacility>();
Then register your component, and also register a factory for it:
container.Register(
Component.For<AbExternalProvider>().ImplementedBy<ExternalConcrete>().LifestyleTransient(),
Component.For<Func<string, AbExternalProvider>>().AsFactory());
Now resolve your factory and get your component:
var factory = container.Resolve<Func<string, AbExternalProvider>>();
var externalProvider = factory("SomeString");
You can also create an interface for the factory instead of using a Func if
it makes the code easier to understand.
I haven't actually compiled any of that code - just doing it from memory.
Hopefully it should do the trick.
Cheers,
Adam
On 16 October 2014 19:40, fsp <[email protected]> wrote:
> Hi all,
> I don't know much about Castle but I started using it some time ago. The
> simple things about dependency resolution are pretty straight-forward and
> well-documented. But I have a problem and I cannot find a solution in an
> easy way. This is a simple console application project. I have an interface
> let's say:
> public interface IExternal
> {
> string GetSomething();
> }
>
> Moreover I have an abstract class:
> public abstract class AbExternalProvider: IExternal
> {
> public string SomeString {get;private set;}
> public AbExternalProvider(string pSomeString)
> {
> SomeString = pSomeString;
> }
> public abstract string GetSomething();
> }
>
> Finally I have a concrete class:
> public class ExternalConcrete: AbExternalProvider
> {
> public ExternalConcrete(string pSomeString)
> : base(pSomeString)
> {
>
> }
> public override string GetSomething()
> {
> return "Something";
> }
> }
>
> in program.cs
> class Program
> {
> static void Main(string[] args)
> {
> var container = new Castle.Windsor.WindsorContainer();
>
> container.Register(Component.For<AbExternalProvider>().ImplementedBy<ExternalConcrete>().LifestyleTransient());
> AbExternalProvider provider =
> container.Resolve<AbExternalProvider>();
>
>
>
>
> Console.ReadLine();
> }
> }
>
> But how can I resolve, in runtime, the dependency and pass a string
> parameter to the Constructor of the concrete class according to an input
> for example? I am currently getting an exception:
>
>> 'cannot create component as it has dependencies to be satisfied'
>
>
> Thanks a lot in advanced!
>
> --
> 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/d/optout.
>
--
Adam Connelly
*Software Developer*
<http://resdiary.com/>
*View our Short Video presentation
<http://www.youtube.com/watch?v=09TuCL1mOu4>*
Sales.......+44 (0)207 871 7491
Support....+44 (0)207 871 7490
DDI..........+44 (0)141 404 1441
Restaurantdiary.com Ltd. Registered in Scotland | Company No. SC258100
| Registered
Office: 75 Bothwell Street, G2 6TS
VAT Registration Number: GB829960776
PCI Compliance
<https://sealserver.trustwave.com/cert.php?customerId=62ba0c8cb2344f8789374c544a067258&size=65x36&style=invert>
--
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/d/optout.