数据映射器模式 [英] Data Mapper Pattern

查看:41
本文介绍了数据映射器模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我一直在所有c#数据库驱动的应用程序中使用Active记录。但是现在我的应用程序需要将我的持久性代码与业务对象分开。我已经阅读了很多有关Martin Fowler数据映射模式的文章,但是我对这种模式的了解仍然非常有限。

Up until now I've been using Active records in all my c# database driven applications. But now my application requires my persistence code being split from my business objects. I have read a lot of posts regarding Martin Fowler's data mapping pattern, but my knowledge of this pattern is still very limited.

让我们使用以下示例:

如果我有2个表-Customer和CustomerParameters。 CustomerParameters表包含用于创建新客户的默认客户值。

If I have 2 tables - Customer and CustomerParameters. The CustomerParameters table contains default Customer values for creating a new Customer.

然后,我将不得不创建一个CustomerMapper类来处理所有Customer持久性。然后,我的Customer和CustomersList类将与此映射器类进行协作,以保留客户数据。

I will then have to create a CustomersMapper class to handle all of the Customer persistence. My Customer and CustomersList class will then collaborate with this mapper class in order to persist customer data.

我有以下问题:


  1. 如何将原始数据传输到&从我的客户类到映射器,而又不违反某些业务规则? DTO?

  1. How would I transfer raw data TO & FROM my Customer class to the mapper without breaking certain business rules? DTO's?

在我的Mapper类中具有SaveAll和LoadAll方法以更新和加载多个客户的数据是否可以接受?如果是这样,那么在SaveAll的情况下,映射器将如何知道何时更新或插入数据?

Is it acceptable to have a SaveAll and LoadAll method in my Mapper class for updating and loading multiple customers' data? If so, in case of SaveAll, how will the mapper know when to update or insert data?

客户映射器类将负责检索默认值还是从CustomerParameters表中创建,还是创建一个CustomerParameters映射器会更好?

Will the Customer mapper class be responsible for retrieving the default values from the CustomerParameters table as well, or will it be better to create a CustomerParameters mapper?

AO / R映射器工具真的不在这里。我正在使用的数据库是Transactional,需要我编写自己的Mapper模式。

A O/R mapper tool is not really here. The database I'm using is Transactional and requires that I write my own Mapper Pattern.

任何想法和评论将不胜感激。

Any ideas and comments will be greatly appreciated.

推荐答案

Shaun我将以这种方式回答您的问题:

Shaun I would answer your questions this way:

ad 1)Mapper负责创建客户目的。您的Mapper对象将具有类似RetrieveById方法的内容(例如)。它将接受一个I​​D,并以某种方式(不是Mapper对象的职责)构造有效的Customer对象。反之亦然。当您使用有效的Customer对象调用Mapper.Update方法时,Mapper对象负责确保所有相关数据都被持久保存(在适当的地方-db,内存,文件等)。

ad 1) Mapper is responsible for creating Customer object. Your Mapper object will have something like RetrieveById method (for example). It will accept an ID and somehow (that't he responsibility of the Mapper object) construct the valid Customer object. The same is true the other way. When you call Mapper.Update method with a valid Customer object, the Mapper object is responsible for making sure that all the relevant data are persisted (wherever appropriate - db, memory, file, etc.)

ad 2)如上文所述,retrieve / persist是Mapper对象上的方法。提供此类功能是其责任。因此,LoadAll,SaveAll(可能传递值对象数组)是有效的Mapper方法。

ad 2) As I noted above retrieve/persist are methods on Mapper object. It is its responsibility to provide such a functionality. Therefore LoadAll, SaveAll (probably passing an array of value objects) are valid Mapper methods.

ad 3)我会说是。但是您可以将Mapper对象的各个方面划分为不同的类(如果需要/需要):默认值,规则验证等。

ad 3) I would say yes. But you can separate various aspects of Mapper objects into separate classes (if you want to/need to): default values, rule validation, etc.

我希望它会有所帮助。我真的建议/建议您阅读Martin Fowler的书企业应用程序模式建筑

I hope it helps. I really suggest/recommend you to read Martin Fowler's book Patterns of Enterprise Application Architecture.

这篇关于数据映射器模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆