开发者

Should Class Structure Mirror DB Structure?

Sorry if this is noobish, but I'm new to development and am teaching myself.

Let's say I've got 2 tables

BioData

Name (string)

Birthdate (datetime)

BloodType (int)

BloodTypes

BloodTypeId (int)

BloodTypeName (string)

When I pull a record, i'll use a join, so that I pull the BloodTypeName and not just ID.

Now, should my class structure mirror these tables, or am I safe to build a "Person" class with

public class Person {
  string Name;
  DateTime Birthdate;
  string BloodType;
}

If I do build class as indicated above, it seems I would have to do some funkiness when it comes to inserting records into by DB because I want to show BloodTypeName to the user, but I have to insert the relevant in开发者_如何学Cteger into the db.

Any insight on best practices would be appreciated.


No, it should definitely not mirror the database structure, if you have any good reason not to.

Sometimes the data classes mirror the database exactly, but that's not because they have to be the same, it's just because there is no reason to make them different. Making the classes mirror the database makes for a simpler data layer, but as soon as there is a reason to model the classes differently, you should consider if mirroring the database makes it harder to use the classes.

In this case it's clearly less efficient if you have to get the blood type name in a separate operation. You might want a blood type class in addition to the person class (for example if you want to populate a dropdown with all possible blood types), but when you fetch the data for the person object you should also fetch the blood type name.

Alternatively, you can fetch both the id and name of the blood type along with the data for the person, and create a blood type object to store in the person object:

public class BloodType {
  int Id;
  string Name;
}

public class Person {
  string Name;
  DateTime Birthdate;
  BloodType BloodType;
}


I would seperate between displaying classes and data classes.

Data classes have to have any and all information present in the database to be able to change it. Now, if you want to display specific information to the user, you can write displaying classes, which just have the specific information needed. Then you simply map them - either yourself or by using a library, like automapper, I use this myself and it is quite handy, and takes a lot of workload of your shoulders.


I am assuming you are using a RDBMS system for your Database and you will use a OOP methodology for your class structure. If you stick with the core principles of those 2 methodologies you will be fine.

There is no need to mix these approaches. So => do not mirror those structures. You are fine building a Person class that way.


Hay andrew.. Yes it should mirror the DB structure,

If you still learning the thing you could start reading about LINQ Tech, which almost do the most of the work you are asking about, Hope the helped, Regards..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜