I have a class structure similar to this:
public abstract class Device
{
public int DeviceId { get; set; }
//Additional Properties
}
public class DeviceA : Device
{
//Specific Behaviour
}
public class DeviceB : Device
{
//Specific Behaviour
}
I need to retrieve a list of Devices, or a single Device which is instantiated as the appropriate derived type (based upon a Type value in the Device Record in the DB). That is, the collection of Device
objects should contain a number of objects with different Types, all of which are derived from Device
.
I have implemented this the following way, but something just doesn't feel right about it.
public static IEnumerable<Device> AllDevices()
{
using (var connection = CreateConnection())
{
connection.Open();
return connection.Query<dynamic>("SELECT * FROM Device").Select<dynamic, Device>(d =>
{
Device device = null;
if (d.DeviceTypeID == 1)
device = new DeviceA();
else if (d.DeviceTypeID == 2)
device = new DeviceB();
else throw new Exception("Unknown Device");
device.DeviceId = d.DeviceID;
return device;
});
}
}
Is this the correct way to achieve this using Dapper, or is there a better approach?