2

Resolved

Mapping from interface to class does not work if interface is based on another interface which contains the actual source fields to be mapped.

description

public interface IIA
    {
        long ID { get; set; }
        string Name { get; set; }
        int Age { get; set; }
        string Email { get; set; }
    }
 
    /* When mapping from IA to B - this is being taken as the litteral mapping source, it does not examine IIA to discover the actual fields, so we can't map IA to B*/
    public interface IA : IIA
    {
    }
 
    public class A : IA
    {
        public long ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Email { get; set; }
    }
 
    /* .. But we can happily map from IB to A*/
    public interface IB
    {
        long ID { get; set; }
        string Name { get; set; }
        int Age { get; set; }
        string Email { get; set; }
    }
 
    public class B : IB
    {
        public long ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Email { get; set; }
    }
 
 
So .. while mapping from IB to A works fine..
 
        B b = new B()
        {
            ID = 1,
            Name = "hello",
            Age = 2,
            Email = "3@4.5"
        };
 
        Console.WriteLine("Original  #{0} {1} {2} {3}", l_b.ID, l_b.Name, l_b.Age, l_b.Email);
 
        var mapper = ObjectMapperManager.DefaultInstance.GetMapper<IB, A>();
 
        A a = l_mapper.Map(b);
 
        Console.WriteLine("Mapped  #{0} {1} {2} {3}", l_a.ID, l_a.Name, l_a.Age, l_a.Email);
 
        Assert.AreEqual(a.ID, b.ID, "ID is different");
        Assert.AreEqual(a.Name, b.Name, "Name is different");
        Assert.AreEqual(a.Age, b.Age, "Age is different");
        Assert.AreEqual(a.Email, b.Email, "Email is different");
 
Mapping from IA to B results in no fields being copied.
 
        A a = new A()
        {
            ID = 1,
            Name = "hello",
            Age = 2,
            Email = "3@4.5"
        };
 
        Console.WriteLine("Original  #{0} {1} {2} {3}", a.ID, a.Name, a.Age, a.Email);
 
        var l_mapper = ObjectMapperManager.DefaultInstance.GetMapper<IA, B>();
 
        B b = l_mapper.Map(a);
 
        Console.Writeline("Mapped  #{0} {1} {2} {3}", b.ID, b.Name, b.Age, b.Email);
 
        Assert.AreEqual(a.ID, b.ID, "ID is different");
        Assert.AreEqual(a.Name, b.Name, "Name is different");
        Assert.AreEqual(a.Age, b.Age, "Age is different");
        Assert.AreEqual(a.Email, b.Email, "Email is different");

file attachments

comments

vzgromozdun wrote Sep 6, 2011 at 1:33 PM

I have fixed it for me. here is patch with fix and updated unit tests

wrote Sep 13, 2011 at 7:15 PM

wrote Sep 13, 2011 at 7:30 PM

wrote Feb 21, 2013 at 10:43 PM

wrote May 16, 2013 at 10:21 AM

wrote May 16, 2013 at 10:21 AM

wrote Jun 14, 2013 at 7:23 AM