1.Identity Map模式简介

  Identity Map(标识映射)模式是通过将所有已加载对象放在一个映射中确保所有对象只被加载一次,并且在引用这些对象时使用该映射来查找对象。在处理数据并发访问时,要有一种策略让多个用户共同影响同一个业务实体,这个固然很重要。同样重要的是,单个用户在一个长运行事务或复杂事务中始终使用业务实体的一致版本。Identity Map模式提供的功能;为事务中使用所有的业务对象均保存一个版本,如果一个实体被请求两次,返回同一个实体。
  每个业务事务使用一个Identity Map,可以确保如果在同一个事务中两次检索同一个实体,则该实体将是唯一的,而且包含该事务所做的任何修改。

  2.Identity Map模式示例

  代码结构:

  Employee.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DataAccessPatterns.IdentityMapPattern.Model
{
public class Employee
{
public Guid ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
}

  IEmployeeRepository.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DataAccessPatterns.IdentityMapPattern.Model
{
public interface IEmployeeRepository
{
Employee FindBy(Guid ID);
}
}

  IdentityMap.cs:   该类使用泛型提供类型安全的Identity Map实现,用于在业务事务期间提供唯一的实体。Identity Map包含一个散列表来存储事务中使用的业务实体,并提供简单的接口来存储和检索实体。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace DataAccessPatterns.IdentityMapPattern.Repository
{
public class IdentityMap<T>
{
Hashtable entities = new Hashtable(); public T GetByID(Guid id)
{
if (entities.ContainsKey(id))
{
return (T)entities[id];
}
else
{
return default(T);
}
} public void Store(T entity, Guid key)
{
if (!entities.ContainsKey(key))
{
entities.Add(key, entity);
}
}
}
}

  EmployeeRepository.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using DataAccessPatterns.IdentityMapPattern.Model; namespace DataAccessPatterns.IdentityMapPattern.Repository
{
public class EmployeeRepository : IEmployeeRepository
{
private IdentityMap<Employee> _employeeMap; public EmployeeRepository(IdentityMap<Employee> employeeMap)
{
_employeeMap = employeeMap;
} public Employee FindBy(Guid Id)
{
Employee employee = _employeeMap.GetByID(Id); if (employee == null)
{
employee = DatastoreFindBy(Id); if (employee != null)
{
_employeeMap.Store(employee, employee.ID);
}
} return employee;
} private Employee DatastoreFindBy(Guid Id)
{
Employee employee = default(Employee); // Code to hydrate employee from datastore...
return employee;
}
}
}

  调用FindBy方法时,Employee Repository首先检查IdentityMap以确定之前是否已经检索Employee实体。如果是,则将其返回给调用者。如果没有,则使用Employee实例的标识从数据存储中查询该实例,然后将其添加到IdentityMap中,如果再次需要从Employee Repository中检索同样的Employee实体,就可以直接使用它。

数据访问模式:Identity Map(标识映射)模式的更多相关文章

  1. Razor视图引擎布局 Razor视图引擎的基本概念与法语 SQL Server Mobile 和 .NET 数据访问接口之间的数据类型映射 binary 和 varbinary datetime 和 smalldatetime float 和 real

    Razor视图引擎布局   不需要像过去aspx一样,使用.Master文件,而是统一使用.cshtml 或 .vbhtml文件.但文件名一般以 _开头,这样做文件不会当做View显示出来 使用@Re ...

  2. 【串线篇】SpringBoot数据访问【数据源/mybatis/指定映射文件位置】

    一.配置数据源 1.1.jdbc版本 JDBC(.tomcat.jdbc.pool.DataSource作为数据源) <?xml version="1.0" encoding ...

  3. SQL Server Mobile 和 .NET 数据访问接口之间的数据类型映射

      .NET 数据类型 SQL Server Mobile 数据类型 binary varbinary boolean bit byte tinyint byte[] varbinary dateti ...

  4. Entity Framework中的Identity map和Unit of Work模式

    阅读目录: 一.什么是Identity map模式 二.关于Identity map模式的验证示例 三.Unit of Work 模式 四.总结和注意的问题 一,什么是Identity map模式 I ...

  5. Entity Framework中的Identity map和Unit of Work模式(转)

    一,什么是Identity map模式 Identity map是EF获取和缓存数据的模式.Identity map模式指的是任何数据都只会被加载一次,以map的形式缓存,以唯一的identity来再 ...

  6. Windows GDI 映射模式(出自:Windows程序设计第5版-珍藏版)

    GDI映射模式(mapping mode):和映射模式紧密相关的还有4个其它的设备环境属性:1.窗口原点(window origin)2.视口原点(viewport origin)3.窗口范围(win ...

  7. 一个类GraphQL的ORM数据访问框架发布

    Zongsoft.Data 发布公告 很高兴我们的 ORM 数据访问框架(Zongsoft.Data)在历经两个 SaaS 产品的应用之后,今天正式宣布对外推广! 这是一个类 GraphQL 风格的  ...

  8. PHP 设计模式 笔记与总结(10)数据对象映射模式 2

    [例2]数据对象映射模式结合[工厂模式]和[注册模式]的使用. 入口文件 index.php: <?php define('BASEDIR',__DIR__); //定义根目录常量 includ ...

  9. PHP 设计模式 笔记与总结(9)数据对象映射模式

    [数据对象映射模式] 是将对象和数据存储映射起来,对一个对象的操作会映射为对数据存储的操作.例如在代码中 new 一个对象,使用数据对象映射模式就可以将对象的一些操作比如设置一些属性,就会自动保存到数 ...

随机推荐

  1. linux菜鸟日记

    本地yum源的安装: 要安装本地yum源,首先需要熟悉本地yum文件的配置和光盘的挂载 第一步挂载光盘: 首先需要指定一个光盘挂载目录 通常情况下我习惯使用默认挂载目录,所以一般我使用的光盘挂载命令是 ...

  2. iOS 中自定义 cell,点击cell的时候文字不出现的原因

    解决方案: 在setSelected方法中设置要显示label的背景颜色即可

  3. 利用js查找页面中的内链,外链

    起初没听说过内链外链,只有链接锚文本,在面试中被问到如何查找到页面中的内链和外链,就在想,什么是内链和外链啊??????? 后来面试官给我解释了一下他们的区别,自己稍微懂了,自己当时回答的是通过获取a ...

  4. 关于i和j

    算法课无聊随手写了段c代码,发现了个问题,就要下课了,先记一下 for(int i = 0; i < 100; i ++) for(int j = 0; j < 100000; j ++) ...

  5. linux ping加执行时间

    [root@back_zabbix_100 ~]# ping 10.10.30.250 | awk '{print $0"\t" strftime("%H:%M:%S&q ...

  6. (转)Vsdocman7.2 注册版

    Vsdocman是一个优秀的.NET源代码注释编写工具,方便的以GUI的方式设计.NET源代码的注释.我们只是大自然的搬运工:http://download.csdn.net/detail/iamyg ...

  7. EXT 下拉框事件

    1. <ext:ComboBox ID="cbline" FieldLabel="平台部门来源" runat="server" Dis ...

  8. java的jxl技术导入Excel

    项目结构: http://www.cnblogs.com/hongten/gallery/image/112177.html 在项目中我们看到Reference Libraries中的jxl.jar包 ...

  9. 免费SVN服务器笔记

    前言: 笔者有个项目,需要类似公司一样进行源代码管理.鉴于很多团队一样,对资金资源限制,只能寻找免费的SVN服务器 于是在BD上搜索一大推资料,很多都是google的,但是GG经常无法正常访问,给项目 ...

  10. SDOI 2016 游戏

    树链剖分 线段树维护区间最小值,区间最大值 更新,对于每一个区间,找到当前区间的最小值的最大值,和要更新的值比较,如果比最大值还大,则此数对于以后的询问无任何贡献,直接返回即可,若有贡献,则一直递归到 ...