如何设计对象?与 ORM 的关系
自从看到下面的示例代码后,我一直对 ORM 感到困惑:
public class Article
{
public List<Comment> Comments;
public void AddComment(Comment comment)
{
Comments.Add(comment);
}
// I'm surprised by this kind of operation
// how much performance hit it should be
public void Save()
{
//update the article and all its comments
}
}
根据我的想法,保存注释的责任应该分配给注释本身:
public class Comment
{
public Article BelongArticle;
//I think this is better/direct than use object Article,
//But it's based on the consideration of database structure,
//I was told one should "forget" the database, but it's really hard
public int ArticleId;
public void Save()
{
//save the comment directly
}
}
I have been confused about ORM since I see the following sample code:
public class Article
{
public List<Comment> Comments;
public void AddComment(Comment comment)
{
Comments.Add(comment);
}
// I'm surprised by this kind of operation
// how much performance hit it should be
public void Save()
{
//update the article and all its comments
}
}
According to what I think, the responsiblity of saving comment should be assigned to the comment itself:
public class Comment
{
public Article BelongArticle;
//I think this is better/direct than use object Article,
//But it's based on the consideration of database structure,
//I was told one should "forget" the database, but it's really hard
public int ArticleId;
public void Save()
{
//save the comment directly
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在没有任何实际依据的情况下得出结论,因为您只是查看一些示例代码,而不考虑实际可能发生的情况。
使用 ORM 的全部意义在于,当您在应用程序中以面向对象而不是关系方式工作时,您可以允许它处理数据库事务。当您仅通过查看 Article.Save 对 Article 进行更新时,您确实无法说明 ORM 的执行情况。 Article.Save 是一个 OO 构造,ORM 在数据库上实际执行的是一个关系操作。 Article.Save 为何让您认为它效率低下?查看它不会给您任何信息。您必须查看所选的 ORM 在数据库上执行的操作。
假设 Article 是一个新对象。在这种情况下,您必须保存文章,在评论中设置外键,然后保存评论。您的“首选”代码不会显示完整的操作,但它仍然必须发生。不同之处在于 ORM 为您提供了一种面向对象的方法来执行此操作 - 只需调用文章上的 save 即可。无论哪种方式,在幕后都必须进行相同的操作。也许 ORM 需要的步骤比您手动完成的步骤要多,但也可能不是。
假设 Article 不是一个新对象。您添加一条新评论。根据平台的不同,当您这次调用 Save 时,ORM 发生的情况可能与您认为更好的情况没有什么不同,具体取决于代码的编写方式。如果文章中没有任何内容需要更新,ORM 可以简单地保存评论。
ORM 使用各种方法,但通常它们维护需要更新的对象的某种运行帐户。
您不能仅仅说第一种方法在某种程度上效率低下,因为您在 Article 而不是 Comment 上调用了方法 - 实际发生的情况将取决于您使用的特定 ORM 平台以及对象的状态。
You are reaching conclusions without any real basis because you are just looking at some sample code and not considering what actually might be happening.
The whole point of using an ORM is so you can allow it to handle the database transactions while you work in an object oriented rather than a relational fashion in your application. You really cannot say anything about how the ORM performs when you do an update on Article by just looking at Article.Save. Article.Save is an OO construct, and what the ORM actually executes on the database is a relational action. What about Article.Save makes you think it is inefficient? Looking at that does not give you any information. You would have to look at what the ORM of choice is doing on the database.
Suppose the Article is a new object. In this case you have to save the Article, set the foreign key in the Comment, and then save the comment. Your "preferred" code does not show the full operation but it still must occur. The difference is the ORM gives you an object oriented way to do this - just call save on the article. Under the hood the same operations must occur either way. Maybe the ORM takes more steps than you could do it in manually, but maybe not.
Suppose Article is not a new object. You add a new Comment. Depending on the platform, when you call Save this time potentially what happens with an ORM could be no different than what you might think as better depending on how your code is written. If there is nothing that needs to be updated in the Article, the ORM may simply save the Comment.
ORMs use various methods, but in general they maintain some kind of running account of objects that need to be updated.
You cannot just say that the first approach is somehow inefficient just on face because you called a method on Article instead of Comment - what is actually happening will depend on the specific ORM platform you use as well as the state of the objects.