I am using linq to sql and I have 2 tables(sedan, suv: they are all same except suv has a height column, everything else is same ) but since they are in different tables and pregenerated classes I have a lot of ifs in my functions.Depending on enum car type I manupulate one or the other. I want to get rid of ifs, how should I design the database or how can I achive eliminating ifs ? like can I use generics?
Share
I would use the same table, but have a nullable height column (mapped to a nullable type in C#). Use another column, say type, to encode the vehicle type (you can use this as a discriminator for your classes). Use a constraint so that when the discriminator shows it is an SUV, the height column is required to be non-null.
Now map the table onto an abstract Car class with subclasses of SUV and Sedan, discriminated by the type column. I’d probably make the height property protected in the Car class and then add a public property in the SUV class that exposes the height property (basically using it as a backing field).