ORM要完成对象的初始化以及CRUD功能。在这些功能中尤其是query和update已经蕴含了相应的map的功能。除此之外还要提供transaction和concurrency的功能。这些基本的功能很好理解,不过相应的实现是比较复杂的。还要用到许多模式。
-
Unit of Work: A Unit of Work(184)keeps track of all objects read from the database, together with all objects modified in any way. It also handles how updates are made to the database. Insteadof the application programmer invoking explicit save methods, the programmer tells the unit of work to commit. That unit of work then sequences all of the appropriate behavior to the database, putting all of the complex commit processing in one place. Unit of Work(184) isan essential pattern whenever the behavioral interactions with the databasebecome awkward。
Unit of Work不仅是减少程序跟数据库之间的round trip。有时候还是业务逻辑的需要。一个事物查询一批数据,更改其中的一些 数据,最后提交。而不是每次仅更改一个数据就提交。
.NET中的DataSet就是Unit of Work的一个实例。
- Identity Map:Ensures that each object gets loaded only once by keeping every loaded object in a map.
Identity Map最好位于Unit of Work 中。A common practice is to place anIdentityMap (195) within a Unit of Work。
--Unit of Work的主要职责是transaction and concurrency。
--Data Mapper的主要职责是对象与数据库之间的映射,从DAL的角度看就是CRUD操作。