Skip to content

How to map an object to an interface #237

Description

@DevilKall

Sorry, I cannot find any documentation about this apparently easy task.
The situation is this:

    public interface IFoo
    {
        string Name { get; set; }
    }

    public class FooDto : IFoo
    {
        public string Name { get; set; }
    }

    public class Foo
    {
        public string Name { get; set; }
    }

I'm tring to configure the following mappings:

public class FooMappingConfiguration : MapperConfiguration
{
    protected override void Configure()
    {
        GetPlansFor<Foo>().To<FooDto>();  // works
        GetPlansFor<IFoo>().To<Foo>();  // works

        WhenMapping
            .From<Foo>().To<IFoo>()                
            .MapTo<FooDto>()
            ;

        GetPlansFor<Foo>().To<IFoo>();  // doesn't work!!!
    }
}

When I test the mapping plans it gives me the following error:

 MappingPlansComplete
 Duration: 789 ms

Message: 
Test method Example.Tests.AgileMapperTests.FooMapping_Test.MappingPlansComplete threw exception:
AgileObjects.AgileMapper.Validation.MappingValidationException: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- Foo -> IFoo
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 Rule set: CreateNew

 Unmappable target Types - fix by ignoring or configuring constructor parameters or factory methods:

  - Foo -> IFoo

Stack Trace: 
MappingValidator.VerifyMappingPlanIsComplete(IEnumerable`1 mapperDatas)
MappingValidator.Validate(Mapper mapper)
IMapper.ThrowNowIfAnyMappingPlanIsIncomplete()
FooMapping_Test.MappingPlansComplete() line 29

I also tried with CreateInstancesOf but the error remains the same.
What am I doing wrong? Any suggestion?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions