SlideShare uma empresa Scribd logo
1 de 18
Baixar para ler offline
Code Contracts and Generics:
                   implementing a LINQ-enabled
                           Repository
        Andrea Saltarello
        C.T.O.
        http://blogs.ugidotnet.org/pape
        http://twitter.com/andysal74
        Managed Designs s.r.l.


http://creativecommons.org/licenses/by-nc-nd/2.5/
Sponsor
Sponsor Platinum




 Sponsor Gold
Agenda
•   Chi
•   Quisquilie IT
•   Perché
•   Come
•   Q&A
Repository pattern [P of EAA, 322]
Mediates between the domain and data mapping layers
using a collection-like interface for accessing domain
objects.
Code Contracts: OverView
I “Software Contract” sono specifiche formali del
   comportamento di una unità di codice espresse mediante
   l‟uso di 3 elementi:
• Espressioni invarianti, che sono vere sia prima sia dopo
   l‟invocazione della funzione
• Precondizioni, che esprimono le condizioni nelle quali una
   funzione può essere invocata
• Postcondizioni, che esprimono il funzionamento delle
   funzioni

Nota: è possibile verificare questi elementi mediante l‟uso di
  asserzioni, che implementeremo mediante unit testing
VS2010 vs. Code Contracts

Il FX 4.0 include il supporto ai Code
   Contracts ma occorre «abilitarlo»
   installando:
• il Code Contract toolkit per VS2010 (d/l da
  sito Microsoft Research)
• [Opzionale] le estensioni Code Contract
  per VS2010
Code Contracts
Repository pattern
A system with a complex domain model often benefits
from a layer, such as the one provided by Data Mapper
(165), that isolates domain objects from details of the
database access code. In such systems it can be worthwhile
to build another layer of abstraction over the mapping
layer where query construction code is concentrated. This
becomes more important when there are a large number
of domain classes or heavy querying. In these cases
particularly, adding this layer helps minimize duplicate
query logic.
…
You can also find a good write-up of this pattern in
Domain Driven Design.
Repository: as easy as 1-2-3
1. Il Repository occorre per persistere
   un Domain Model
2. La «Bibbia» del Domain Model è
   [DDD]
3. E vediamolo, „sto DDD 
DDD Architecture
DDD Key Concepts (redux)
• Il Domain Layer contiene la domain logic ed
  è composto da
   – Model: Entità (identità e stato) e Valori
     (solo stato)
   – Servizi
• Entità e Valori a runtime formano dei grafi
  di oggetti. I grafi dotati di “dignità propria”
  sono chiamati Aggregate e il sistema (es: i
  Repository) si “impegna” a gestirli
  correttamente ed atomicamente
• Le istanze di entità/aggregate sono costruite
  da Factory (pattern Builder [GoF])
Da 0 ad Aggregate
• E' un insieme di elementi raggruppati in un‟unità
  logica, quindi un grafo di oggetti
• Ha come radice l'entità principale dell'aggregato
• La radice è l‟unico elemento che può essere
  referenziato fuori dai confini dell‟aggregato
• Non è possibile agire direttamente sugli elementi
  senza passare dalla radice dell'aggregato
• L‟aggregate ha la responsabilità di implementare
  la propria logica
Domain Model
Repository pattern (Reloaded)
Mediates between the domain and data mapping layers
using a collection-like interface for accessing domain
objects.

Ricapitolando:
• Interfaccia “collection like”
• Gestisce la persistenza degli Aggregate
• LINQ! (siamo dei buongustai )
IRepository<T>
Demo code: NSK

Progetto open source (licenza CPL)
disponibile su CodePlex:
http://nsk.codeplex.com
Bibliografia

[DDD] Domain Driven Design, Eric
  Evans, Addison-Wesley

[P of EAA] Pattern of Enterprise
  Application Architecture, Martin
  Fowler, Addison-Wesley
Code Contracts and Generics: implementing a LINQ-enabled Repository

Mais conteúdo relacionado

Semelhante a Code Contracts and Generics: implementing a LINQ-enabled Repository

Cert04 70-484 - essentials of developing windows store apps
Cert04   70-484 - essentials of developing windows store appsCert04   70-484 - essentials of developing windows store apps
Cert04 70-484 - essentials of developing windows store apps
DotNetCampus
 
Writing apps for android with .net
Writing apps for android with .net Writing apps for android with .net
Writing apps for android with .net
Leonardo Alario
 
Idiomatic Domain Driven Design
Idiomatic Domain Driven DesignIdiomatic Domain Driven Design
Idiomatic Domain Driven Design
Andrea Saltarello
 
Introduzione a TypeScript
Introduzione a TypeScriptIntroduzione a TypeScript
Introduzione a TypeScript
Sinergia Totale
 
DotNetToscana - Sessione TypeScript
DotNetToscana - Sessione TypeScriptDotNetToscana - Sessione TypeScript
DotNetToscana - Sessione TypeScript
Sinergia Totale
 
Community Tour 2009 Windows Azure Overview
Community Tour 2009 Windows Azure OverviewCommunity Tour 2009 Windows Azure Overview
Community Tour 2009 Windows Azure Overview
Fabio Cozzolino
 
Slide typescript - xe dotnet - Codemotion Rome 2015
Slide typescript - xe dotnet - Codemotion Rome 2015Slide typescript - xe dotnet - Codemotion Rome 2015
Slide typescript - xe dotnet - Codemotion Rome 2015
Codemotion
 

Semelhante a Code Contracts and Generics: implementing a LINQ-enabled Repository (20)

Cert04 70-484 - essentials of developing windows store apps
Cert04   70-484 - essentials of developing windows store appsCert04   70-484 - essentials of developing windows store apps
Cert04 70-484 - essentials of developing windows store apps
 
Writing apps for android with .net
Writing apps for android with .net Writing apps for android with .net
Writing apps for android with .net
 
Kotlin hexagonal-architecture
Kotlin hexagonal-architectureKotlin hexagonal-architecture
Kotlin hexagonal-architecture
 
XeDotNet - Source Generator
XeDotNet - Source GeneratorXeDotNet - Source Generator
XeDotNet - Source Generator
 
Idiomatic Domain Driven Design
Idiomatic Domain Driven DesignIdiomatic Domain Driven Design
Idiomatic Domain Driven Design
 
DDive - 8.5.2 Xpages - L'evoluzione continua
DDive - 8.5.2 Xpages - L'evoluzione continuaDDive - 8.5.2 Xpages - L'evoluzione continua
DDive - 8.5.2 Xpages - L'evoluzione continua
 
Design Patterns - Enterprise Patterns (part 2)
Design Patterns - Enterprise Patterns (part 2)Design Patterns - Enterprise Patterns (part 2)
Design Patterns - Enterprise Patterns (part 2)
 
Introduzione al Domain Driven Design (DDD)
Introduzione al Domain Driven Design (DDD)Introduzione al Domain Driven Design (DDD)
Introduzione al Domain Driven Design (DDD)
 
Layered Expression Trees feat. CQRS
Layered Expression Trees feat. CQRSLayered Expression Trees feat. CQRS
Layered Expression Trees feat. CQRS
 
Introduzione a TypeScript
Introduzione a TypeScriptIntroduzione a TypeScript
Introduzione a TypeScript
 
Oracle 1
Oracle 1Oracle 1
Oracle 1
 
DotNetToscana - Sessione TypeScript
DotNetToscana - Sessione TypeScriptDotNetToscana - Sessione TypeScript
DotNetToscana - Sessione TypeScript
 
Visual Studio Performance Tools
Visual Studio Performance ToolsVisual Studio Performance Tools
Visual Studio Performance Tools
 
Microsoft Azure per l'IT Pro
Microsoft Azure per l'IT ProMicrosoft Azure per l'IT Pro
Microsoft Azure per l'IT Pro
 
Community Tour 2009 Windows Azure Overview
Community Tour 2009 Windows Azure OverviewCommunity Tour 2009 Windows Azure Overview
Community Tour 2009 Windows Azure Overview
 
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
 
Domain Driven Design e CQRS
Domain Driven Design e CQRSDomain Driven Design e CQRS
Domain Driven Design e CQRS
 
ASP.NET MVC: Andare oltre il 100% (Web@work)
ASP.NET MVC: Andare oltre il 100% (Web@work)ASP.NET MVC: Andare oltre il 100% (Web@work)
ASP.NET MVC: Andare oltre il 100% (Web@work)
 
Usare Knockout JS
Usare Knockout JSUsare Knockout JS
Usare Knockout JS
 
Slide typescript - xe dotnet - Codemotion Rome 2015
Slide typescript - xe dotnet - Codemotion Rome 2015Slide typescript - xe dotnet - Codemotion Rome 2015
Slide typescript - xe dotnet - Codemotion Rome 2015
 

Mais de Andrea Saltarello

From relational data to object spaces
From relational data to object spacesFrom relational data to object spaces
From relational data to object spaces
Andrea Saltarello
 

Mais de Andrea Saltarello (9)

Da Rotor a .NET Core ed indietro: Microsoft &lt;3 Open Source
Da Rotor a .NET Core ed indietro: Microsoft &lt;3 Open SourceDa Rotor a .NET Core ed indietro: Microsoft &lt;3 Open Source
Da Rotor a .NET Core ed indietro: Microsoft &lt;3 Open Source
 
The Fine Art of Time Travelling: implementing Event Sourcing
The Fine Art of Time Travelling: implementing Event SourcingThe Fine Art of Time Travelling: implementing Event Sourcing
The Fine Art of Time Travelling: implementing Event Sourcing
 
ASP.NET Core essentials
ASP.NET Core essentialsASP.NET Core essentials
ASP.NET Core essentials
 
Implementing Event Sourcing in .NET
Implementing Event Sourcing in .NETImplementing Event Sourcing in .NET
Implementing Event Sourcing in .NET
 
Idiomatic Domain Driven Design: implementing CQRS
Idiomatic Domain Driven Design: implementing CQRSIdiomatic Domain Driven Design: implementing CQRS
Idiomatic Domain Driven Design: implementing CQRS
 
Architecting an ASP.NET MVC Solution
Architecting an ASP.NET MVC SolutionArchitecting an ASP.NET MVC Solution
Architecting an ASP.NET MVC Solution
 
Layered Expression Trees: una terza via (idiomatica) verso il DDD
Layered Expression Trees: una terza via (idiomatica) verso il DDDLayered Expression Trees: una terza via (idiomatica) verso il DDD
Layered Expression Trees: una terza via (idiomatica) verso il DDD
 
From relational data to object spaces
From relational data to object spacesFrom relational data to object spaces
From relational data to object spaces
 
MVC2: non solo tecnologia
MVC2: non solo tecnologiaMVC2: non solo tecnologia
MVC2: non solo tecnologia
 

Code Contracts and Generics: implementing a LINQ-enabled Repository

  • 1. Code Contracts and Generics: implementing a LINQ-enabled Repository Andrea Saltarello C.T.O. http://blogs.ugidotnet.org/pape http://twitter.com/andysal74 Managed Designs s.r.l. http://creativecommons.org/licenses/by-nc-nd/2.5/
  • 3. Agenda • Chi • Quisquilie IT • Perché • Come • Q&A
  • 4. Repository pattern [P of EAA, 322] Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.
  • 5. Code Contracts: OverView I “Software Contract” sono specifiche formali del comportamento di una unità di codice espresse mediante l‟uso di 3 elementi: • Espressioni invarianti, che sono vere sia prima sia dopo l‟invocazione della funzione • Precondizioni, che esprimono le condizioni nelle quali una funzione può essere invocata • Postcondizioni, che esprimono il funzionamento delle funzioni Nota: è possibile verificare questi elementi mediante l‟uso di asserzioni, che implementeremo mediante unit testing
  • 6. VS2010 vs. Code Contracts Il FX 4.0 include il supporto ai Code Contracts ma occorre «abilitarlo» installando: • il Code Contract toolkit per VS2010 (d/l da sito Microsoft Research) • [Opzionale] le estensioni Code Contract per VS2010
  • 8. Repository pattern A system with a complex domain model often benefits from a layer, such as the one provided by Data Mapper (165), that isolates domain objects from details of the database access code. In such systems it can be worthwhile to build another layer of abstraction over the mapping layer where query construction code is concentrated. This becomes more important when there are a large number of domain classes or heavy querying. In these cases particularly, adding this layer helps minimize duplicate query logic. … You can also find a good write-up of this pattern in Domain Driven Design.
  • 9. Repository: as easy as 1-2-3 1. Il Repository occorre per persistere un Domain Model 2. La «Bibbia» del Domain Model è [DDD] 3. E vediamolo, „sto DDD 
  • 11. DDD Key Concepts (redux) • Il Domain Layer contiene la domain logic ed è composto da – Model: Entità (identità e stato) e Valori (solo stato) – Servizi • Entità e Valori a runtime formano dei grafi di oggetti. I grafi dotati di “dignità propria” sono chiamati Aggregate e il sistema (es: i Repository) si “impegna” a gestirli correttamente ed atomicamente • Le istanze di entità/aggregate sono costruite da Factory (pattern Builder [GoF])
  • 12. Da 0 ad Aggregate • E' un insieme di elementi raggruppati in un‟unità logica, quindi un grafo di oggetti • Ha come radice l'entità principale dell'aggregato • La radice è l‟unico elemento che può essere referenziato fuori dai confini dell‟aggregato • Non è possibile agire direttamente sugli elementi senza passare dalla radice dell'aggregato • L‟aggregate ha la responsabilità di implementare la propria logica
  • 14. Repository pattern (Reloaded) Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects. Ricapitolando: • Interfaccia “collection like” • Gestisce la persistenza degli Aggregate • LINQ! (siamo dei buongustai )
  • 16. Demo code: NSK Progetto open source (licenza CPL) disponibile su CodePlex: http://nsk.codeplex.com
  • 17. Bibliografia [DDD] Domain Driven Design, Eric Evans, Addison-Wesley [P of EAA] Pattern of Enterprise Application Architecture, Martin Fowler, Addison-Wesley