SlideShare a Scribd company logo
Document-oriented data access for
domain-driven .NET systems with
PostgreSQL & Marten
Bojan Veljanovski, CTO at HASELT
October, 2017 @ Macedonian .NET User Group
1
What we are going to talk about?
● Limitations in relational-only DBs for storing true domain
aggregates
● How document-oriented DBs come into play
● Marten as document DB for .NET with PostgreSQL
● SQL + NoSQL => best of both worlds
2
Setting context
● How I got interested in document-oriented DBs?
Why it makes so much sense?
● Why PostgreSQL?
Why not MongoDb, CouchDb, RavenDb?
What about SQL Server, MySQL?
● Who else uses this approach?
3
Sample domain model
4
DDD Aggregate
Cluster of domain objects that can be treated as a
single unit.
● Consists of
○ Aggregate Root (or Root Entity)
○ Entities
○ Value Objects
○ Services
○ Repositories 5
Basic characteristics of an Aggregate Root
● Global identity
● Ensures invariants within aggregate
● Guarantees consistency
● Can reference other ARs only by identity
● Cannot reference non ARs
● Transactional consistency
● Stored and loaded in DB as a whole. 6
Relational-only model
7
Sample domain model in code
Let’s see the sample codebase in Visual Studio.
8
Limitations when working with
domain models in relational-only DBs...
● Cannot store an array of values
○ Needs new table with 1-m relation
● ...an array of objects
○ Needs new table with 1-m relation
● …a nested object
○ Needs new table with 1-1 relation
● …an AR in a single table
○ AR is spanned across many tables, and needs joinsThese limitations often lead to...
9
These limitations often lead to...
● Complex O/R mapping code
● Sacrificing encapsulation for easier O/R mapping
● Leak of domain logic to upper layers
● Anemic domain models - only data, no behavior
● Dev teams focusing on data-relations instead of
system behavior
● Implicit “SELECT N+1” problems
● Unit tests full with mocks & stubs 10
Moving out of the relational-only mindset
From thinking in terms of relations and data
To thinking in terms of behavior and data
11
Document model
12
How document-oriented DBs come into play
● Single document per [whole] aggregate
● Can store complex object graphs
● Can store arrays, collections, nested objects
● No need for ORM
● No limitations for creating rich domain models
● Dev team focus is in on behavior and logic
● Models are easy to unit test
13
Introducing PostgreSQL
14
Introducing PostgreSQL
● Object-relational database
● ACID
● Transactional
● Highly Extendable
● Open Source
● Free
15
PostgreSQL NoSQL data types
● Key/Value store
○ hstore
● Document store
○ json and jsonb
● Spatial and Geographic objects
○ postgis 16
PostgreSQL as document store:
Creating table with JSONB column
17
Inserting documents
18
Querying documents (1)
19
Querying documents (2)
20
Indexing documents
Various indexes are supported. 21
Marten as Document DB for
.NET with PostgreSQL
22
Introducing Marten
● OSS .NET library that provides polyglot persistence
● Document store
● Event store
● Uses PostgreSQL’s powerful JSON support
● Initial pre-alpha release on 08.12.2015
● Current version 2.3.2
● Community chat: gitter.im/JasperFx/marten
23
Getting started
24
Persisting documents
25
How our data looks like? -> User
26
Loading document by Id
27
Querying documents with LINQ
28
Filtering in arrays
29
Paging and sorting
30
Querying with plain SQL through Dapper
(yes, querying NoSQL documents with plain-old SQL)
31
Adding Foreign Key to Order document
32
How our data looks like with FK? -> Order
33
Querying multiple documents
by using SQL JOIN
34
Other notable features in Marten as Document Db
● Foreign Keys
● Indexes
● Duplicate columns
● Batched queries
● Compiled queries
● Bulk insert
● Multitenancy
● Optimistic Concurrency 35
Closing
36
Summary
● Relational-only DBs limit the ability to craft good
domain models
● Document DBs are natural fit for DDD Aggregates
● SQL + NoSQL in Postgres = best of both worlds
in a single DB provider
● Marten helps .NET devs leverage PostgreSQL as
polyglot database
● Polyglot data access mindset - there is no single
approach
37
Resources
● Marten
○ https://jasperfx.github.io/marten
○ https://gitter.im/JasperFx/marten
● PostgreSQL JSON Functions and Operators
○ https://www.postgresql.org
● This presentation’s code samples
○ https://github.com/bojanv91/MartenPostgreSamples
● Other useful articles
○ https://martinfowler.com/bliki/AggregateOrientedDatabase.html
38

More Related Content

PDF
Indexing, searching, and aggregation with redi search and .net
PPTX
.NET Fest 2017. Константин Проскурдин. Marten как хранилище документов для .N...
PDF
Brett Ragozzine - Graph Databases and Neo4j
PPTX
Data driven pages in a word press site - Srikanth Meenakshi
PDF
LDCache - a cache for linked data-driven web applications
PDF
Log stage zero-cost structured logging
PDF
DBpedia Viewer - LDOW 2014
PDF
Big Data Day LA 2015 - How to model anything in Redis by Josiah Carlson of Ze...
Indexing, searching, and aggregation with redi search and .net
.NET Fest 2017. Константин Проскурдин. Marten как хранилище документов для .N...
Brett Ragozzine - Graph Databases and Neo4j
Data driven pages in a word press site - Srikanth Meenakshi
LDCache - a cache for linked data-driven web applications
Log stage zero-cost structured logging
DBpedia Viewer - LDOW 2014
Big Data Day LA 2015 - How to model anything in Redis by Josiah Carlson of Ze...

What's hot (19)

PDF
The ECM world from the point of view of Alfresco - Linux Day 2013 - Rome
PPTX
MongoDB NoSQL - Developer Guide
PPTX
Mongo db nosql (1)
PPTX
Mongo db
PDF
Crawling the Web for Structured Documents
KEY
Legislation.gov.uk
PDF
Storage dei dati con MongoDB
PDF
ODP
Redis - Your Magical superfast database
PPTX
CSCi226PPT1
PPTX
Structures
PDF
Xml databases
PPTX
DataTables view CKAN monthly live
PPTX
Neo4j graph database
PPTX
Intoduction to OrientDB
PDF
Mobicents Summit 2012 - Alexandre Mendonca - Mobicents jDiameter
The ECM world from the point of view of Alfresco - Linux Day 2013 - Rome
MongoDB NoSQL - Developer Guide
Mongo db nosql (1)
Mongo db
Crawling the Web for Structured Documents
Legislation.gov.uk
Storage dei dati con MongoDB
Redis - Your Magical superfast database
CSCi226PPT1
Structures
Xml databases
DataTables view CKAN monthly live
Neo4j graph database
Intoduction to OrientDB
Mobicents Summit 2012 - Alexandre Mendonca - Mobicents jDiameter
Ad

Similar to NoSQL document oriented data access for .net systems with postgresql and marten (20)

PDF
NoSQL Now: Postgres - The NoSQL Cake You Can Eat
PDF
EDB NoSQL German Webinar 2015
 
PPTX
Being RDBMS Free -- Alternate Approaches to Data Persistence
PDF
NoSQL on ACID - Meet Unstructured Postgres
 
PDF
Oslo baksia2014
PPTX
Why Organizations are Looking at Alternative Database Technologies – Introduc...
PDF
Postgres: The NoSQL Cake You Can Eat
 
PDF
Pg no sql_beatemjoinem_v10
PDF
No sql bigdata and postgresql
PPT
Postgres for the Future
 
PPTX
Freeing Yourself from an RDBMS Architecture
PDF
Is multi-model the future of NoSQL?
PDF
FOSSASIA 2015 - 10 Features your developers are missing when stuck with Propr...
PDF
NoSQL and Spatial Database Capabilities using PostgreSQL
 
PPTX
Choosing your NoSQL storage
PPTX
Twisting Postgresql into a Document Db and Event Store.pptx
PDF
Some NoSQL
PDF
Relational vs. Non-Relational
PDF
PostgreSQL, your NoSQL database
PDF
Postgres NoSQL - Delivering Apps Faster
 
NoSQL Now: Postgres - The NoSQL Cake You Can Eat
EDB NoSQL German Webinar 2015
 
Being RDBMS Free -- Alternate Approaches to Data Persistence
NoSQL on ACID - Meet Unstructured Postgres
 
Oslo baksia2014
Why Organizations are Looking at Alternative Database Technologies – Introduc...
Postgres: The NoSQL Cake You Can Eat
 
Pg no sql_beatemjoinem_v10
No sql bigdata and postgresql
Postgres for the Future
 
Freeing Yourself from an RDBMS Architecture
Is multi-model the future of NoSQL?
FOSSASIA 2015 - 10 Features your developers are missing when stuck with Propr...
NoSQL and Spatial Database Capabilities using PostgreSQL
 
Choosing your NoSQL storage
Twisting Postgresql into a Document Db and Event Store.pptx
Some NoSQL
Relational vs. Non-Relational
PostgreSQL, your NoSQL database
Postgres NoSQL - Delivering Apps Faster
 
Ad

More from Bojan Veljanovski (7)

PDF
CodeCamp 2018 - Architecting Systems with CQRS - Four Years Experience Report...
PPTX
DIGIT Noe 2016 - Overview of front end development today
PPTX
Bojan Veljanovski - Modular Software Architecture and Design (Code Camp 2016)
PPTX
Vertical Slices in .NET - Bojan Veljanovski, March 2016, MK.NET UG
PPTX
Quick look at Web Components and Project Polymer - MKNET UG
PPTX
Real-world software design practices when developing ASP.NET web systems by B...
PPTX
AngularJS + NancyFx + MongoDB = The best trio for ultimate SPA by Bojan Velja...
CodeCamp 2018 - Architecting Systems with CQRS - Four Years Experience Report...
DIGIT Noe 2016 - Overview of front end development today
Bojan Veljanovski - Modular Software Architecture and Design (Code Camp 2016)
Vertical Slices in .NET - Bojan Veljanovski, March 2016, MK.NET UG
Quick look at Web Components and Project Polymer - MKNET UG
Real-world software design practices when developing ASP.NET web systems by B...
AngularJS + NancyFx + MongoDB = The best trio for ultimate SPA by Bojan Velja...

Recently uploaded (20)

PPTX
Internet of Things (IOT) - A guide to understanding
PPT
Mechanical Engineering MATERIALS Selection
PDF
Well-logging-methods_new................
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
composite construction of structures.pdf
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Welding lecture in detail for understanding
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Geodesy 1.pptx...............................................
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
PPT on Performance Review to get promotions
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Internet of Things (IOT) - A guide to understanding
Mechanical Engineering MATERIALS Selection
Well-logging-methods_new................
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
composite construction of structures.pdf
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Welding lecture in detail for understanding
Lecture Notes Electrical Wiring System Components
bas. eng. economics group 4 presentation 1.pptx
Geodesy 1.pptx...............................................
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Operating System & Kernel Study Guide-1 - converted.pdf
Model Code of Practice - Construction Work - 21102022 .pdf
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPT on Performance Review to get promotions
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT

NoSQL document oriented data access for .net systems with postgresql and marten

  • 1. Document-oriented data access for domain-driven .NET systems with PostgreSQL & Marten Bojan Veljanovski, CTO at HASELT October, 2017 @ Macedonian .NET User Group 1
  • 2. What we are going to talk about? ● Limitations in relational-only DBs for storing true domain aggregates ● How document-oriented DBs come into play ● Marten as document DB for .NET with PostgreSQL ● SQL + NoSQL => best of both worlds 2
  • 3. Setting context ● How I got interested in document-oriented DBs? Why it makes so much sense? ● Why PostgreSQL? Why not MongoDb, CouchDb, RavenDb? What about SQL Server, MySQL? ● Who else uses this approach? 3
  • 5. DDD Aggregate Cluster of domain objects that can be treated as a single unit. ● Consists of ○ Aggregate Root (or Root Entity) ○ Entities ○ Value Objects ○ Services ○ Repositories 5
  • 6. Basic characteristics of an Aggregate Root ● Global identity ● Ensures invariants within aggregate ● Guarantees consistency ● Can reference other ARs only by identity ● Cannot reference non ARs ● Transactional consistency ● Stored and loaded in DB as a whole. 6
  • 8. Sample domain model in code Let’s see the sample codebase in Visual Studio. 8
  • 9. Limitations when working with domain models in relational-only DBs... ● Cannot store an array of values ○ Needs new table with 1-m relation ● ...an array of objects ○ Needs new table with 1-m relation ● …a nested object ○ Needs new table with 1-1 relation ● …an AR in a single table ○ AR is spanned across many tables, and needs joinsThese limitations often lead to... 9
  • 10. These limitations often lead to... ● Complex O/R mapping code ● Sacrificing encapsulation for easier O/R mapping ● Leak of domain logic to upper layers ● Anemic domain models - only data, no behavior ● Dev teams focusing on data-relations instead of system behavior ● Implicit “SELECT N+1” problems ● Unit tests full with mocks & stubs 10
  • 11. Moving out of the relational-only mindset From thinking in terms of relations and data To thinking in terms of behavior and data 11
  • 13. How document-oriented DBs come into play ● Single document per [whole] aggregate ● Can store complex object graphs ● Can store arrays, collections, nested objects ● No need for ORM ● No limitations for creating rich domain models ● Dev team focus is in on behavior and logic ● Models are easy to unit test 13
  • 15. Introducing PostgreSQL ● Object-relational database ● ACID ● Transactional ● Highly Extendable ● Open Source ● Free 15
  • 16. PostgreSQL NoSQL data types ● Key/Value store ○ hstore ● Document store ○ json and jsonb ● Spatial and Geographic objects ○ postgis 16
  • 17. PostgreSQL as document store: Creating table with JSONB column 17
  • 22. Marten as Document DB for .NET with PostgreSQL 22
  • 23. Introducing Marten ● OSS .NET library that provides polyglot persistence ● Document store ● Event store ● Uses PostgreSQL’s powerful JSON support ● Initial pre-alpha release on 08.12.2015 ● Current version 2.3.2 ● Community chat: gitter.im/JasperFx/marten 23
  • 26. How our data looks like? -> User 26
  • 31. Querying with plain SQL through Dapper (yes, querying NoSQL documents with plain-old SQL) 31
  • 32. Adding Foreign Key to Order document 32
  • 33. How our data looks like with FK? -> Order 33
  • 34. Querying multiple documents by using SQL JOIN 34
  • 35. Other notable features in Marten as Document Db ● Foreign Keys ● Indexes ● Duplicate columns ● Batched queries ● Compiled queries ● Bulk insert ● Multitenancy ● Optimistic Concurrency 35
  • 37. Summary ● Relational-only DBs limit the ability to craft good domain models ● Document DBs are natural fit for DDD Aggregates ● SQL + NoSQL in Postgres = best of both worlds in a single DB provider ● Marten helps .NET devs leverage PostgreSQL as polyglot database ● Polyglot data access mindset - there is no single approach 37
  • 38. Resources ● Marten ○ https://jasperfx.github.io/marten ○ https://gitter.im/JasperFx/marten ● PostgreSQL JSON Functions and Operators ○ https://www.postgresql.org ● This presentation’s code samples ○ https://github.com/bojanv91/MartenPostgreSamples ● Other useful articles ○ https://martinfowler.com/bliki/AggregateOrientedDatabase.html 38