Repository 设计模式介绍

上传人:飞*** 文档编号:44918688 上传时间:2018-06-14 格式:DOCX 页数:4 大小:383.23KB
返回 下载 相关 举报
Repository 设计模式介绍_第1页
第1页 / 共4页
Repository 设计模式介绍_第2页
第2页 / 共4页
Repository 设计模式介绍_第3页
第3页 / 共4页
Repository 设计模式介绍_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《Repository 设计模式介绍》由会员分享,可在线阅读,更多相关《Repository 设计模式介绍(4页珍藏版)》请在金锄头文库上搜索。

1、Repository 设计模式介绍设计模式介绍 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 remov

2、ed 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

3、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。对

4、象能够从 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 on

5、e member of the Aggregate, designated as the root. A set of consistency rules applies within the Aggregates boundaries.“. 通常我们是对于 domain 的每个集合会对应的定义一个 repository。也就说,并不是每个实体都会有对应的一个 repository。Repository 的接口一般情况下是作为 domain model 的一部分,但是严格意义上讲它不属于 domain model。当我们处理 aggregates 时,大部分时间我们需要 3 个常用的相关操作。

6、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,然后再实现接口的几个方法。下面来进行单元测试:运行后可以通过。

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号