c# - Design-pattern: different types of objects to one -
i want convert different types of objects one.
for example have: object1:class1 object2:class2 object3:class3
all objects different, not make them inherit 1 interface.
before transferring part of application, want convert them objectdto:classdto.
what design pattern used in situation?
you can use implicit
type conversion operator in c#:
public class object1dto { public int property {get; set;} } public class object1 { public int property {get; set;} public static implicit operator object1dto(object1 object1) { return new object1dto { property = object1.property }; } }
or can use extensions:
public static class extensions { public static object1dto todto(this object1 object1) { return new object1dto { property = object1.property }; } }
you can @ libraries automapper
handle such object-object mapping scenarios.
Comments
Post a Comment