This might be a bit of an edge case, and it might be some bad modeling, but I wanted to check if it should work.
If we map an instance of a subclass with a property X marked as “new” (because it should have the same name as in the base calss) to another class, the values use the value from the base class and not from the subclass.
Here are 3 test cases:
namespace Tests
{
using ServiceStack;
using Xunit;
public class Test
{
[Fact]
public void ConvertingFromClassWithNewPropertiesUsesValuesForNewProperties()
{
var x = new BaseClassInherited
{
Id = "id",
Content = new MultiLanguageThingieReduced
{
Name = "name",
Value = "value"
}
};
var d = x.ConvertTo<InterfaceDto>();
Assert.NotNull(d.Content);
Assert.Equal("id", d.Id);
Assert.Equal("name", d.Content.Name);
Assert.Equal("value", d.Content.Value);
}
[Fact]
public void ConvertingFromClassWithNewPropertiesWithBasePropertyFilledUsesValuesForNewProperties()
{
var x = new BaseClassInherited
{
Id = "id",
Content = new MultiLanguageThingieReduced
{
Name = "subclass name",
Value = "subclass value"
}
};
((BaseClass)x).Content = new ComplexValueClass()
{
Name = "name"
};
var d = x.ConvertTo<InterfaceDto>();
Assert.NotNull(d.Content);
Assert.Equal("id", d.Id);
Assert.Equal("subclass name", d.Content.Name);
Assert.Equal("subclass value", d.Content.Value);
}
[Fact]
public void BaseConversionWorks()
{
var x = new BaseClass
{
Id = "id",
Content = new ComplexValueClass
{
Name = "name"
}
};
var d = x.ConvertTo<InterfaceDto>();
Assert.NotNull(d.Content);
Assert.Equal("id", d.Id);
Assert.Equal("name", d.Content.Name);
}
}
class BaseClass
{
public string Id { get; set; }
public ComplexValueClass Content { get; set; }
}
class ComplexValueClass
{
public string Name { get; set; }
}
class BaseClassInherited : BaseClass
{
public new MultiLanguageThingieReduced Content { get; set; }
}
class MultiLanguageThingieReduced
{
public string Name { get; set; }
public string Value { get; set; }
}
class InterfaceDto
{
public string Id { get; set; }
public MultiLanguageThingieReducedDto Content { get; set; }
}
class MultiLanguageThingieReducedDto
{
public string Name { get; set; }
public string Value { get; set; }
}
}