Enterprise Application Architecture Model-resource library (Repository)

Source: Internet
Author: User

Repository mode definition:

Coordinates the domain and data ing layers and uses interfaces similar to a set to access domain objects.

The biggest advantage of using this mode is to decouple the domain model from the customer code and data ing layer.

The following is a demo of using this mode in Castle ActiveRecord.

Entity-Layer Code

   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:  }
 

------------------ Split line --------------------

1. We use the IRepository interface for business entity persistence operations.

   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. Create an IPostRepository API for private operations on business entities.

   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. Implementation of Object Repository class

   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:  }

 

 

 

 

 

Summary of the actual purpose of the Respository mode in the example (Source)

  1. The Repository mode is the architecture mode, which has reference value only when designing the architecture;
  2. The Repository mode encapsulates Data Query and storage logic;
  3. Actual use of the Repository mode: change or upgrade the ORM engine without affecting the business logic;
  4. The Repository mode can improve the test efficiency. In unit testing, the Mock object can be used to replace the actual database access, which improves the test case running speed exponentially.

Difference between Repository and Dal (Source)

Repository is a concept in DDD, emphasizing that Repository is driven by Domain. The features defined in Repository must reflect Domain intent and constraints, while Dal is more purely a function for data access, not strictly limited by the Business layer.

The use of Repository implies a kind of intention tendency, that is, what the Domain needs to provide, what features should not be provided, and everything is centered on the needs of the Domain; while the use of Dal, the intention is that the data database access operations that can be used by the Dal layer are provided to the Business layer. Which Business should you choose. You can use this Dal for another Business. Everything is centered on what operations My Dal can provide.

Related Articles:

EntityFramework-driven design practices (7)

Repository Mode

Repository Mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.