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?