How to convert a custom complex type to another custom complex type

So, I have two custom complex types (this is simplified for this example):

public class PendingProduct { public string Name { get; set; } public string Description { get; set; } public int ColorCount { get; set; } } 

Let's say I need this object to know how to transform myself into another type:

 public class Product { public string Name { get; set; } public string Description { get; set; } public ProductColors Colors { get; set;} } 

Therefore, when I call the method to convert PendingProduct to Product, I execute some custom logic that adds the ColorCount number to the ProductColor objects in the Product class. This is completely simplified, but the architecture of this class does not really matter.

What is my main question:

What best practice method can be used to implement the conversion of one complex type to another complex type when the properties of objects are different?

In the real world, the properties are very different, and I will write some user logic to match what I need from Object A to Object B.

Obviously, I could just write a function that takes an input parameter Object A and returns Object B, but I'm looking for a more "Best Practice" method. Is IConvertible included here? Is there something more like OOP that I can use rather than just write a function to do what I want?

Object A must always know how to transform itself into object B.

EDIT: As a basic note in the real world, Object A and Object B are Entity Framework 4 objects. I want to take the "Pending Product", convert it to a new Product object, bind it to the data context and save.

+8
c # oop entity-framework entity-framework-4
source share
2 answers

There are many ways to do this, but they really come down to the fact that they themselves write the display code, process it through reflection, or rely on a pre-built infrastructure, such as AutoMapper . I answered a similar question in another SO question here .

I will add it here for reference:

Really you could

1.Reflection

 public void Map<TSource, TDestination>(TSource source, TDestination destination) { var props = typeof(TSource).GetProperties(BindingFlags.Public | BindingFlags.Instance); var type = typeof(TDestination); foreach (var prop in props) { object value = prop.GetValue(source, null); var prop2 = type.GetProperty(prop.Name); if (prop2 == null) continue; if (prop.PropertyType != prop2.PropertyType) continue; prop2.SetValue(destination, value, null); } } 

Constructor 2.Copy

 public Employee(Person person) { // Copy properties } 

3. Explicit / Explicit Conversion

 public static implicit operator Employee(Person person) { // Build instance and return } 

4.AutoMapper

 Mapper.Map<Person, Employee>(person); 

5. Combination 3/4:

 public static implicit operator Employee(Person person) { return Mapper.Map<Person, Employee>(person); } 

Note on implicit / explicit conversion operators: I believe in their use, you will not generate CLS-compatible code.

AutoMapper allows you to customize how various properties of the target type are mapped, for example:

 Mapper.CreateMap<Person, Employee>() .ForMember(e => e.Forename, o => o.MapFrom(p => p.Forename.ToLower())); 
+18
source share

Would you like to take PendingProduct out of the product?

 public class Product { public string Name { get; set; } public string Description { get; set; } public ProductColors Colors { get; set; } } public class PendingProduct : Product { public int ColorCount { get; set; } } 
0
source share

Source: https://habr.com/ru/post/651084/


All Articles