在解决方案中包含 POCO、实体和 ViewModel 感觉有点大材小用
最新版本的实体框架让我爱上了它,尽管它仍然很好,但我不喜欢使用实体对象作为域对象来解决所有明显的问题,所以我正在做的是在我的服务中翻译检索到的实体对象,将 POCO 返回给使用该服务的任何人。感谢 automapper,从 pocos 到实体的转换以及返回结果在服务内产生了一些易于维护的代码。事情变得棘手的是,当您将 ViewModel 添加到图片中,最终将视图模型映射到控制器中的 poco,然后将 poco 映射到服务中要存储在存储库中的实体对象时。
你会认为这太过分了还是我太挑剔了?
The latest version of the Entity Framework got me in love, still at good as it is I don't like using entities objects as domain objects for all the obvious headaches, so what I'm doing is translating retrieved entity objects in my services and returning POCOs to whomever consumes the service. Thanks to automapper the translation from pocos to entities and back results in some easily maintainable code inside the services. Where things get hairy is when you add ViewModels to the picture and you end up mapping a viewmodel to the poco in the controller and then mapping the poco to the entity object in the service to be stored in the repository.
Would you consider this an overkill or am I being too nitpicking ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是“过度杀伤”,而是“开销”。当你想要适当的分层时,这是不可避免的。 ViewModel 属于 UI 层,您不想将其与 POCO 混合。
虽然您可能会称其对于小型应用程序来说“太过分了”,但您宁愿牺牲哪个边界?
It's not 'overkill' but 'overhead'. And It is inevitable when you want proper layering. The ViewModels belong to the UI layer, you don't want to mix that with the POCOs.
And while you might call it 'overkill' for a small app, which boundary would you rather sacrifice?
Entity Framework 4 内置了对 POCO 的支持。一个很好的起点是 链接
简而言之,您可以让您的域模型项目完全不知道持久性。您可以将此方法与 EF 4 中新引入的 Code First 方法结合使用。现在,您不再需要 Automapper 来进行 POCO 到实体对象的转换,反之亦然。但是,您可以使用 Automapper 在 POCO 和 ViewModel 之间进行映射,从而减少大量管道代码。
HTH。
Entity Framework 4 has built-in support for POCO's. A good place to start on that is Link
In short, you can have your domain model project that is completely persistence ignorant. You can use this approach with the newly introduced Code First approach in EF 4. Now you don't need Automapper anymore for your POCO's to Entity Objects conversion and vice versa. You can, however, use Automapper to map between POCO's and ViewModels reducing a lot of the plumbing code.
HTH.