views:

62

answers:

1

Hi guys,

I'm having issues trying to model a new system with nhibernate or even databases.

I have lot of hardware which can be connected via IP or Serial Com Port (maybe more in the future). I want to be able to have different information depending on which one it is.

I was thinking of a ConnectionInformation abstract class with SerialPortConnectionInformation and IPConnectionInformation. The when I get it back, check to see which one is null and connect to the hardware. But this feels wrong I think or impossible?

Any help would be really grateful.

A: 

I think I understand what you're asking about. You have two types of entities that may share some common information between them (in particular, "What am I connected to?"), but have a lot of fundamental differences (I could imagine the IP-based devices having a field for "IP Address" whereas the Serial-based ones wouldn't).

You may want to do some research on the term "single-table inheritance". This is a technique whereby a single table contains all of the fields for both types of entities. Some of these fields will be common to both types, but some of them will only be applicable to one type or the other. Under STI that's expected; you usually have a field which indicates which "type" of entity the record represents, and the fields that aren't applicable will all be set to NULL.

Alternately, you can do a similar thing with multiple tables. You could have a Hardware table to represent the common data, and then IPHardware and SerialHardware tables to represent the data specific to each type. The latter two tables could then have a Foreign Key reference to the Hardware table to make a reference to their own "common" data. Other tables would typically reference the Hardware table and then drill-down into the "subclass" tables depending on their needs & the type of the Hardware.

Craig Walker