I cannot seem to google this one correctly… I have a class (Widget) that represents a database table from the Data Layer.
The table holds 3 different types of records, where one uses only 5 columns, another uses 10 columns etc. Each record has a different set of validation and business rules that I want to control with Business Layer abstraction.
Is the proper thing to create 3 concrete classes and map the properties to the single database table class? I feel like I’m missing an opportunity to use an interface or inheritance?
If I want something like below wouldn’t my Widget classes inherit from the database table class that holds all widgets? And if it did, then how would I “hide” or disinherit the properties betwen the specific widget types?
List<SmallWidget> sw = BusinessLayer.GetWidgets<SmallWidget>();
List<MediumWidget> mw = BusinessLayer.GetWidgets<MediumWidget>();
List<LargeWidget> lw = BusinessLayer.GetWidgets<LargeWidget>();
Thanks for the advice.
Keep your business objects as close to the actual business entities as possible – create three separate classes for the three types of widgets – SmallWidget, MediumWidget and LargeWidget. Let there be a single data class – WidgetData that maps to the table. You can then implement the three widget classes using any ONE of the following strategies:
This will cover the data part. Then if you feel that there is some commonality between the three widget classes, you can also create a base class which the three can extend and add any common methods or fields there.