类结构应该反映数据库结构吗?

发布于 2024-09-28 05:45:31 字数 529 浏览 3 评论 0原文

抱歉,如果这有点新手,但我是开发新手,正在自学。

假设我有 2 个表

BioData
名称(字符串)
出生日期(日期时间)
血型 (int)

血型
BloodTypeId(整数)
BloodTypeName(字符串)

当我提取记录时,我将使用联接,以便提取 BloodTypeName 而不仅仅是 ID。

现在,我的类结构是否应该镜像这些表,或者我可以安全地构建一个“Person”类吗?

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

如果我如上所述构建类,那么当它出现时,我似乎必须做一些有趣的事情通过数据库插入记录,因为我想向用户显示 BloodTypeName,但我必须将相关整数插入数据库。

任何有关最佳实践的见解将不胜感激。

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 integer into the db.

Any insight on best practices would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

_畞蕅 2024-10-05 05:45:31

不,如果您有充分的理由不这样做,它绝对不应该镜像数据库结构。

有时,数据类完全反映数据库,但这并不是因为它们必须相同,而是因为没有理由使它们不同。使类镜像数据库可以使数据层更简单,但是一旦有理由对类进行不同的建模,您就应该考虑镜像数据库是否会使使用这些类变得更加困难。

在这种情况下,如果您必须在单独的操作中获取血型名称,显然效率较低。除了 person 类之外,您可能还需要一个血型类(例如,如果您想用所有可能的血型填充下拉列表),但是当您获取 person 对象的数据时,您还应该获取血型名称。

或者,您可以获取血型的 ID 和名称以及人员的数据,并创建一个血型对象来存储在人员对象中:

public class BloodType {
  int Id;
  string Name;
}

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

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;
}
云朵有点甜 2024-10-05 05:45:31

我会区分显示类和数据类。

数据类必须具有数据库中存在的所有信息才能更改它。现在,如果您想向用户显示特定信息,您可以编写显示类,其中只包含所需的特定信息。然后你只需映射它们 - 自己或使用库,例如​​ automapper,我自己使用它,它是非常方便,并且会减轻您肩膀的负担。

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.

深海蓝天 2024-10-05 05:45:31

我假设您的数据库使用 RDBMS 系统,并且您的类结构将使用 OOP 方法。如果您坚持这两种方法的核心原则,那就没问题。

没有必要混合使用这些方法。所以=>不要镜像那些结构。你可以用这种方式构建一个 Person 类。

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.

忆悲凉 2024-10-05 05:45:31

海伊·安德鲁..
是的,它应该反映数据库结构,

如果您仍在学习您可以开始阅读有关 LINQ Tech 的内容,它几乎完成了您所询问的大部分工作,希望有所帮助,
问候..

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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文