agileobjects / AgileMapper

A zero-configuration, highly-configurable, unopinionated object mapper with viewable execution plans. Flattens, unflattens, deep clones, merges, updates and projects queries. .NET 3.5+ and .NET Standard 1.0+.
MIT License
460 stars 27 forks source link

How to map an object to an interface #237

Open DevilKall opened 8 months ago

DevilKall commented 8 months ago

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?