zoukankan      html  css  js  c++  java
  • Repository 设计模式介绍

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

    运行后可以通过。。。

     

    以上转载:《果子的天空

     

  • 相关阅读:
    初识ABP vNext(8):ABP特征管理
    初识ABP vNext(7):vue身份认证管理&租户管理
    初识ABP vNext(6):vue+ABP实现国际化
    初识ABP vNext(5):ABP扩展实体
    初识ABP vNext(4):vue用户登录&菜单权限
    【Flutter 混合开发】嵌入原生View-iOS
    【Flutter 混合开发】嵌入原生View-Android
    使用Flutter完成10个商业项目后的经验教训
    谷歌发布Flutter Alpha:支持Windows
    【Flutter 实战】大量复杂数据持久化
  • 原文地址:https://www.cnblogs.com/ezplusy/p/3443264.html
Copyright © 2011-2022 走看看