Repository Design Pattern Introduction

Source: Internet
Author: User

In DDD design, repository pattern is used to obtain the data required by the domain model.

1. What's 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 added to and removed from the Repository, as they can from a simple collection of Objects, and the mapping Code encapsulated by the Repository would 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 the M, 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."

According to the original author's introduction, it is a link between the data mapping layer and the domain, which acts as a collection of domain objects in memory. The client object combines some entities of the query and submits them to repository. objects can be removed or added from repository, as if these objects were to be manipulated on a collection object, while the code of the mapping layer would fetch corresponding data from the database.

Conceptually, Repository is a collection of data from a data store that is encapsulated into objects and provides operations on those collections ....

In domain-driven design, we have a collection (aggregate) concept, which is:

"a cluster of associated objects that is treated as A unit for the purpose of data changes. External references is restricted to one member of the Aggregate, designated as the root. A set of consistency rules applies within the Aggregate ' s boundaries.

Usually we are defining a repository for each set of domain. It is said that not every entity will have a corresponding repository.

The repository interface is generally used as part of the domain model, but it does not belong to the domain model in strict sense.

When we deal with aggregates, we need 3 commonly used related operations most of the time.

1. Get the corresponding collection by ID ·.

2. Add a collection to Repository.

3. Remove a collection from the repository.

For example, we have an order form,

Then its repository interface IRepository (which contains three basic methods) is as follows:

Suppose we have another domain model that is product, and its classes are as follows:

Its repository interface is iproductrepository as follows:

You can see that our two interfaces actually have a lot of code is duplicated, so you can come to a basic interface irepository, and combined with System.Collection.Generic in the generic attribute to design IRepository code:

The corresponding iproductrepository and iorderrepository codes also have to be modified:

To be able to test our repository, we create a repository inherited Iproductrepository interface here:

In the constructor we first add the product and then implement several methods of the interface. Here's the unit test:

After running can pass ...

Use repository mode in NHibernate.

First we need to create a session provider, as follows:

The mapping file for Hibernate.cfg.xml and product is now required:

You can then use the unit test.

But we seldom use this pattern in actual projects. Let's analyze the following:

When we have a private method is getsession, each access to it will return a session entity, so each operation in repository will use its own session entity, when our project is not the use of distributed transactions, We all want these operations to be in one transaction.

In order to solve this problem, we can use UOW mode.

Repository Design Pattern Introduction

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.