在DDD设计中大家都会使用Repository pattern来获取domain model所需要的数据。

1.什么是Repository?

"A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection.

 Client objects construct query specifications declaratively and submit them to Repository for satisfaction.

 Objects can be added to and removed from the Repository, as they can from a simple collection of objects,

 and the mapping code encapsulated by the Repository will carry out the appropriate operations behind the scenes. 

Conceptually, a Repository encapsulates the set of objects persisted in a data store and the operations performed over them, 

providing a more object-oriented view of the persistence layer. Repository also supports the objective of achieving a clean separation and one-way 

dependency between the domain and data mapping layers."

按照最初提出者的介绍,它是衔接数据映射层 和域之间的一个纽带,作用相当于一个在内存中的域对象集合。客户端对象把查询的一些实体进行组合,并把它 们提交给Repository。对象能够从 Repository中移除或者添加,就好比这些对象在一个Collection对象上就行数据操作,同时映射 层的代码会对应的从数据库中取出相应的数 据。

从概念上讲,Repository是把一个数据存储区的数据给封装成对象的集合并提供了对这些集合的操作。。。。。。。

在领域驱动设计中,我们有个集合(aggregate)的概念,集合是:

"A cluster of associated objects that are treated as a unit for the purpose of data changes.

 External references are restricted to one member of the Aggregate, designated as the root. 

A set of consistency rules applies within the Aggregate's boundaries.".

通常我们是对于domain的每个集合会对应的定义一个repository。也就说,并不是每个实体都会有对应的一个repository。

Repository的接口一般情况下是作为domain model的一部分,但是严格意义上讲它不属于domain model。

当我们处理aggregates时,大部分时间我们需要3个常用的相关操作。

1.通过Id得到对应的集合·。

2.向repository添加一个集合。

3.从repository中移除一个集合。

比如我们有个Order表,

那么它的Repository接口IRepository(包含三个基本的方法)如下:

假设我们还有另外一个domain model是Product,它的类如下:

它的Repository接口IProductRepository如下:

可以看出来我们的两个接口其实有很多代码是重复的,所以可以来个基本的接口IRepository,并结合System.Collection.Generic中的Generic属性来设计IRepository的代码:

对应的IProductRepository和IOrderRepository代码也得修改:

为了能够测试我们的Repository,我们这里创建一个Repository继承IProductRepository接口:

在构造函数中我们先添加product,然后再实现接口的几个方法。下面来进行单元测试:

运行后可以通过。。。

以上转载:《果子的天空

Repository 设计模式介绍的更多相关文章

  1. Repository 设计模式介绍(转)

    在DDD设计中大家都会使用Repository pattern来获取domain model所需要的数据. 1.什么事Repository? "A Repository mediates b ...

  2. java23种设计模式—— 一、设计模式介绍

    Java23种设计模式全解析 目录 java23种设计模式-- 一.设计模式介绍 java23种设计模式-- 二.单例模式 java23种设计模式--三.工厂模式 java23种设计模式--四.原型模 ...

  3. Repository模式介绍汇总

    1.Linq To Sql中Repository模式应用场景 http://www.cnblogs.com/zhijianliutang/archive/2012/02/24/2367305.html ...

  4. JavaBean中DAO设计模式介绍(转)

    一.信息系统的开发架构 客户层-------显示层-------业务层---------数据层---------数据库 1.客户层:客户层就是客户端,简单的来说就是浏览器. 2.显示层:JSP/Ser ...

  5. JavaBean中DAO设计模式介绍

    一.信息系统的开发架构 客户层-------显示层-------业务层---------数据层---------数据库 1.客户层:客户层就是client,简单的来说就是浏览器. 2.显示层:JSP/ ...

  6. 设计模式——介绍与工厂模式(扁平管理模式VS职业经理人模式)

    本文主要对设计模式进行大概解说.特别是对工厂模式进行简明的解析: 一.设计模式的分类 创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. 结构型模式,共七种:适配器模式. ...

  7. c#设计模式介绍

    序号 模式名称 模式描述 应用场景 例子 1 单例模式 (SigletonPattern) 保证一个类仅有一个实例,并提供一个访问它的全局访问点. • 单例类只能有一个实例. • 单例类必须自己创建自 ...

  8. Python:设计模式介绍--单例模式

    单例模式 1.单例是只有一个实例2.通过静态字段+静态字段伪造出一个单例效果3.什么时候用:当所有实例中封装的数据相同时,创建单例模式(eg:连接池) 用单例模式创建连接池: class CP: __ ...

  9. Repository设计模式

    definition: 通过用来访问领域对象的一个类似集合的接口,在领域与数据映射层之间进行协调. advantage: 使用该模式的最大好处就是将领域模型从客户代码和数据映射层之间解耦出来. 理解内 ...

随机推荐

  1. 如何应用Font Awesome矢量字体图标

    Font Awesome 是一套专门为 Twitter Boostrap 设计的图标字体库.这套图标字体集几乎囊括了网页中可能用到的所有图标,除了包括 Twitter Boostrap 的默认图标外, ...

  2. Qt5.3编译错误——call of overloaded ‘max(int int)’is ambiguous

    错误描述: 今天在使用Qt写一个C++函数模板的测试程序的时候,编译的时候,编译的时候出现如下错误: 错误描述为:在main函数中,进行函数max()重载时,出现(ambiguous)含糊的,不明确的 ...

  3. Atitit attilax总结的对于attilax重要的jsr规范,以及需要增加的jsr规范

    Atitit attilax总结的对于attilax重要的jsr规范,以及需要增加的jsr规范 需要增加的jsr规范1 开发常用的10个规范(jsp etc)1 other开发常用的50个规范1 需要 ...

  4. Atitit 拦截数据库异常的处理最佳实践

    Atitit 拦截数据库异常的处理最佳实践 需要特殊处理的ex 在Dao层异常转换并抛出1 Server层转换为业务异常1 需要特殊处理的ex 在Dao层异常转换并抛出 } catch (SQLExc ...

  5. Zabbix安装部署

    zabbix服务器端配置:(10.27.98.29服务器端) 1.安装lamp环境 yum -y install autoconf curl-devel gcc gcc-c++ httpd httpd ...

  6. java 线程安全 Lock

    java.util.concurrent.locks 对于线程安全我们前面使用了synchronized关键字,对于线程的协作我们使用Object.wait()和Object.notify().在JD ...

  7. 每天一个linux命令(44):top命令

    top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法.top是一个动态显示过程,即可以通过用户按键来不断刷新 ...

  8. angularjs decorator

    angularjs decorator https://docs.angularjs.org/guide/decorators decorator() $provide服务提供了在服务实例创建时对其进 ...

  9. AngularJS中Directive指令系列 - scope属性的使用

    文章是转的,我做下补充.原文地址:https://segmentfault.com/a/1190000002773689 每当一个指令被创建的时候,都会有这样一个选择,是继承自己的父作用域(一般是外部 ...

  10. PNG使用技巧 PNG的使用技巧

    PNG的格式和透明度 这个Fireworks会比较清楚,打开Fireworkd优化面板,可以清楚看到png有3种不同深度的格式:png8.png24.png32. 其中,在优化面板选择png8,可发现 ...