oop - c# OO advice for inherited attribute classes -
i oo correct advice please. correct way of accessing attribute data inherited classes.
assuming 3 classes:
public abstract class vehicle { public int id {get; set; } public int wheels {get; set; } public string name {get; set; } public string color {get; set; } } public class car : vehicle { } public class truck : vehicle { public int loadcapacity {get; set; } }
my first thought create repository of kind gives me attributes of whatever i'm looking for.
there 2 types of queries see need:
- return attributes of vehicle specific id
- give me vehiclesof specific type
so:
public class vehiclerepository { icollection <???> _vehicles; ??? getvehicle(int id); ??? getvehicles(??? vehicletype); }
the ??? unknowns.
my thought add interface ivehicle , implement vehicle it. result like:
public class vehiclerepository { icollection <ivehicle> _vehicles; ivehicle getvehicle(int id); icollection <ivehicle> getvehicles(ivehicle vehicletype); }
this doesnt work.
public ivehicle getvehicle(int id) { return _vehicles.first(x=>x.id == id); // above want compile interface has no attributes }
if use vehicle instead of ivehicle, 'first(x=>x.id == id)' work fine not return 'loadcapacity' if vehicle truck.
looking @ getvehicles function, thinking like:
public icollection <t> getvehicles <t> () { var vehicles = _vehicles.where(???); return vehicles; }
a bit lost on should go in there avoid reflection , reading assemblies , class types, etc.
thanks advice.
to store , vericles, need like:
class vehiclerepository { list<vehicle> vehicles; }
to retrieve them, use ienumerable<t>.oftype<u>()
:
vehiclerepository repository = ... repository.vehicles.oftype<car>(); // of cars repository.vehicles.oftype<truck>(); // of trucks
if wanted fancy though, make vehiclerepository
collection own making implement ienumerable<t>
. consider:
class vehiclerepository : ienumerable<vehicle> { list<vehicle> vehicles; public ienumerator<vehicle> getenumerator() { return vehicles.getenumerator(); } system.collections.ienumerator system.collections.ienumerable.getenumerator() { return this.getenumerator(); } }
then can query against collection itself, in:
public static void main() { vehiclerepository repository = ...; // notice skip accessing vehicles member // because implementing ienumerable<t> class // becomes collection. can use oftype<t> directly. repository.oftype<car>(); }
edit: getvehiclebyid
, might try this:
class vehiclerepository : ienumerable<vehicle> { list<vehicle> vehicles; public vehicle getvehiclebyid(int32 id) { return vehicles[id]; } }
you use indexer, so:
class vehiclerepository : ienumerable<vehicle> { list<vehicle> vehicles; public vehicle this[int32 id] { { return vehicles[id]; } } }
to loadcapacity
truck, have this:
public static void main() { vehiclerepository repository = ... // assuming index 0 truck truck truck = (truck)repository.getvehiclebyid(0); truck.loadcapacity = ... }
one cheaty way items of particular type (when bound ui, i.e. assuming you're using strings
) without using reflection match tostring
values of type names. example:
public static void main() { vehiclerepository repository = ... // assume we're storing off typename in ui dropdown. // when user selects it, automatically have correct // type name want filter by. string trucktypename = typeof(truck).gettype().tostring(); // filter types matching type name. inefficient, // avoid reflection. repository.where(vehicle => vehicle.gettype().tostring() == trucktypename) }
Comments
Post a Comment