I have this interface to use AutoMapper:
public interface IMapper { object Map(object source, Type sourceType, Type destinationType); }
Then for each data type I have a different mapping 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 parameters in my controller class, for example:
public UsersController(IUsersRepository repo, IMapper userMapper) {....}
I use Windsor as IOC for my application, and the problem is that I want to register the components, so when I run it in UserController it uses the UserMapper class, and if it runs on ProductController, it will use my ProductMapper class.
My registration code looks something like 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 did my homework on google and stackoverflow and I know that I need to use ServiceOverride, but I still stick to this, can anyone give me a hand?
thanks