我想知道为我的项目设计域对象的最佳方式是什么。
领域对象应该对 ORM/Framework 甚至数据库技术(MySQL、MSSQL 甚至 Mongo)一无所知
下一个问题出现在脑海中,假设我有两个对象,Post 和 Comment。
这是 Post 对象的代码:
class Post {
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public IList<Comment> Comments { get; set; }
}
现在,我对 Comment 对象的问题是它应该包含 Post Id 还是仅包含 Post 对象?
class Comment {
public int Id { get; set; }
public string Content { get; set; }
public Post Post { get; set; }
public int PostId { get; set; } // Should i include it?
}
包含关系对象的 Id 还是仅包含其域对象是一种好习惯吗?
另一个问题是没有像 mongodb 这样的 sql 数据库。如果我将数据存储在 mongodb 中,Comment 对象将引用什么 Post 对象?在mongodb中,帖子和评论之间没有关系,任何帖子都包含评论列表,但评论本身不知道他们所属的帖子。那么,使这些领域类与基于 Sql 的数据库和 NoSql 数据库兼容的最佳方法是什么?
谢谢