Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

inversion of control - Register component based on parameter name on requestor in windsor

I have this interface for using AutoMapper:

public interface IMapper
{
    object Map(object source, Type sourceType, Type destinationType);
}

Then for each type of data, I have a different mapper class , for example:

 public class UserMapper : IMapper
{
    static UserMapper()
    {
        Mapper.CreateMap<User, UserViewModel>();
        Mapper.CreateMap<UserViewModel, User>();
    }

    public object Map(object source, Type sourceType, Type destinationType)
    {
        return Mapper.Map(source, sourceType, destinationType);
    }
}

Then I have IMapper as one of the parametter in my controller class like this:

public UsersController(IUsersRepository repo, IMapper userMapper)
{....}

I am using Windsor as the IOC for my application and the problem is that I want to register the components, so that when running in UsersController , it use the UserMapper class and if running on ProductsController it will use my ProductMapper class.

My registration code looks something along the line of this:

container.Register(
    Component.For<IMapper>()
             .ImplementedBy<UsersMapper>()
             .Named("usersMapper"),
    Component.For<IMapper>()
             .ImplementedBy<ProductsMapper>()
             .Named("productsMapper"),
    Component.For<ProductController>()
             .ServiceOverrides(ServiceOverride.ForKey("usersMapper").Eq("productsMapper"))
)

I have done my homework on google and stackoverflow, and i know that I need to use ServicesOverride but I am still stuck on this, could anyone give me a hand please?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

While svick's solution looks correct to me (I haven't attempted to compile it, though), this scenario is an excellent case for convention-based configuration.

Let's introduce this convention: Each consumer of IMapper will signal the intended role of the mapper by its name. By default, that name will be matched with a type of the same name - only with different casing.

So, constructor parameters could be mapped like this:

  • userMapper -> UserMapper
  • productMapper -> ProductMapper

In Castle Windsor, such a configuration might look like this:

container.Register(Classes
    .FromThisAssembly()
    .Pick()
    .WithServiceAllInterfaces()
    .WithServiceSelf());

container.Kernel.Resolver.AddSubResolver(
    new MapperConvention(container.Kernel));

And the Sub Resolver (where the magic really happens) looks like this:

public class MapperConvention : ISubDependencyResolver
{
    private readonly IKernel kernel;

    public MapperConvention(IKernel kernel)
    {
        this.kernel = kernel;
    }

    public bool CanResolve(CreationContext context,
        ISubDependencyResolver contextHandlerResolver,
        ComponentModel model,
        DependencyModel dependency)
    {
        return typeof(IMapper).IsAssignableFrom(dependency.TargetType);
    }

    public object Resolve(CreationContext context,
        ISubDependencyResolver contextHandlerResolver,
        ComponentModel model,
        DependencyModel dependency)
    {
        var representativeMapperType = typeof(UserMapper);
        var concreteMapperType = representativeMapperType.Assembly
            .GetExportedTypes()
            .Where(t => 
                t.Name.Equals(dependency.DependencyKey,
                    StringComparison.OrdinalIgnoreCase))
            .Single();
        return this.kernel.Resolve(concreteMapperType);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...