2015-08-13 23:54:40 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								layout: pattern
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								title: Repository
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								folder: repository
							 
						 
					
						
							
								
									
										
										
										
											2015-08-15 18:03:05 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								permalink: /patterns/repository/
							 
						 
					
						
							
								
									
										
										
										
											2019-12-13 21:09:28 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								categories: Architectural
							 
						 
					
						
							
								
									
										
										
										
											2015-12-28 15:52:44 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								tags:
							 
						 
					
						
							
								
									
										
										
										
											2019-12-13 21:09:28 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								 -  Data access
							 
						 
					
						
							
								
									
										
										
										
											2015-08-13 23:54:40 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-03 21:14:30 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								## Intent
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								Repository layer is added between the domain and data mapping
							 
						 
					
						
							
								
									
										
										
										
											2015-08-13 23:54:40 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								layers to isolate domain objects from details of the database access code and
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								to minimize scattering and duplication of query code. The Repository pattern is
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								especially useful in systems where number of domain classes is large or heavy
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								querying is utilized.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-12-07 20:01:13 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								## Class diagram
 
							 
						 
					
						
							
								
									
										
										
										
											2015-08-13 23:54:40 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-03 21:14:30 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								## Applicability
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								Use the Repository pattern when
							 
						 
					
						
							
								
									
										
										
										
											2015-08-13 23:54:40 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-12-13 21:09:28 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								*  The number of domain objects is large
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								*  You want to avoid duplication of query code
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								*  You want to keep the database querying code in single place
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								*  You have multiple data sources
							 
						 
					
						
							
								
									
										
										
										
											2015-08-13 23:54:40 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-03 21:14:30 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								## Real world examples
 
							 
						 
					
						
							
								
									
										
										
										
											2015-08-13 23:54:40 +02:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-08-15 18:03:05 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								*  [Spring Data ](http://projects.spring.io/spring-data/ )
							 
						 
					
						
							
								
									
										
										
										
											2015-11-27 11:22:33 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-03 21:14:30 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								## Credits
 
							 
						 
					
						
							
								
									
										
										
										
											2015-11-27 11:22:33 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
										 
									
								 
							
							
								*  [Don’  t use DAO, use Repository ](http://thinkinginobjects.com/2012/08/26/dont-use-dao-use-repository/ )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
								
									
								 
							
							
								*  [Advanced Spring Data JPA - Specifications and Querydsl ](https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/ )
							 
						 
					
						
							
								
									
										
										
										
											2019-12-07 20:01:13 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
								
									
								 
							
							
								*  [Repository Pattern Benefits and Spring Implementation ](https://stackoverflow.com/questions/40068965/repository-pattern-benefits-and-spring-implementation )