C# re-use base mapping in subclass

Date: 2019-04-04
// Convert this mapper from the base class:
        public static VmSensor FromDomain(Sensor sensor)
        {
            return new VmSensor {
                    Id = sensor.Id,
                    SensorSerial = sensor.SensorSerial,
                    SensorModuleId = sensor.SensorModuleId,
                    TankId = sensor.TankId,
                    Status = sensor.Status,
                    InstallDate = sensor.InstallDate,
                    Remark = sensor.Remark,
                    SensorTypeId = sensor.SensorTypeId,
                    BatteryInstallDate = sensor.BatteryInstallDate
                };
        }
// to a generic one:
        public static VmSensor FromDomain(Sensor sensor)
        {
            return FromDomain<VmSensor>(sensor);
        }
        public static T FromDomain<T>(Sensor sensor) where T : VmSensor
        {
            if (sensor == null)
                return null;

            var vmSensor = Activator.CreateInstance<T>();

            vmSensor.Id = sensor.Id;
            vmSensor.SensorSerial = sensor.SensorSerial;
            vmSensor.SensorModuleId = sensor.SensorModuleId;
            vmSensor.TankId = sensor.TankId;
            vmSensor.Status = sensor.Status;
            vmSensor.InstallDate = sensor.InstallDate;
            vmSensor.Remark = sensor.Remark;
            vmSensor.SensorTypeId = sensor.SensorTypeId;
            vmSensor.BatteryInstallDate = sensor.BatteryInstallDate;

            return vmSensor;
        }

// and in the subclass:
        public static VmSensorWithBasicMeasurements FromDomain(Sensor sensor)
        {
            var vm = FromDomain<VmSensorWithBasicMeasurements>(sensor);
            vm.Measurements = sensor.Measurements.Select(x => VmMeasurementBasic.FromDomain(x));

            return vm;
        }

20590cookie-checkC# re-use base mapping in subclass