SlideShare a Scribd company logo
Onion Architecture
And The Blog
Making a blog (and having fun while doing it)
Who am I
Product Developer - Systems Architect - Freelancer - Development Manager
Past experience
Senior Developer for Cleverbug
Lecturer at the Digital Skills Academy
Director of Tercet, software development consultancy
Current position
Software Development Manager for OliveMedia
Barry O Sullivan
This talk
We’re going to …
● Talk about the blog and it’s features
● look at the architecture
● look at the services we used
● look at some Laravel 5.1 code
● Show you how to publish a blog post
The Blog
What do we want it to do?
What’s is its purpose?
Our goals were to
● Host content from the community
● Make it easy for the community to contribute content
● Keep it simple and clean
● Use existing tools and libraries as much as possible
● Showcase some design best practice (as we see it anyway)
● Have some fun making it
The features
So to we decided on some simple features
● Articles are displayed as a list in the blog section
● Articles can be clicked and viewed individually
● Articles have unique urls (for SEO)
● Articles are written in markdown
● Articles are part of the repo
● People can send us articles via pull request
We had a lot more features, but these were dropped, as they increased the
scope and didn’t actually add a lot of value.
The architecture
We decided to use an Onion architecture
(Also known as Ports and Adapters or *Hexagonal Architecture)
Why we choose it:
● Clean separation of concerns
● Easy to maintain
● Encourages CQRS
● Industry best practice
● It’s just really nice
* A very misleading name, programmers are the worst at naming things
The Domain
The heart of the application.
Contains anything that’s business rule related
Keep your business logic completely separated
from your storage/service/controller logic.
Doesn’t care about the technical details, contains
only what’s necessary to validate business rules.
Made up off
● ValueObjects
● Entities
● RepositoryInterfaces
The API
Entry point for the domain.
Uses the domain to perform business operations.
Acts as the interface between the “Application”
and the business logic.
Ensures that implementation details don’t bleed
out into your controllers,
eg. database logic, presentation logic
Made up off
● Commands
● Queries
The Infrastructure
Contains the technical details about specific
technologies. Eg, how data is accessed and
where data is stored
(The framework is technically infrastructure)
Three types of infrastructure
● Controllers
● Data Storage (Database/FileSystem)
● External Services (Package, API, etc. . .)
How layers interact
A key part of an onion architecture is that outer
layers can use inner layers, but inner layers
have no knowledge of outer layers.
This means that the infrastructure can see
ValueObjects, but ValueObjects have no
knowledge of the Database.
Forces you to keep things simple and to put logic
where it belongs, no bleeding of details that will
make future changes difficult.
Building the Blog
The implementation is split into 3 sections
● The domain
● The API
● The Infrastructure
○ Services
○ Storage
○ Controllers
The Blog Domain
A blog post has the following business rules
● A blog must have a title, an author, an ID and content
● Title, author and content cannot be blank
● Publish date cannot be in the future
That leaves us with the following value objects that enforce business rules
Post, ID, Title, Author, PublishDate, Content
We don’t have any other business rules for now, so we left it at that
The Blog Domain (2)
The next step is the definition of the repo, for storing and retrieving posts.
The repo is an interface, and it defines how we expect our repository to behave,
regardless of what implementation we decide to go for.
We do this because it forces us to keep it generic, clean and easy to test. It
also gives us the option of switching to another repo type (Which we will)
The interface has three methods
store(Post $post)
fetch(UUID $id)
all()
The Blog API
Next we have the API, this is the entry point for our app, and it offers all the functionality we want to
expose. It’s CQRS, which just means we have two types of operations
We offer the following command and queries
Commands
CreatePost
Queries
Post
PostList
We’re using Laravel 5.1s commands and command bus for this, as it has the IOC built in. This means
we only ever use interfaces at this level and we let laravel handle the injection of our concrete
implementations of our repository.
Infrastructure - Services
Why reinvent the wheel when someone is giving you a jet pack for free?
We used the following packages to make our job easier
Laravel 5.1 - It’s so hot right now (and it’s really powerful)
FlySystem -Generic API for multiple storage engines
CommonMark -Convert markdown into HTML, extensible
UUID - UUIDs, because they’re actually really handy
Carbon -Improved version of PHPs DateTime classes
Infrastructure - Storage
We create a concrete class that implements the PostRepo interface.
Since they’re part of the repo, they’re part of the file system.
They’re stored by ID, and are represented by two files
blog/
e09135f2-ade1-4c25-9527-7ecbdc0d7c15/
details.json (title, author, date)
markdown.md (Actual content)
When we create a post, this structure is created
When we fetch a post, it reads from the file system and parses this data,
turning it into a post object.
Infrastructure - Controllers
Controllers are our access point to the application from an external sources
In our case, HTTP controllers and Artisan commands
HTTP to access content (It’s a website)
Artisan to create posts (We are developers after all)
In the HTTP controllers, we load CommonMark and convert the content into
HTML.
This is an important, we made the decision that rendering a post and it’s format
had nothing to do with the domain.
The Code
Let’s have a look at some code
Just a quick look so you can see
what we’re talking about
Creating a post
We made this really easy.
Open up a terminal and go to the root folder
Run the following
php artisan post:make “Post title” barryosull
This will output the ID of the post, so you can go the files and edit them. Visit
the blog on your local machine and you’ll see the post.
*barryosull is my github handle, use your own
Quick additions
At the beginning we decided to use GitHub usernames in the author field.
We then saw that best practice was to have the author’s avatar beside the post.
How hard would it be to add?
Well, not hard, with a little bit of JS in the front-end, we
connected to the public Github API and fetched their avatar.
15mins work, a better experience for visitor and our contributors
Next Steps
What will we do next?
● Cache the posts in Redis (faster)
● Unit tests
● Tags for blog posts
● View posts by tag
● View posts by author
● Turn the blog into a laravel package
● Spruce up the design
● Events for broadcasting domain changes
● Projections for handling views of data
● Write more content!
We need your content
Guess what? We need you!
Want to become an active member in the community?
Want to be part of an active github project?
Want to build up your profile?
Want to mess around with Laravel 5.1?
Want to write about your experiences?
Then write a post and send it on!
Thanks for listening
Now for questions.
Ask me any question you like!
. . . except for my pin number
. . . or my password
. . . ok, please just stick to questions about the talk
Barry O Sullivan - barry@tercet.io - http://barryosull.com

More Related Content

PPTX
Why Kotlin?
PDF
Spring Boot Workshop - January w/ Women Who Code
PDF
Zen and the Art of REST API documentation - MuCon London 2015
PDF
Your First Scala Web Application using Play 2.1
PDF
The Ember.js Framework - Everything You Need To Know
PDF
Building Awesome APIs with Lumen
PDF
WordPress 4.1
ODP
Creating Web Services with Zend Framework - Matthew Turland
Why Kotlin?
Spring Boot Workshop - January w/ Women Who Code
Zen and the Art of REST API documentation - MuCon London 2015
Your First Scala Web Application using Play 2.1
The Ember.js Framework - Everything You Need To Know
Building Awesome APIs with Lumen
WordPress 4.1
Creating Web Services with Zend Framework - Matthew Turland

What's hot (20)

PDF
Building a chatbot – step by step
PDF
React Native Firebase Realtime Database + Authentication
PPTX
Laravel Webcon 2015
PPTX
Codeigniter
PDF
[React Native Tutorial] Lecture 6: Component, Props, and Network
PPT
Os Bubna
PPTX
Java 8 concurrency abstractions
PPTX
Entity Framework 4 In Microsoft Visual Studio 2010 - ericnel
PDF
PHP Indonesia Meetup - What's New in Yii2 and PHP5.5
PPTX
Patterns in JavaScript
PDF
React Native Firebase
PDF
Python's dynamic nature (rough slides, November 2004)
PPTX
Modular PHP Development using CodeIgniter Bonfire
PPTX
DEVNET-1001 Coding 101: How to Call REST APIs from a REST Client and Python
PPTX
How to build a rest api
KEY
Cucumber, Cuke4Duke, and Groovy
PDF
SiLCC Overview
PPTX
Drupal & Summon: Keeping Article Discovery in the Library
PPTX
Introduction of lambda expression and predicate builder
Building a chatbot – step by step
React Native Firebase Realtime Database + Authentication
Laravel Webcon 2015
Codeigniter
[React Native Tutorial] Lecture 6: Component, Props, and Network
Os Bubna
Java 8 concurrency abstractions
Entity Framework 4 In Microsoft Visual Studio 2010 - ericnel
PHP Indonesia Meetup - What's New in Yii2 and PHP5.5
Patterns in JavaScript
React Native Firebase
Python's dynamic nature (rough slides, November 2004)
Modular PHP Development using CodeIgniter Bonfire
DEVNET-1001 Coding 101: How to Call REST APIs from a REST Client and Python
How to build a rest api
Cucumber, Cuke4Duke, and Groovy
SiLCC Overview
Drupal & Summon: Keeping Article Discovery in the Library
Introduction of lambda expression and predicate builder
Ad

Similar to Building a blog with an Onion Architecture (20)

PDF
Functional testing with behat
PDF
Advanced web application architecture - Talk
PDF
PDF
MuleSoft Manchester Meetup #3 slides 31st March 2020
PDF
Evolutionary Design Solid
PDF
Android meetup
DOCX
Company Visitor Management System Report.docx
PPTX
PDF
React fristy day learn basic NM_Day01.pdf
PDF
Microsoft SharePoint in the Workplace
PPTX
Guidelines to understand durable functions with .net core, c# and stateful se...
PDF
Code igniter - A brief introduction
PDF
Broadcom CA SDM overview - Introduction to BOP
PDF
PDF
Getting started contributing to Apache Spark
PPTX
Professionalizing the Front-end
PDF
Code for Startup MVP (Ruby on Rails) Session 1
PDF
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
PPTX
Dev Ops for systems of record - Talk at Agile Australia 2015
PDF
Has serverless adoption hit a roadblock?
Functional testing with behat
Advanced web application architecture - Talk
MuleSoft Manchester Meetup #3 slides 31st March 2020
Evolutionary Design Solid
Android meetup
Company Visitor Management System Report.docx
React fristy day learn basic NM_Day01.pdf
Microsoft SharePoint in the Workplace
Guidelines to understand durable functions with .net core, c# and stateful se...
Code igniter - A brief introduction
Broadcom CA SDM overview - Introduction to BOP
Getting started contributing to Apache Spark
Professionalizing the Front-end
Code for Startup MVP (Ruby on Rails) Session 1
Strategies and Tips for Building Enterprise Drupal Applications - PNWDS 2013
Dev Ops for systems of record - Talk at Agile Australia 2015
Has serverless adoption hit a roadblock?
Ad

More from Barry O Sullivan (6)

PDF
Value objects
PDF
Managing expectations
PDF
DDD: lessons learned
PPTX
Php test fest
PDF
Cleaning up your codebase with a clean architecture
PPTX
Design patterns - The Good, the Bad, and the Anti-Pattern
Value objects
Managing expectations
DDD: lessons learned
Php test fest
Cleaning up your codebase with a clean architecture
Design patterns - The Good, the Bad, and the Anti-Pattern

Recently uploaded (20)

PDF
System and Network Administraation Chapter 3
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Introduction to Artificial Intelligence
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PPTX
Transform Your Business with a Software ERP System
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
ai tools demonstartion for schools and inter college
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
System and Network Administration Chapter 2
System and Network Administraation Chapter 3
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
CHAPTER 2 - PM Management and IT Context
Introduction to Artificial Intelligence
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Transform Your Business with a Software ERP System
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Navsoft: AI-Powered Business Solutions & Custom Software Development
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
ai tools demonstartion for schools and inter college
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Upgrade and Innovation Strategies for SAP ERP Customers
How Creative Agencies Leverage Project Management Software.pdf
Reimagine Home Health with the Power of Agentic AI​
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Adobe Illustrator 28.6 Crack My Vision of Vector Design
VVF-Customer-Presentation2025-Ver1.9.pptx
System and Network Administration Chapter 2

Building a blog with an Onion Architecture

  • 1. Onion Architecture And The Blog Making a blog (and having fun while doing it)
  • 2. Who am I Product Developer - Systems Architect - Freelancer - Development Manager Past experience Senior Developer for Cleverbug Lecturer at the Digital Skills Academy Director of Tercet, software development consultancy Current position Software Development Manager for OliveMedia Barry O Sullivan
  • 3. This talk We’re going to … ● Talk about the blog and it’s features ● look at the architecture ● look at the services we used ● look at some Laravel 5.1 code ● Show you how to publish a blog post
  • 4. The Blog What do we want it to do? What’s is its purpose? Our goals were to ● Host content from the community ● Make it easy for the community to contribute content ● Keep it simple and clean ● Use existing tools and libraries as much as possible ● Showcase some design best practice (as we see it anyway) ● Have some fun making it
  • 5. The features So to we decided on some simple features ● Articles are displayed as a list in the blog section ● Articles can be clicked and viewed individually ● Articles have unique urls (for SEO) ● Articles are written in markdown ● Articles are part of the repo ● People can send us articles via pull request We had a lot more features, but these were dropped, as they increased the scope and didn’t actually add a lot of value.
  • 6. The architecture We decided to use an Onion architecture (Also known as Ports and Adapters or *Hexagonal Architecture) Why we choose it: ● Clean separation of concerns ● Easy to maintain ● Encourages CQRS ● Industry best practice ● It’s just really nice * A very misleading name, programmers are the worst at naming things
  • 7. The Domain The heart of the application. Contains anything that’s business rule related Keep your business logic completely separated from your storage/service/controller logic. Doesn’t care about the technical details, contains only what’s necessary to validate business rules. Made up off ● ValueObjects ● Entities ● RepositoryInterfaces
  • 8. The API Entry point for the domain. Uses the domain to perform business operations. Acts as the interface between the “Application” and the business logic. Ensures that implementation details don’t bleed out into your controllers, eg. database logic, presentation logic Made up off ● Commands ● Queries
  • 9. The Infrastructure Contains the technical details about specific technologies. Eg, how data is accessed and where data is stored (The framework is technically infrastructure) Three types of infrastructure ● Controllers ● Data Storage (Database/FileSystem) ● External Services (Package, API, etc. . .)
  • 10. How layers interact A key part of an onion architecture is that outer layers can use inner layers, but inner layers have no knowledge of outer layers. This means that the infrastructure can see ValueObjects, but ValueObjects have no knowledge of the Database. Forces you to keep things simple and to put logic where it belongs, no bleeding of details that will make future changes difficult.
  • 11. Building the Blog The implementation is split into 3 sections ● The domain ● The API ● The Infrastructure ○ Services ○ Storage ○ Controllers
  • 12. The Blog Domain A blog post has the following business rules ● A blog must have a title, an author, an ID and content ● Title, author and content cannot be blank ● Publish date cannot be in the future That leaves us with the following value objects that enforce business rules Post, ID, Title, Author, PublishDate, Content We don’t have any other business rules for now, so we left it at that
  • 13. The Blog Domain (2) The next step is the definition of the repo, for storing and retrieving posts. The repo is an interface, and it defines how we expect our repository to behave, regardless of what implementation we decide to go for. We do this because it forces us to keep it generic, clean and easy to test. It also gives us the option of switching to another repo type (Which we will) The interface has three methods store(Post $post) fetch(UUID $id) all()
  • 14. The Blog API Next we have the API, this is the entry point for our app, and it offers all the functionality we want to expose. It’s CQRS, which just means we have two types of operations We offer the following command and queries Commands CreatePost Queries Post PostList We’re using Laravel 5.1s commands and command bus for this, as it has the IOC built in. This means we only ever use interfaces at this level and we let laravel handle the injection of our concrete implementations of our repository.
  • 15. Infrastructure - Services Why reinvent the wheel when someone is giving you a jet pack for free? We used the following packages to make our job easier Laravel 5.1 - It’s so hot right now (and it’s really powerful) FlySystem -Generic API for multiple storage engines CommonMark -Convert markdown into HTML, extensible UUID - UUIDs, because they’re actually really handy Carbon -Improved version of PHPs DateTime classes
  • 16. Infrastructure - Storage We create a concrete class that implements the PostRepo interface. Since they’re part of the repo, they’re part of the file system. They’re stored by ID, and are represented by two files blog/ e09135f2-ade1-4c25-9527-7ecbdc0d7c15/ details.json (title, author, date) markdown.md (Actual content) When we create a post, this structure is created When we fetch a post, it reads from the file system and parses this data, turning it into a post object.
  • 17. Infrastructure - Controllers Controllers are our access point to the application from an external sources In our case, HTTP controllers and Artisan commands HTTP to access content (It’s a website) Artisan to create posts (We are developers after all) In the HTTP controllers, we load CommonMark and convert the content into HTML. This is an important, we made the decision that rendering a post and it’s format had nothing to do with the domain.
  • 18. The Code Let’s have a look at some code Just a quick look so you can see what we’re talking about
  • 19. Creating a post We made this really easy. Open up a terminal and go to the root folder Run the following php artisan post:make “Post title” barryosull This will output the ID of the post, so you can go the files and edit them. Visit the blog on your local machine and you’ll see the post. *barryosull is my github handle, use your own
  • 20. Quick additions At the beginning we decided to use GitHub usernames in the author field. We then saw that best practice was to have the author’s avatar beside the post. How hard would it be to add? Well, not hard, with a little bit of JS in the front-end, we connected to the public Github API and fetched their avatar. 15mins work, a better experience for visitor and our contributors
  • 21. Next Steps What will we do next? ● Cache the posts in Redis (faster) ● Unit tests ● Tags for blog posts ● View posts by tag ● View posts by author ● Turn the blog into a laravel package ● Spruce up the design ● Events for broadcasting domain changes ● Projections for handling views of data ● Write more content!
  • 22. We need your content Guess what? We need you! Want to become an active member in the community? Want to be part of an active github project? Want to build up your profile? Want to mess around with Laravel 5.1? Want to write about your experiences? Then write a post and send it on!
  • 23. Thanks for listening Now for questions. Ask me any question you like! . . . except for my pin number . . . or my password . . . ok, please just stick to questions about the talk Barry O Sullivan - barry@tercet.io - http://barryosull.com