MongoDB:规范化存储到嵌入式域模型
我已经确定关系模型对于数据库中的特定集合更有意义。问题是,领域模型最初是从嵌入式模型开始的,并且有大量的 UI 代码期望它采用这种形式。更新数据库架构不是问题,但我很好奇是否有任何简单的方法可以将 C# 域对象重新建模为更老式的关系模型。我已经开始在版本一和版本二之间编写映射器(使用 AutoMapper)(请参阅下面的类),但它很快就会变得混乱。
下面是一些虚构的代码,概述了类似的域模型。
// Existing
class MyClass
{
List<Event> Events { get; set; }
List<Movie> Movies { get; set; }
}
// How it should have been modeled
class MyClass
{
List<int> Events { get; set; } // Stores Event IDs
List<int> Movies { get; set; } // Stores Movie IDs
}
数据库必须标准化。
如果我必须重塑领域模型,那也没关系;我只是想感到舒服,我已经用尽了其他可能节省时间的可能性。对于我忽略的这个问题有一个简单的解决方案吗?
I have determined a relational model makes more sense for a particular collection in a database. The problem is, the domain model was originally started with an embedded model and there is a large amount of UI code that expects it to be in this form. Updating the database schema isn't an issue, but I'm curious if there is any easy way around remodeling the C# domain objects to a more old-fashioned relational model. I've started writing mappers (with AutoMapper) between version one and version two (see classes below), but it's getting messy really quick.
Below is some fictitious code that outlines a similar domain model.
// Existing
class MyClass
{
List<Event> Events { get; set; }
List<Movie> Movies { get; set; }
}
// How it should have been modeled
class MyClass
{
List<int> Events { get; set; } // Stores Event IDs
List<int> Movies { get; set; } // Stores Movie IDs
}
The database will have to be normalized.
If I have to remodel the domain model, that's fine; I just want to feel comfortable I've exhausted other possibilities that might save time. Is there an easy solution to this problem I'm overlooking?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果重组的唯一目的是关系数据库,我建议您研究 O/R 映射。一个 O/R 映射器,例如 NHibernate 或 实体框架,应该能够映射您的现有嵌入模型到关系数据库。使用 O/R 映射器可以消除重构域的需要。
If the only purpose of your restructuring is the relational database I'd advise you to look into O/R mapping. An O/R mapper, like NHibernate or the Entity Framework, should be able to map your existing embedded model to a relational database. Using an O/R mapper can take away the need of remodeling your domain.
考虑到具体问题,我似乎唯一可以证明的两个选项是我在最初的帖子中提到的两个选项(手动映射数据或更改我的域对象)。最终,对我来说,阻力最小的途径是手动绘制数据。我很欣赏pjvds的建议,但我不能考虑到许多其他东西可以更好地与 C# MongoDB 驱动程序配合使用,并且考虑到映射器也不是在项目的这个阶段切换到新的 ORM 是合理的我们数据库的其他部分是必需的。
Given the specific problem, it seemed the only two options I could justify were the two I mentioned in my initial post (map the data manually or change my domain object). Ultimately, for me, the path of least resistance was to map the data manually. I appreciate the suggestion by pjvds, but I couldn't justify switching to a new ORM at this stage of the project considering so many other things work better with the C# MongoDB driver and also considering a mapper isn't necessary for the other portions of our database.