开发者

Object Relational Mapping between a Database and the Domain model

开发者 https://www.devze.com 2023-02-20 09:50 出处:网络
What rules should i follow in order map efficiently entities from my domain model to a database table?

What rules should i follow in order map efficiently entities from my domain model to a database table?

Should database columns be aligned with domain class properties?

For example i have a Database table

Person {(varchar)FirstName, (varchar)LastName, int(age)}

And a Domain class

class Person(){

String firstN开发者_如何学Came;
String lastName;
int age;
}
  • Should the class and the table contain the same number of properties
  • Should they be of the same corresponding type (e.g : varchar - string)
  • What are the best practices
  • What to avoid?


There are many strategies for object-relational mapping. Ultimately you have to represent every piece of persistent data in a field somewhere, so your strategy will work. Other questions you'll have to answer:

1) How are your records keyed? Are you going to have a unique ID field for each class?

2) How will you handle inheritance? If you have a PersonA(Person) class that introduces an additional field, will you create a whole new table for every field in Person + PersonA, or just have a PersonA table for the additional field?

3) How are you planning to construct queries against the database? Are you going to provide some form of object-query mapping?

There are many open source ORMs out there, so unless you're doing this for a research project, I'd advise that you use one of them.

0

精彩评论

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