博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据访问模式:Identity Map(标识映射)模式
阅读量:7218 次
发布时间:2019-06-29

本文共 3025 字,大约阅读时间需要 10 分钟。

  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; }    }}
View Code

  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);    }}
View Code

  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
{ 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
_employeeMap; public EmployeeRepository(IdentityMap
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; } }}
View Code

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

转载于:https://www.cnblogs.com/libingql/p/3874780.html

你可能感兴趣的文章
六种排序算法C语言版(上)
查看>>
292. Nim Game(easy)
查看>>
ERROR 1786 (HY000)
查看>>
Kubernetes 学习7 Pod控制器应用进阶2
查看>>
Python字符串相加以及字符串格式化
查看>>
11.08 轮换行值
查看>>
AIX lsof 命令
查看>>
微信小程序个人项目(node.js+koa2+koa-router+middleware+mysql+node-mysql-promise+axios)
查看>>
C#温故而知新学习系列之面向对象编程—类的数据成员(三)
查看>>
列表字典推导式
查看>>
HDOJ 1228 A+B(map水题)
查看>>
intellij IDEA 导入包的方法·
查看>>
Python之路番外:PYTHON基本数据类型和小知识点
查看>>
转:matlab+spider+weka
查看>>
步步为营 .NET 设计模式学习笔记 十五、Composite(组合模式)
查看>>
angular通过路由实现跳转 resource加载数据
查看>>
python try except, 异常处理
查看>>
字符串中的各种方法
查看>>
创建文件夹、新建txt文件
查看>>
js form表单 鼠标移入弹出提示功能
查看>>