存储库; POCO / Linq-to-Sql 实体类之间的映射

发布于 2024-10-13 09:58:18 字数 277 浏览 6 评论 0原文

我正在使用 Sql Express 制作我的第一个数据库程序。目前,我正在使用 Linq-to-Sql 进行数据访问,并且我的存储库类返回“实体”类型对象。意义;我扩展了 dbml 实体类以用作我的业务对象类。现在我想让它更加分离;并拥有 POCO 业务对象。

这就是我想知道可能存在哪些不同的解决方案的地方。在我看来,我需要在存储库中手动将属性逐个属性、每个实体类映射到域类。到目前为止,我有大约 20 个表,总共几百列。现在..我只想验证这是否是您仍在使用的常见/典型方法?如果有替代方案而不引入过度复杂性,那会是什么?

I'm making my first database program, with Sql Express. Currently I'm using Linq-to-Sql for data access, and my repository classes return "entity" type objects. Meaning; I extend the dbml entity classes to use as my business object classes. Now I want to make this more separated; and have POCO bussiness objects.

This is where I wonder about what different solutions may exist. It looks to me like I need to manually map property-by-property, each entity class into domain class, in the repositories. I have so far about 20 tables with total few hundred columns. Now.. I just want to verify if this is a common/typical approach that you still use? And if there are alternatives without introducing excessive complexity, what would that be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

○闲身 2024-10-20 09:58:18

在手动创建映射之前,请查看 AutoMapper

AutoMapper 是一个对象-对象映射器。
对象-对象映射的工作原理
转换一个输入对象
输入到 a 的输出对象中
不同类型。是什么让 AutoMapper
有趣的是它提供了一些
有趣的惯例
弄清楚如何做是一件肮脏的工作
将类型 A 映射到类型 B。只要键入
B 遵循 AutoMapper 的既定
约定,几乎零配置
需要映射两种类型。

Before creating your mappings manually, have a look at AutoMapper

AutoMapper is an object-object mapper.
Object-object mapping works by
transforming an input object of one
type into an output object of a
different type. What makes AutoMapper
interesting is that it provides some
interesting conventions to take the
dirty work out of figuring out how to
map type A to type B. As long as type
B follows AutoMapper's established
convention, almost zero configuration
is needed to map two types.

歌入人心 2024-10-20 09:58:18

AutoMapper 是一个很好的工具,可以用来执行类到类的转换。然而,我正在考虑结合 Linq2Sql 和 AutoMapper 的 DAL,并且我在想为什么不直接使用 Fluent NHibernate?它非常容易设置,适用于几乎任何数据库,包括 SqlExpress,并且有一个可以无缝集成的 Linq 提供程序。所有这些都是免费的开源代码,并且非常常用,因此有充足的文档和支持。

如果您想继续使用 Linq2Sql 但拥有功能更齐全的域模型,您可以考虑从 DTO 派生域模型。这将允许您在域中拥有业务逻辑,并将属性传递给 DTO。但是,请了解 Linq2SQL 对象将无法直接转换为域对象;您需要在域中使用一个构造函数,该构造函数接受 DTO 并将信息复制到域中(至少需要 DTO 到域的单向映射)。然而,域可以被视为 DTO(因为类始终是其父类),因此不需要反向转换;只需将域类传递到需要 DTO 的存储库即可。

AutoMapper is a good tool to use to perform class-to-class conversions. However, I'm thinking of a DAL that combines Linq2Sql and AutoMapper, and I'm thinking why not just go with Fluent NHibernate? It's very easy to set up, works on just about any database including SqlExpress, and there is a Linq provider that integrates pretty seamlessly. All of this is free open-source code, and very commonly-used so there's ample documentation and support.

If you want to stay with Linq2Sql but have a more full-featured domain model, you could consider deriving your domain model from the DTOs. That would allow you to have the business logic in the domain, with the properties passed up to the DTO. However, understand that the Linq2SQL objects will not be able to be directly cast to domain objects; you'll need a constructor in the domain that takes a DTO and copies the info into the domain (requiring at least a one-way mapping of DTO to domain). However, the domain can be treated like a DTO (because a class is always its parent) so the reverse conversion isn't necessary; just hand the domain class to the repository where it would expect the DTO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文