---
title: "ASP.NET设计模式读书笔记（2）---Unit Of Work模式"
author: "dotcpp"
date: "2013-06-04T14:53:50Z"
tags: []
categories: ["企业架构"]
slug: "asp-net-2-unit-of-work"
draft: false
lastmod: "2013-06-04T14:53:14Z"
id: 1881800
url: "https://dotcpp.iteye.com/blog/1881800"
---

Unit Of Work 模式是数据访问层一个理解的重点，用来确保数据的完整性，如果在持久化的过程中出现问题，那么将所有的对象回滚，以确保数据始终处于有状态。书中例子的整体结构图如下：

![](http://dl2.iteye.com/upload/attachment/0085/3730/2165c97d-9940-39a9-9339-8e37430de2ad.jpg)  
 在UnitOfWork类中维护一系列的Dictionary&lt;IAggregateRoot,IRepository&gt;，当在应用服务中AccountService进行Save操作时，只是注册到Dictionary，只有当调用\_unitOfWork.Commit()时才遍历字典，进行真正的持久化操作。执行顺序如下：

![](http://dl2.iteye.com/upload/attachment/0085/3948/45ec6484-f53b-3026-b57d-996e4eb89adc.jpg)