here are my objects.

interface IMapper<Input, Output>
{
          Output MapFrom(Input item);
}

interface IEnumerableMapper<Input, Output> :
IMapper<IEnumerable<Input>, IEnumerable<Output>>
{
}

class IntToString: IMapper<int, string>
{
          string MapFrom(int item) { return item.ToString(); }
}

interface EnumerableMapper<Input, Output> : IEnumerableMapper<Input,
Output>
{
          IEnumerable<Output> MapFrom(IEnumerable<Input>item)
          {
                     IMapper<Input, Output> mapper =
IoC.Resolve<IMapper<Input, Output>>();
                     foreach(Input input in item) yield return
mapper.MapFrom(input);
          }
}

here is how I would access them

IoC.Resolve<IMapper<int, string>>().MapFrom(1);
IoC.Resolve<IEnumerableMapper<int, string>>().MapFrom(new int[]
{1,2,3});

-------------------------------------
IoC.Initialize(new WindsorContainer());
IoC.Container.Register(AllTypes
          .Pick().FromAssembly(assembly)
          .WithService.FirstInterface()

IoC.Resolve<IMapper<int, string>>() // exception: Not a
GenericeTypeDefiniation
IoC.Resolve<IEnumerableMapper<int, string>>() // successful

-------------------------------------
IoC.Initialize(new WindsorContainer());
IoC.Container.Register(AllTypes
          .Pick().FromAssembly(assembly)
          .WithService.Select(TheFirstInterface)

private Type TheFirstInterface(Type type)
{
   return type.GetInterfaces()[0];
}

IoC.Resolve<IMapper<int, string>>() // successful
IoC.Resolve<IEnumerableMapper<int, string>>() // exception: No
component found

How can I define TheFirstInterface so it will map
IntToString to IMapper<int, string>
EnumerableMapper<Input, Output> to IEnumerableMapper<Input, Output>
correctly?

this example is small. in productionn there would be 1 instance of a
generic EnumerableMapper and many instances of defined Mappers. For
now I created a facility like this.

public void Init()
{
          Kernel.Register(
 
Component.For(typeof(IEnumerableMapper<,>).ImplementedBy(EnumerableMapper<,>));
          Kernel.Register(
                    AllTypes.Of(typeof(IMapper<,>)
                              .FromAssembly(GetType().Assembly)
                              .WithService.Select(TheFirstInterface));
}

private static Type TheFirstInterface(Type type)
{
          return type.GetInterfaces()[0];
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to