0

I have the following:

 public class Car : ITransportRelatedEntity
 {
    public void UpdateFrom(ITransportRelatedEntity otherEntity)
    {
        var typedEntity = otherEntity as Car; // From HERE      *
        if (typedEntity == null)              //                *
        {                                     //                *
            throw new ArgumentException(nameof(otherEntity));// *
        }                                     //                *
                                              //                *
        Name = typedEntity.Name;              //                *
        Make = typedEntity.Make;              //                *
        Model= typedEntity.Model;             // To HERE        *
    }

 }

 public class Plane : ITransportRelatedEntity
 {
    public void UpdateFrom(ITransportRelatedEntity otherEntity)
    {
        var typedEntity = otherEntity as Plane; // From HERE      *
        if (typedEntity == null)                //                *
        {                                       //                *
            throw new ArgumentException(nameof(otherEntity));  // *
        }                                       //                *
                                                //                *
        Name = typedEntity.Name;                //                *
        Brand = typedEntity.Brand;              //                *
        Passengers = typedEntity.Passengers;    // To HERE        *
    }

 }

public interface ITransportRelatedEntity
{
    void UpdateFrom(ITransportRelatedEntity otherEntity);
}

I feel like all of this could somehow be abstracted away, but I don't know how. Should I somehow try to force ITransportRelatedEntity to detect the calling type and automatically extrapolate / map the properties accordingly? If so, how do I do that?

SB2055
  • 12,272
  • 32
  • 97
  • 202
  • Interfaces cannot inherit from classes. But your method expects the `otherEntity` to be a `Car`, so why not just make the parameter of type `Car`? –  Oct 27 '16 at 17:05
  • Also, I don't understand why the question is tagged with "automapper". –  Oct 27 '16 at 17:07
  • @Amy added another example usage of the interface to address both comments. – SB2055 Oct 27 '16 at 17:08
  • I feel like you want to do what [this question asks](http://stackoverflow.com/questions/3610891/c-sharp-copying-property-values-from-one-instance-to-another-different-classe), and the answer is to use AutoMapper, but you tagged AutoMapper.. so are you asking how to use AutoMapper to map these properties? – Quantic Oct 27 '16 at 17:22
  • @Quantic I'm asking how to extract the automapping away from the interface implementations by extrapolating the calling type – SB2055 Oct 27 '16 at 17:26
  • I don't know AutoMapper, but after viewing the [tag](http://stackoverflow.com/questions/tagged/automapper) I see a `CreateMap` call in every single question. I can only assume you are not using "AutoMapper" [the library](http://automapper.org) and instead want to "automatically map properties" (without the library). Off the top of my head, you could `GetProperties` on `this`, and on `otherEntity`, then double `foreach` them, compare the property names to make sure they are the same, and make sure the property types are the same, then you can assign the `this` values by `propertyInfo.SetValue`. – Quantic Oct 27 '16 at 17:34
  • @Quantic - Thank you - I like that - but then how do I extract it out so that the *implementations* don't have to be concerned with duplicating that logic (so I only need to implement it once)? – SB2055 Oct 27 '16 at 18:07
  • 1
    You don't need `var typedEntity = otherEntity as Car;`, you just do `otherEntity.GetType()`, and reflect against that type (which is always the most derived type). The general pseudocode should be, `Map(object from, object to) { foreach (var fromProp in from.GetType().GetProperties()) { foreach (var toProp in to.GetType().GetProperties() { if (fromProp.Name == toProp.Name && fromProp.Type == toProp.Type) { toProp.SetValue(fromProp.Getvalue)}`. Same with fields, then there's the matter of reference types which is not handled here. And probably tons of other caveats that `AutoMapper` handles. – Quantic Oct 27 '16 at 18:17
  • @Quantic that was very generous of you - thank you. – SB2055 Oct 27 '16 at 18:33

0 Answers0