I’m new to .net and OOP and I’ve been having difficulty working out how I should create classes for a property site I’m building.
In my database, I have a “House” table with various fields.
Sometimes, a house may have a garden, or garage, a pool etc, so I have separate tables to store the data on each of these, all connected to the “House” table’s unique identifier.
In my code, I have created a “House” class, but how do I then define classes for the other tables?
I could obviously have a “Garden” class, which would inherit the “House” class, but, depending on a visitor’s selections, I may sometimes need to display data on (for example) the house, the garden and the garage and I can’t see how this method would work. I could just have one big class that defines the house, the garden, the garage and so on and leave lots of nulls when something isn’t needed, but I’m pretty sure that can’t be the correct approach!
I’ve been struggling with this all day, so any info is very much appreciated!
House class may have a collection of features.
You can basicly create an abstract base class called “Feature” or an interface called “IFeature” and inherit/implement it to the class that is meant to be a feature (i.e.
Garden).Then all you need to do is creating a collection in
Houseclass called “Features”.Here is an example interface in C#:
Your feature classes need to implement
IFeatureinterface.To implement
IFeature, a class must have adecimalproperty called “Price” with a get accessor like theGardenclass above and thePoolclass below:Houseclass should have a collection ofIFeatureinstead ofPoolorGarden:Then, you can add features to a house like this:
And using LINQ, you can,
More information about interfaces.
More information about LINQ.