Pergunta

I am currently registering multiple objects with specific names, I'd like to do it by convention.

Currently works:

container.Register(Component.For<Repository>().Named("Repository1").ImplementedBy<Repository1>());
container.Register(Component.For<Repository>().Named("Repository2").ImplementedBy<Repository2>());

container.Register(
            Component.For<RepositoryFactorySelector, ITypedFactoryComponentSelector>().LifestyleSingleton(),
            Component.For<IRepositoryFactory>().AsFactory(c => c.SelectedWith<RepositoryFactorySelector>()));

I've tried:

container.Register(Classes.FromThisAssembly().BasedOn<Repository>().WithServiceDefaultInterfaces());

But then my custom Factory and DefaultTypedFactoryComponentSelector can't find the component by name?

Requested component named 'Repository2' was not found in the container. Did you forget to register it?

How can I register components using a convention but dynamically named?

Foi útil?

Solução

On the windsor page about registering components by convention (http://docs.castleproject.org/Windsor.Registering-components-by-conventions.ashx) the following code snippet should help you:

container.Register(
   Classes.FromAssembly(Assembly.GetExecutingAssembly())
      .BasedOn<ICommon>()
      .LifestyleTransient()
      .Configure(component => component.Named(component.Implementation.FullName + "XYZ"))

);

By the way, why don't you change your componentselector to select the correct handler based on implementation type ?

Kind regards, Marwijn.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top