OO和SQL [英] OO and SQL

查看:105
本文介绍了OO和SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试了解程序对象与表中的数据之间的相关性(这里: OO程序和SQL数据库),我仍然不太明白,有一个强烈的意见分歧揭示了使用SQL数据库是否可以存储OO程序的数据。

In trying to understand the correlation between program objects and data in tables (here:OO program and SQL database), which I still don't really understand, a strong difference of opinion was revealed over whether it's ok to use a SQL db to store data for an OO program.


  1. 对于一个新的程序员,是否可以/推荐?

  1. For a new programmer, is it ok/recommended, or not?

如果没有,OO程序是替代程序程序吗?

If not, is the alternative to an OO program a procedural program?

另外我不明白 对象关系阻抗不匹配 ,其中是什么人意味着,当他们说使用SQL DB存储OO程序的数据是不行的:有人可以总结一下,还是有一个简单的例子来说明? p>

Also I don't understand Object-relational impedance mismatch, which is what people mean when they say that it's "not OK to use an SQL DB to store data for an OO program": can someone summarize it, or is there a simple example that illustrates it?

推荐答案


对于一个新的程序员,是否可以/推荐? $ b

For a new programmer, is it ok/recommended, or not?

这个问题意味着系统设计/组件/架构被选择/意图使程序员受益。

That question implies that the system design/components/architecture are chosen/intended to benefit the programmer.

而不是选择设计/组件/架构来使系统(以及系统的所有者,用户和操作员)受益,并确保程序员知道(这可能需要一些学习或培训)他们需要什么为了开发该系统。

Instead I prefer to choose the design/components/architecture to benefit the system (and the system's owners, users, and operators), and ensure that the programmers know (which may require some learning or training on their part) what they need in order to develop that system.

事实是:


  • OO是通常是开发软件的好方法

  • SQL数据库通常是存储数据的好方法

  • 设计从SQL到OO的映射可能是非法的,简单的


如果没有,OO程序是替代程序程序吗?

If not, is the alternative to an OO program a procedural program?

嗯,也许,是的:OO的一个功能是子类化,子类/继承是模型/存储的问题之一SQL数据库。

Well, maybe, yes: one of the features of OO is subclassing, and subclasses/inheritance is one of the things that's problematic to model/store in a SQL database.

例如,给出这样一个OOD ...

For example, given a OOD like this ...

class Animal
{
  int id;
  string name;
  abstract void eat();
  abstract void breed();
}

class Dog : Animal
{
  bool pedigree;
  override void eat() {...}
  override void breed() {...}
}

class Bird : Animal
{
  bool carnivore;
  int numberOfEggs;
  void fly() {...}
  override void eat() {...}
  override void breed() {...}
}

...使用2个SQL表存储此数据不是很明显,或3.如果您采用子类化:

... it isn't obvious whether to store this data using 2 SQL tables, or 3. Whereas if you take the subclassing away:

class Dog
{
  int id;
  string name;
  bool pedigree;
  void eat() {...}
  void breed() {...}
}

class Bird
{
  int id;
  string name;
  bool carnivore;
  int numberOfEggs;

  void fly() {...}
  void eat() {...}
  void breed() {...}
}

...那么它更容易/更明显/更多1到1 /更准确的建模这个数据使用两个表。

... then it's easier/more obvious/more 1-to-1/more accurate to model this data using exactly two tables.


另外我不明白对象关系阻抗不匹配

Also I don't understand Object-relational impedance mismatch

这是一篇比维基百科文章更长更有名的文章;也许这更容易理解:计算机越南科学

Here's an article that's longer and more famous than the Wikipedia article; maybe it's easier to understand: The Vietnam of Computer Science

请注意,针对该问题提出的解决方案之一是:

Note that one of the solutions, which is proposes to the problem, is:


手动映射。开发人员只需
即可接受手工解决这个难题,
并写直接关系访问
代码返回关系到
语言,访问元组,
根据需要填充对象。

"Manual mapping. Developers simply accept that it's not such a hard problem to solve manually after all, and write straight relational-access code to return relations to the language, access the tuples, and populate objects as necessary."

换句话说,在实践中这不是一个难题。这在理论上是一个难题,也就是很难编写一个自动创建映射的工具,而不需要考虑它;但是编程的许多方面都是这样,不仅仅是这一个。

In other words, it's not such a hard problem in practice. It's a hard problem in theory, i.e. it's hard to write a tool which creates the mapping automatically and without your having to think about it; but that's true of many aspects of programming, not only this one.

这篇关于OO和SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆