SlideShare a Scribd company logo
Repository Pattern
Un buon design al servizio della testabilità
Mi presento
Christian Nastasi
Software Architect & Technical Coach
Sviluppo Software dal 1999
Esperienza di quasi 2 anni a Londra
Email: christian.nastasi@gmail.com
Linkedin: /in/cnastasi/
Disclaimer
● Non possiedo la verità assoluta, questo è solo uno dei
tanti approcci possibili
● Fra 1 anno probabilmente scriverò codice in maniera
completamente differente
● Questo talk era stato ideato sotto forma di workshop,
per questioni di tempistiche è stato ridotto e certe
tematiche potrebbero essere incomplete.
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.
Martin Fowler
Repository Pattern: Definizione
Un Repository fa da intermediario tra il dominio
(business logic) ed il layer di persistenza, agendo come
una collezione di oggetti di dominio, salvati in
memoria.
Gli oggetti “cliente” costruiscono le query
dichiarativamente e le inviano al Repository per essere
eseguite.
Gli oggetti possono essere aggiunti e rimossi nello
stesso modo in cui viene fatto per una collezione di
oggetti. Il codice relativo alla persistenza è incapsulato
all’interno del repository, che si occuperà di eseguire le
operazioni appropriate “dietro le quinte”
Martino Cacciatore di uccelli
Repository Pattern: Definizione
Un Repository è un’astrazione che ci permette di separare i dati
da come vengono utilizzati a come vengono memorizzati.
L’astrazione è quella della collezione di oggetti con interfaccia
CRUD.
Repository Pattern: Definizione
Le query vanno scritte in modo dichiarativo: significa che
dobbiamo indicare soltanto cosa vogliamo ottenere.
Il come è compito del Repository, che incapsula al suo interno le
logiche di persistenza.
Repository Pattern: Esempio di interfaccia
interface EntityRepository
{
public function get (int $id):?Entity;
public function getBy (Criteria $criteria):Iterator;
public function getAll():Iterator;
public function save (Entity $entity);
public function delete (Entity $entity);
public function exists (Entity $entity):bool;
}
Caso di studio: Blog MVP
- As a user I want to publish a post
- As a user I want to see all my posts
- As a user I want to publish a comment on a post
- As a user I want to see all the comments of a post
Step 0: Codice Monolitico
- Classe monolitica
- Logica e persistenza mischiate nello
stesso posto
- Non si possono usare i mock
- Necessità di un database di test
Step 1: Dividiamo le responsabilità
+ Codice suddiviso logicamente
- Logica e persistenza mischiate nello
stesso posto
- Non si possono usare i mock
- Necessità di un database di test
Step 2: Separiamo la logica dalla persistenza
+ Codice suddiviso logicamente
+ Logica e persistenza separate
- Non si possono usare i mock
- Necessità di un database di test
Step 3: Incapsuliamo i dati in modelli
+ Codice suddiviso logicamente
+ Logica e persistenza separate
+ Si possono usare i mock
- Necessità di un database di test
Entità
Un’entità è un modello che rappresenta un oggetto di dominio e che può
essere identificato univocamente attraverso un id.
Istanze differenti dello stesso oggetto ma con un id identico rappresentano la
stessa entità e sono pertanto uguali.
Step 4: Refactoring dei Manager
+ Codice suddiviso logicamente
+ Logica e persistenza separate
+ Si possono usare i mock
- Necessità di un database di test
Step 5: Refactoring dei Repository
+ Codice suddiviso logicamente
+ Logica e persistenza separate
+ Si possono usare i mock
+ Necessità di un database di test
Step 5: Refactoring dei Repository
Step 5: Refactoring dei Repository
Vantaggi
+ Si può mutare il layer di persistenza senza incidere sul funzionamento
della logica
+ Utilizzando un Repository In-memory i tempi per eseguire i test di unità
si accorciano e non c’è bisogno di ripulire il DB ogni volta.
+ Incoraggia un design DRY (don’t repeat yourself)
+ Il layer di persistenza potrebbe essere di qualsiasi natura, anche remoto
(Utile in una logica a microservices)
Grazie!
Commenti o domande? Contattatemi
christian.nastasi@gmail.com

More Related Content

What's hot

OSI reference model
OSI reference modelOSI reference model
OSI reference model
Satyam Jaiswal
 
Data link layer
Data link layerData link layer
Data link layer
Abdul MaTéèñ
 
Transport layer services
Transport layer servicesTransport layer services
Transport layer services
Melvin Cabatuan
 
Naming in Distributed System
Naming in Distributed SystemNaming in Distributed System
Naming in Distributed System
MNM Jain Engineering College
 
Leftist heap
Leftist heapLeftist heap
Leftist heap
Shuvro Roy
 
Osi reference model and the tcp
Osi reference model and the tcpOsi reference model and the tcp
Osi reference model and the tcp
Agrippa Mungazi
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
CEC Landran
 
Ipv4 vs Ipv6 comparison
Ipv4 vs Ipv6 comparisonIpv4 vs Ipv6 comparison
Ipv4 vs Ipv6 comparison
Shailesh Pachori
 
TCP and UDP
TCP and UDP TCP and UDP
TCP and UDP
Ramesh Giri
 
Heap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithmsHeap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithms
samairaakram
 
Ip addressing
Ip addressingIp addressing
Ip addressing
Online
 
17. Recovery System in DBMS
17. Recovery System in DBMS17. Recovery System in DBMS
17. Recovery System in DBMS
koolkampus
 
Internet Protocol version 6
Internet Protocol version 6Internet Protocol version 6
Internet Protocol version 6
Rekha Yadav
 
ER DIAGRAM TO RELATIONAL SCHEMA MAPPING
ER DIAGRAM TO RELATIONAL SCHEMA MAPPING ER DIAGRAM TO RELATIONAL SCHEMA MAPPING
ER DIAGRAM TO RELATIONAL SCHEMA MAPPING
ARADHYAYANA
 
protocol architecture
 protocol architecture protocol architecture
protocol architecture
Srinivasa Rao
 
COMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed TranslationCOMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed Translation
Jyothishmathi Institute of Technology and Science Karimnagar
 
Ipv4 and Ipv6
Ipv4 and Ipv6Ipv4 and Ipv6
Ipv4 and Ipv6
rahul kundu
 
Remote invocation
Remote invocationRemote invocation
Remote invocation
ishapadhy
 
Liit tyit sem 5 enterprise java unit 4 notes mosst important questions with s...
Liit tyit sem 5 enterprise java unit 4 notes mosst important questions with s...Liit tyit sem 5 enterprise java unit 4 notes mosst important questions with s...
Liit tyit sem 5 enterprise java unit 4 notes mosst important questions with s...
tanujaparihar
 
binary tree
binary treebinary tree
binary tree
Shankar Bishnoi
 

What's hot (20)

OSI reference model
OSI reference modelOSI reference model
OSI reference model
 
Data link layer
Data link layerData link layer
Data link layer
 
Transport layer services
Transport layer servicesTransport layer services
Transport layer services
 
Naming in Distributed System
Naming in Distributed SystemNaming in Distributed System
Naming in Distributed System
 
Leftist heap
Leftist heapLeftist heap
Leftist heap
 
Osi reference model and the tcp
Osi reference model and the tcpOsi reference model and the tcp
Osi reference model and the tcp
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Ipv4 vs Ipv6 comparison
Ipv4 vs Ipv6 comparisonIpv4 vs Ipv6 comparison
Ipv4 vs Ipv6 comparison
 
TCP and UDP
TCP and UDP TCP and UDP
TCP and UDP
 
Heap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithmsHeap Sort in Design and Analysis of algorithms
Heap Sort in Design and Analysis of algorithms
 
Ip addressing
Ip addressingIp addressing
Ip addressing
 
17. Recovery System in DBMS
17. Recovery System in DBMS17. Recovery System in DBMS
17. Recovery System in DBMS
 
Internet Protocol version 6
Internet Protocol version 6Internet Protocol version 6
Internet Protocol version 6
 
ER DIAGRAM TO RELATIONAL SCHEMA MAPPING
ER DIAGRAM TO RELATIONAL SCHEMA MAPPING ER DIAGRAM TO RELATIONAL SCHEMA MAPPING
ER DIAGRAM TO RELATIONAL SCHEMA MAPPING
 
protocol architecture
 protocol architecture protocol architecture
protocol architecture
 
COMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed TranslationCOMPILER DESIGN- Syntax Directed Translation
COMPILER DESIGN- Syntax Directed Translation
 
Ipv4 and Ipv6
Ipv4 and Ipv6Ipv4 and Ipv6
Ipv4 and Ipv6
 
Remote invocation
Remote invocationRemote invocation
Remote invocation
 
Liit tyit sem 5 enterprise java unit 4 notes mosst important questions with s...
Liit tyit sem 5 enterprise java unit 4 notes mosst important questions with s...Liit tyit sem 5 enterprise java unit 4 notes mosst important questions with s...
Liit tyit sem 5 enterprise java unit 4 notes mosst important questions with s...
 
binary tree
binary treebinary tree
binary tree
 

Similar to Repository pattern

Repository pattern slides v1.1
Repository pattern slides v1.1Repository pattern slides v1.1
Repository pattern slides v1.1
Christian Nastasi
 
Code Contracts and Generics: implementing a LINQ-enabled Repository
Code Contracts and Generics: implementing a LINQ-enabled RepositoryCode Contracts and Generics: implementing a LINQ-enabled Repository
Code Contracts and Generics: implementing a LINQ-enabled Repository
Andrea Saltarello
 
How I did it (in .NET): idiomatic Domain Driven Design
How I did it (in .NET): idiomatic Domain Driven DesignHow I did it (in .NET): idiomatic Domain Driven Design
How I did it (in .NET): idiomatic Domain Driven DesignAndrea Saltarello
 
Sencha touch: Sviluppare un'app - 4° parte
Sencha touch: Sviluppare un'app - 4° parteSencha touch: Sviluppare un'app - 4° parte
Sencha touch: Sviluppare un'app - 4° parte
Giuseppe Toto
 
Entity Framework 4.0 vs NHibernate
Entity Framework 4.0 vs NHibernateEntity Framework 4.0 vs NHibernate
Entity Framework 4.0 vs NHibernate
Manuel Scapolan
 
C#, imparare a programmare e sopravvivere
C#, imparare a programmare e sopravvivereC#, imparare a programmare e sopravvivere
C#, imparare a programmare e sopravvivere
Matteo Valoriani
 
Alla scoperta dei Vector Database e dei RAG
Alla scoperta dei Vector Database e dei RAGAlla scoperta dei Vector Database e dei RAG
Alla scoperta dei Vector Database e dei RAG
Commit University
 
Layered Expression Trees feat. CQRS
Layered Expression Trees feat. CQRSLayered Expression Trees feat. CQRS
Layered Expression Trees feat. CQRS
Andrea Saltarello
 
Design Patterns - enterprise patterns (part I)
Design Patterns - enterprise patterns (part I)Design Patterns - enterprise patterns (part I)
Design Patterns - enterprise patterns (part I)
Fabio Armani
 
Design Patterns - Enterprise Patterns (part 2)
Design Patterns - Enterprise Patterns (part 2)Design Patterns - Enterprise Patterns (part 2)
Design Patterns - Enterprise Patterns (part 2)
Fabio Armani
 
Detailed Model Capture
Detailed Model CaptureDetailed Model Capture
Detailed Model Capturefcospito
 
Detailed Model Capture
Detailed Model CaptureDetailed Model Capture
Detailed Model Capturefcospito
 
Community Tour 2009 Windows Azure Overview
Community Tour 2009 Windows Azure OverviewCommunity Tour 2009 Windows Azure Overview
Community Tour 2009 Windows Azure OverviewFabio Cozzolino
 
Javascript - 4 | WebMaster & WebDesigner
Javascript - 4 | WebMaster & WebDesignerJavascript - 4 | WebMaster & WebDesigner
Javascript - 4 | WebMaster & WebDesigner
Matteo Magni
 
MongoDB Scala Roma SpringFramework Meeting2009
MongoDB Scala Roma SpringFramework Meeting2009MongoDB Scala Roma SpringFramework Meeting2009
MongoDB Scala Roma SpringFramework Meeting2009
Massimiliano Dessì
 
MongoDb and Scala SpringFramework Meeting
MongoDb and Scala SpringFramework MeetingMongoDb and Scala SpringFramework Meeting
MongoDb and Scala SpringFramework Meeting
guest67beeb9
 
Never Mind the Bollocks: here's the Domain Driven Design
Never Mind the Bollocks: here's the Domain Driven DesignNever Mind the Bollocks: here's the Domain Driven Design
Never Mind the Bollocks: here's the Domain Driven Design
Andrea Saltarello
 
Approccio Pratico al Domain Driven Design
Approccio Pratico al Domain Driven DesignApproccio Pratico al Domain Driven Design
Approccio Pratico al Domain Driven Design
Luca Milan
 
ios 8 - parte 1 - intro - ita
ios 8 - parte 1 - intro - itaios 8 - parte 1 - intro - ita
ios 8 - parte 1 - intro - itaDario Rusignuolo
 
Kotlin hexagonal-architecture
Kotlin hexagonal-architectureKotlin hexagonal-architecture
Kotlin hexagonal-architecture
Alessandro Minoccheri
 

Similar to Repository pattern (20)

Repository pattern slides v1.1
Repository pattern slides v1.1Repository pattern slides v1.1
Repository pattern slides v1.1
 
Code Contracts and Generics: implementing a LINQ-enabled Repository
Code Contracts and Generics: implementing a LINQ-enabled RepositoryCode Contracts and Generics: implementing a LINQ-enabled Repository
Code Contracts and Generics: implementing a LINQ-enabled Repository
 
How I did it (in .NET): idiomatic Domain Driven Design
How I did it (in .NET): idiomatic Domain Driven DesignHow I did it (in .NET): idiomatic Domain Driven Design
How I did it (in .NET): idiomatic Domain Driven Design
 
Sencha touch: Sviluppare un'app - 4° parte
Sencha touch: Sviluppare un'app - 4° parteSencha touch: Sviluppare un'app - 4° parte
Sencha touch: Sviluppare un'app - 4° parte
 
Entity Framework 4.0 vs NHibernate
Entity Framework 4.0 vs NHibernateEntity Framework 4.0 vs NHibernate
Entity Framework 4.0 vs NHibernate
 
C#, imparare a programmare e sopravvivere
C#, imparare a programmare e sopravvivereC#, imparare a programmare e sopravvivere
C#, imparare a programmare e sopravvivere
 
Alla scoperta dei Vector Database e dei RAG
Alla scoperta dei Vector Database e dei RAGAlla scoperta dei Vector Database e dei RAG
Alla scoperta dei Vector Database e dei RAG
 
Layered Expression Trees feat. CQRS
Layered Expression Trees feat. CQRSLayered Expression Trees feat. CQRS
Layered Expression Trees feat. CQRS
 
Design Patterns - enterprise patterns (part I)
Design Patterns - enterprise patterns (part I)Design Patterns - enterprise patterns (part I)
Design Patterns - enterprise patterns (part I)
 
Design Patterns - Enterprise Patterns (part 2)
Design Patterns - Enterprise Patterns (part 2)Design Patterns - Enterprise Patterns (part 2)
Design Patterns - Enterprise Patterns (part 2)
 
Detailed Model Capture
Detailed Model CaptureDetailed Model Capture
Detailed Model Capture
 
Detailed Model Capture
Detailed Model CaptureDetailed Model Capture
Detailed Model Capture
 
Community Tour 2009 Windows Azure Overview
Community Tour 2009 Windows Azure OverviewCommunity Tour 2009 Windows Azure Overview
Community Tour 2009 Windows Azure Overview
 
Javascript - 4 | WebMaster & WebDesigner
Javascript - 4 | WebMaster & WebDesignerJavascript - 4 | WebMaster & WebDesigner
Javascript - 4 | WebMaster & WebDesigner
 
MongoDB Scala Roma SpringFramework Meeting2009
MongoDB Scala Roma SpringFramework Meeting2009MongoDB Scala Roma SpringFramework Meeting2009
MongoDB Scala Roma SpringFramework Meeting2009
 
MongoDb and Scala SpringFramework Meeting
MongoDb and Scala SpringFramework MeetingMongoDb and Scala SpringFramework Meeting
MongoDb and Scala SpringFramework Meeting
 
Never Mind the Bollocks: here's the Domain Driven Design
Never Mind the Bollocks: here's the Domain Driven DesignNever Mind the Bollocks: here's the Domain Driven Design
Never Mind the Bollocks: here's the Domain Driven Design
 
Approccio Pratico al Domain Driven Design
Approccio Pratico al Domain Driven DesignApproccio Pratico al Domain Driven Design
Approccio Pratico al Domain Driven Design
 
ios 8 - parte 1 - intro - ita
ios 8 - parte 1 - intro - itaios 8 - parte 1 - intro - ita
ios 8 - parte 1 - intro - ita
 
Kotlin hexagonal-architecture
Kotlin hexagonal-architectureKotlin hexagonal-architecture
Kotlin hexagonal-architecture
 

Repository pattern

  • 1. Repository Pattern Un buon design al servizio della testabilità
  • 2. Mi presento Christian Nastasi Software Architect & Technical Coach Sviluppo Software dal 1999 Esperienza di quasi 2 anni a Londra Email: christian.nastasi@gmail.com Linkedin: /in/cnastasi/
  • 3. Disclaimer ● Non possiedo la verità assoluta, questo è solo uno dei tanti approcci possibili ● Fra 1 anno probabilmente scriverò codice in maniera completamente differente ● Questo talk era stato ideato sotto forma di workshop, per questioni di tempistiche è stato ridotto e certe tematiche potrebbero essere incomplete.
  • 4. 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. Martin Fowler Repository Pattern: Definizione Un Repository fa da intermediario tra il dominio (business logic) ed il layer di persistenza, agendo come una collezione di oggetti di dominio, salvati in memoria. Gli oggetti “cliente” costruiscono le query dichiarativamente e le inviano al Repository per essere eseguite. Gli oggetti possono essere aggiunti e rimossi nello stesso modo in cui viene fatto per una collezione di oggetti. Il codice relativo alla persistenza è incapsulato all’interno del repository, che si occuperà di eseguire le operazioni appropriate “dietro le quinte” Martino Cacciatore di uccelli
  • 5. Repository Pattern: Definizione Un Repository è un’astrazione che ci permette di separare i dati da come vengono utilizzati a come vengono memorizzati. L’astrazione è quella della collezione di oggetti con interfaccia CRUD.
  • 6. Repository Pattern: Definizione Le query vanno scritte in modo dichiarativo: significa che dobbiamo indicare soltanto cosa vogliamo ottenere. Il come è compito del Repository, che incapsula al suo interno le logiche di persistenza.
  • 7. Repository Pattern: Esempio di interfaccia interface EntityRepository { public function get (int $id):?Entity; public function getBy (Criteria $criteria):Iterator; public function getAll():Iterator; public function save (Entity $entity); public function delete (Entity $entity); public function exists (Entity $entity):bool; }
  • 8. Caso di studio: Blog MVP - As a user I want to publish a post - As a user I want to see all my posts - As a user I want to publish a comment on a post - As a user I want to see all the comments of a post
  • 9. Step 0: Codice Monolitico - Classe monolitica - Logica e persistenza mischiate nello stesso posto - Non si possono usare i mock - Necessità di un database di test
  • 10. Step 1: Dividiamo le responsabilità + Codice suddiviso logicamente - Logica e persistenza mischiate nello stesso posto - Non si possono usare i mock - Necessità di un database di test
  • 11. Step 2: Separiamo la logica dalla persistenza + Codice suddiviso logicamente + Logica e persistenza separate - Non si possono usare i mock - Necessità di un database di test
  • 12. Step 3: Incapsuliamo i dati in modelli + Codice suddiviso logicamente + Logica e persistenza separate + Si possono usare i mock - Necessità di un database di test
  • 13. Entità Un’entità è un modello che rappresenta un oggetto di dominio e che può essere identificato univocamente attraverso un id. Istanze differenti dello stesso oggetto ma con un id identico rappresentano la stessa entità e sono pertanto uguali.
  • 14. Step 4: Refactoring dei Manager + Codice suddiviso logicamente + Logica e persistenza separate + Si possono usare i mock - Necessità di un database di test
  • 15. Step 5: Refactoring dei Repository + Codice suddiviso logicamente + Logica e persistenza separate + Si possono usare i mock + Necessità di un database di test
  • 16. Step 5: Refactoring dei Repository
  • 17. Step 5: Refactoring dei Repository
  • 18. Vantaggi + Si può mutare il layer di persistenza senza incidere sul funzionamento della logica + Utilizzando un Repository In-memory i tempi per eseguire i test di unità si accorciano e non c’è bisogno di ripulire il DB ogni volta. + Incoraggia un design DRY (don’t repeat yourself) + Il layer di persistenza potrebbe essere di qualsiasi natura, anche remoto (Utile in una logica a microservices)
  • 19. Grazie! Commenti o domande? Contattatemi christian.nastasi@gmail.com