Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7525289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:30:08+00:00 2026-05-30T03:30:08+00:00

I have this interface for using AutoMapper: public interface IMapper { object Map(object source,

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:30:10+00:00Added an answer on May 30, 2026 at 3:30 am

    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this interface: public interface IValidationCRUD { public ICRUDValidation IsValid(object obj); private void
Suppose I have this interface public interface IFoo { ///<summary> /// Foo method ///</summary>
So lets say I have this interface: public interface IBox { public void setSize(int
Say I have this code - public interface ParentInterface1 { public List<? extends ChildInterface1>
I have this code (which is way simplified from the real code): public interface
I have this following interface : public interface FruitDetails { public String getFruitName(); }
I have this design: public interface IFactory<T> { T Create(); T CreateWithSensibleDefaults(); } public
Say I have an interface like this: public interface ISomeInterface { ... } I
I have IMessageSender interface. using System.ComponentModel.Composition; public interface IMessageSender { void Send(string message); }
I have just discovered this feature. Declaring an interface using the @interface syntax allows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.