公司專屬應用程式架構模式——資產庫(Repository)

來源:互聯網
上載者:User

Repository模式定義:

協調領域和資料對應層,利用類似於集合的介面來訪問領域對象。

使用此模式最大的好處是將領域模型從客戶代碼和資料對應層之間解耦出來。

接下來給出一個在Castle ActiveRecord中使用該模式的demo

實體層代碼

   1:  [ActiveRecord("Posts")]
   2:  public class Post:ActiveRecordBase<Post>
   3:  {
   4:      [PrimaryKey("PostId")]
   5:      public int Id { get; set; }
   6:   
   7:      [Property]
   8:      public string Subject { get; set; }
   9:   
  10:      [Property]
  11:      public string Text { get; set; }
  12:   
  13:      [Property]
  14:      public DateTime DateAdded { get; set; }
  15:   
  16:      [BelongsTo("CategoryId")]
  17:      public Category Category { get; set; }
  18:   
  19:      [HasMany(Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Inverse = true)]
  20:      public IList<Comment> Comments { get; set; }
  21:   
  22:  }
 

——————————————————分割線————————————————————

1、我們對業務實體持久化操作,提取為IRepository介面

   1:  public interface IRepository<T> where T:class
   2:  {
   3:      T Find(int id);
   4:      void Add(T entity);
   5:      void Save(T entity);
   6:      void Remove(T entity);
   7:  }

2、對業務實體的私人操作,單獨建立一個IPostRepository介面

   1:  public interface IPostRepository:IRepository<Post>
   2:  {
   3:      IEnumerable<Post> FindAll();
   4:      IEnumerable<Post> FindAll(int index, int count);
   5:   
   6:      //IEnumerable<Post> FindAll();
   7:      //IEnumerable<Post> FindAll(int index, int count);
   8:      //IEnumerable<Post> FindBy(Query query);
   9:      //IEnumerable<Post> FindBy(Query query, int index, int count);
  10:  }

3、實體的Repository類的實現

   1:  public class PostRepository : IPostRepository
   2:  {
   3:      public IEnumerable<Post> FindAll()
   4:      {
   5:          return Post.FindAll();
   6:      }
   7:   
   8:      public IEnumerable<Post> FindAll(int index, int count)
   9:      {
  10:          return Post.FindAll().Skip(index * count).Take(count);
  11:      }
  12:   
  13:      public Post Find(int id)
  14:      {
  15:          return Post.Find(id);
  16:      }
  17:   
  18:      public void Add(Post entity)
  19:      {
  20:          entity.Create();
  21:      }
  22:   
  23:      public void Save(Post entity)
  24:      {
  25:          entity.Save();
  26:      }
  27:   
  28:      public void Remove(Post entity)
  29:      {
  30:          entity.Delete();
  31:      }
  32:   
  33:  }

 

 

 

 

 

Respository模式在樣本中的實際目的小結一下(來源

  1. Repository模式是架構模式,在設計架構時,才有參考價值;
  2. Repository模式主要是封裝資料查詢和儲存邏輯;
  3. Repository模式實際用途:更換、升級ORM 引擎,不影響商務邏輯;
  4. Repository模式能提高測試效率,單元測試時,用Mock對象代替實際的資料庫存取,可以成倍地提高測試案例運行速度。

Repository與Dal的區別(來源

Repository是DDD中的概念,強調Repository是受Domain驅動的,Repository中定義的功能要體現Domain的意圖和約束,而Dal更純粹的就是提供資料訪問的功能,並不嚴格受限於Business層。

使用Repository,隱含著一種意圖傾向,就是 Domain需要什麼我才提供什麼,不該提供的功能就不要提供,一切都是以Domain的需求為核心;而使用Dal,其意圖傾向在於我Dal層能使用的數 據庫訪問操作提供給Business層,你Business要用哪個自己選。換一個Business也可以用我這個Dal,一切是以我Dal能提供什麼操 作為核心。

相關文章:

EntityFramework之領域驅動設計實踐(七)

關於Repository模式

Repository模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.