Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database model to object oriented design?

How would I design classes say in c# to represents a database model?

Given the following tables and fields for a database,

Table: Employee

Pk EmpID
   Lname
   Fname
   Adress
Fk DeptID

Table: Department

Pk DeptID
   DeptName
   Location

Ok so now I want to make 2 classes in C#, one for Employee and one for Department. The part I get hung up on is the foreign keys. Should I use the foreign keys in my design for the objects, or should I put a reference in my Employee class of the Department,or should I put a list of employee references in my Department class,or should I do both? I know if I use foreign keys, it will be less efficient because I will have to search through a list of primary keys that match the foreign key but I probably should include them in the design anyway.

like image 513
SupermansTshirt Avatar asked Jul 06 '11 23:07

SupermansTshirt


2 Answers

Don't use "foreign keys" in your objects. Use references; each Department should have a list of Employees. Depending on whether or not you have a need to backreference from Employee to their Department, make the determination as to whether Employee will have a reference to Department.

like image 142
Paul Sonier Avatar answered Sep 28 '22 13:09

Paul Sonier


Elaboration on @Paul Sonier answer...

P.S. I'm using business layer, business classes in a general sense, not as jargon of some particular technical design pattern.

The specific problems of using database keys
Using database keys is going to cause an explosion of coding overhead to keep objects and the database in synch. As the need to add, change, delete objects happens (via user GUI) you'll be jumping through hoops like crazy. How would you create a child object when the parent object does not exist yet in the database? Imagine trying to do this with any N-level data structure.

Always design business classes without regard to data storage
Business layer classes should faithfully reflect the business rules, jargon, concepts, and context. Polluting this "idea space" with non-business stuff with details about storing or displaying data is bad in the long run. Hear me now and believe me later.

Business classes based on some particular database table layout (and it's keys, etc.) is going to make it disastrously difficult to write code for validating rules, creating proper state of those objects, and so on. This is on top of the problem of keeping the object IDs in synch w/ the database.

Maximize Decoupling of Business layer and Data layer
The example shown in your question is a tempting deception. Some of your business classes may map very well to your database design. And consequently the primary and foreign keys may seem to fit as well.

BUT in any non-trivial application the database model will deviate. If not now, later. It will deviate for the purposes of database integrity, efficiency, and speed. And what does this have to do with the business model? Nothing.

Indicators that you're doing things right

  1. You can instantiate a business object without an existing database

  2. Each object can reference it's "children" without requiring special keys created outside the business class model.

  3. Each business object on its own can validate, enforce, flag, etc. all of it's own rules, even trivial ones like "can't be blank". Business rule validation for composite classes/objects is class design & analysis activity - not database design activity.

like image 32
radarbob Avatar answered Sep 28 '22 13:09

radarbob