MapProperty on collections seem impossible #1030
-
So I'm a bit stuck and I'm staring at the documentation without answers at this point... So say I have an entity class: public class MyClass
{
public ICollection<MyOtherClass> OtherItems { get; set; }
}
public class MyOtherClass
{
public string Name { get; set; }
} And a view class: public class MyViewClass
{
public IEnumerable<MyOtherViewClass> ViewItems { get; set; }
}
public class MyOtherViewClass
{
public string SomeName { get; set; }
} Now I want to map these to each other. Of course the mapping of [MapProperty(nameof(MyClass.OtherItems), nameof(MyViewClass.ViewItems))] But how do I map
[MapProperty("MyClass.OtherItems.Name", "MyViewClass.ViewItems.SomeName")] Which does not seem to work;
private static IEnumerable<MyOtherViewClass> ToView(this ICollection<MyClass> values)
{
return new List<MyOtherViewClass>();
} Which is then not used to map.
private static MyOtherViewClass ToView(this MyClass value)
{
return new MyOtherViewClass();
} Which is then also not used to map. I'm out of ideas. Am I overseeing something or is this a bug? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey, sorry to hear your stuck, 2/3 would be the recommended way of handling your issue. Unfortunately the function signatures are invalid.
[MapProperty("MyClass.OtherItems.Name", "MyViewClass.ViewItems.SomeName")] This won't work for two reasons. Firstly the member path is invalid as you have specified the type of the source and taget objects ( Secondly
It looks like you've made a mistake in your example you should use public static partial global::Riok.Mapperly.Sample.MyViewClass MapTo(global::Riok.Mapperly.Sample.MyClass src)
{
var target = new global::Riok.Mapperly.Sample.MyViewClass();
target.ViewItems = ToView(src.OtherItems);
return target;
}
Same issue as above: [MapProperty(nameof(MyOtherClass.Name), nameof(MyOtherViewClass.SomeName))]
private static partial MyOtherViewClass MapToOtherViewClass(MyOtherClass src); public static partial global::Riok.Mapperly.Sample.MyViewClass MapTo(global::Riok.Mapperly.Sample.MyClass src)
{
var target = new global::Riok.Mapperly.Sample.MyViewClass();
target.ViewItems = MapToIEnumerable(src.OtherItems);
return target;
}
private static partial global::Riok.Mapperly.Sample.MyOtherViewClass MapToOtherViewClass(global::Riok.Mapperly.Sample.MyOtherClass src)
{
var target = new global::Riok.Mapperly.Sample.MyOtherViewClass();
target.SomeName = src.Name;
return target;
}
private static global::System.Collections.Generic.IEnumerable<global::Riok.Mapperly.Sample.MyOtherViewClass> MapToIEnumerable(global::System.Collections.Generic.ICollection<global::Riok.Mapperly.Sample.MyOtherClass> source)
{
var target = new global::Riok.Mapperly.Sample.MyOtherViewClass[source.Count];
var i = 0;
foreach (var item in source)
{
target[i] = MapToOtherViewClass(item);
i++;
}
return target;
} |
Beta Was this translation helpful? Give feedback.
Hey, sorry to hear your stuck, 2/3 would be the recommended way of handling your issue. Unfortunately the function signatures are invalid.
This won't work for two reasons.
Firstly the member path is invalid as you have specified the type of the source and taget objects (
MyClass
&MyViewClass
) therefore mapperly will look for members of the nameMyClass
insideMyClass
.Secondly
OtherItems
andViewItems
are enumerable so its not possible to mapName
andSomeName
, what you've done in2.
and3.
is the correct way to handle enumerables.It looks like you'…