SlideShare a Scribd company logo
Keeping business logic out of
your UIs
Petter Holmström, Vaadin Architect
petter@vaadin.com
What is “business logic”?
● Every information system exists to solve a real-world problem (I hope...)
● A real-world problem consists of static parts (things, concepts) and dynamic
parts (events, processes)
● A system uses a model of the real world to solve its problems
○ The domain model
● The static parts of the domain model are often the easy ones
● The dynamic parts are easily forgotten even though they are just as important
● The business logic is the implementation of the dynamic parts of the domain
model
Disclaimer:
Business logic creeping into the
UI is not really an architectural
problem but a problem with
developer discipline
Let’s look at a “classic” architecture
Persistence layer (JPA)
Service layer (EJB)
UI layer (Vaadin)
Entities
SQL database
Problem 1
UI Operation
Service 1
Service 2
Service 3
Actual transaction
boundaries
Logical transaction
boundary
Problem 2
UI CRUD Service+
Business logic in the user’s head and in the UI
Service layer is only a frontend to the database
Problem 3
UI 1
UI 2
UI 3
God Service
Problem 4
Service A Service B
New method
?
What can we do?
Think about the transaction boundary
● Pay attention while coding and while reviewing others’ code
● The transaction boundary should IMO go between the UI and your backend
● Rules of thumb:
○ One UI operation, one backend call
○ One backend call, one transaction
○ One transaction, one thread
● Imagine your backend is running on a remote machine and you want to
minimize the traffic
Take care of your services
● Strive for highly cohesive services
● Avoid too general names like CustomerService
● Instead, opt for more specific names like CustomerOnboarding
● If you are adding a new service method and you find you could add it to more
than one service, you should probably create a completely new one or refactor
the existing ones
Separate reading and writing
● Place your queries in separate components
● Create UI specific query components instead of adding more and more query
methods to existing ones
● Allows for some neat optimizations and scaling
Avoid CRUD
● CRUD UIs and services basically move the business logic into the head of the
user
● Base your UIs and services on actual business processes and operations
● Exception: management UIs for reference and master data
Introducing a
Command based
architecture
● Think in terms of Commands and Queries
● A Command tells the system to perform a specific business operation
○ A command may return some result
○ A command either completes successfully or fails with an error
● A Query asks the system for specific data
○ A query always returns something
○ A query only fails because of external runtime errors such as network problems
● Commands and queries can be synchronous or asynchronous
● You can model each command and each query as a separate class
Forget about services!
The query/command class
● The name is always a verb in its imperative form
● The parameters are class attributes
● Use a generic parameter for the result, if any
● Try to make your class immutable
○ Not always possible, e.g. if you are binding a form directly to your command
● Validate the parameters inside your class
○ If you do this in the constructor, you cannot create a command or query with invalid parameters
Examples
<<command>>
CreatePatient<Patient>
firstName: string
lastName: string
gender: Gender
birthDate: LocalDate
<<query>>
FindOrders<List<Order>>
from: LocalDate
to: LocalDate
customer: Optional<Customer>
Examples (code)
public class CreatePatient implements Command<Patient> {
final String firstName;
final String lastName;
final Gender gender;
final LocalDate birthDate;
public CreatePatient(String firstName, String lastname,
Gender gender, LocalDate birthDate) {
this.firstName = Objects.requireNonNull(firstName);
…
}
}
Examples (code)
public class FindOrders implements Query<List<Order>> {
final LocalDate from;
final LocalDate to;
final Optional<Customer> customer;
public FindOrders(LocalDate from, LocalDate to) {
…
}
public FindOrders(LocalDate from, LocalDate to,
Customer customer) {
…
}
}
What about the implementation?
● Technically, queries and commands are implemented in the same way
● Each command/query has a handler
● The handler contains a single method that accepts the command/query as a
single parameter and returns the result of the command/query
● Handlers are transactional
● Similar queries/commands can share a common base class
● Handlers can invoke other handlers
Finding the correct handler
● A client will never invoke another handler directly
● Instead, commands/queries are passed to a broker or gateway
● The broker/gateway will look up the correct handler, invoke it and return the
result
Advantages
● An easy way of modeling the dynamics of the problem domain
● Forces you to think in terms of the domain while designing your UI
○ Since your UI should only be constructing and invoking commands/queries, the risk of business
logic ending up there should be smaller (I hope)
● Your business logic consists of small, highly cohesive classes
● No more god classes
● Easy to distribute, e.g. by using a service bus
○ Command and queries could be serialized into JSON
○ Command and query handlers can be running anywhere
● Easy to run asynchronously
● You can use separate data sources for reading and writing
○ CQRS
Master SlaveSlave
Query Handler Query Handler
Command
Handler
Broker
UI
Disadvantages
● More boilerplate code: for each operation, you have to write a command/query
class and a handler class
● The client does not invoke the handler directly so if the handler is missing, it
will not be detected until during runtime
● You have to write the command/query framework yourself
● You can still write too coarse or too fine grained commands
Time for a code
example!
What about long-running transactions?
● Many commands participate in the same conversation
● If your container supports it, you can make your handlers conversation scoped
○ This makes it possible to store state in the handlers
● If you want your handlers to remain stateless, create a conversation object that
is returned by and passed to every command handler
● The conversation object contains the state necessary to move the
conversation forward
Summary
● Discipline vs patterns
● Transaction boundary
● High cohesion
● Separate read and write
● Avoid CRUD
● Consider using commands and queries in business driven user interfaces
That’s all folks!

More Related Content

What's hot

Mocking in python
Mocking in pythonMocking in python
Mocking in python
Ooblioob
 
An Overview of automated testing (1)
An Overview of automated testing (1)An Overview of automated testing (1)
An Overview of automated testing (1)
Rodrigo Lopes
 
Overview of Testing Talks at Pycon
Overview of Testing Talks at PyconOverview of Testing Talks at Pycon
Overview of Testing Talks at Pycon
Jacqueline Kazil
 
@LinkingNote annotation in YATSPEC
@LinkingNote annotation in YATSPEC@LinkingNote annotation in YATSPEC
@LinkingNote annotation in YATSPEC
Wojciech Bulaty
 
Effective Unit Testing
Effective Unit TestingEffective Unit Testing
Effective Unit Testing
Eyal Kenig
 
Cuis smalltalk past present and future
Cuis smalltalk past present and futureCuis smalltalk past present and future
Cuis smalltalk past present and future
Hernan Wilkinson
 
Lessons Learned When Automating
Lessons Learned When AutomatingLessons Learned When Automating
Lessons Learned When Automating
Alan Richardson
 
Delhi second draft
Delhi second draft Delhi second draft
Delhi second draft
vaibhav lokhande
 
Code Quality with Magento 2
Code Quality with Magento 2Code Quality with Magento 2
Code Quality with Magento 2
Andreas von Studnitz
 
Delhi first draft_2
Delhi first draft_2Delhi first draft_2
Delhi first draft_2
vaibhav lokhande
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017
Xavi Hidalgo
 
xUnit test patterns 0
xUnit test patterns 0xUnit test patterns 0
xUnit test patterns 0
Stanislav Petrov
 
An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1
Blue Elephant Consulting
 
The working architecture of NodeJs applications
The working architecture of NodeJs applicationsThe working architecture of NodeJs applications
The working architecture of NodeJs applications
Viktor Turskyi
 
Codeception @ New Business Dept Adira Finance
Codeception @ New Business Dept Adira FinanceCodeception @ New Business Dept Adira Finance
Codeception @ New Business Dept Adira Finance
Fachrul Choliluddin
 
Computational thinking
Computational thinkingComputational thinking
Computational thinking
r123457
 
DevOps Anti-Patterns
DevOps Anti-PatternsDevOps Anti-Patterns
DevOps Anti-Patterns
Fernando Ike
 
Tdd in swift
Tdd in swiftTdd in swift
Tdd in swift
Javal Nanda
 
Test-Driven Development (TDD) in Swift
Test-Driven Development (TDD) in SwiftTest-Driven Development (TDD) in Swift
Test-Driven Development (TDD) in Swift
Amey Tavkar
 
Open source tools - Test Management Summit - 2009
Open source tools - Test Management Summit - 2009Open source tools - Test Management Summit - 2009
Open source tools - Test Management Summit - 2009
Alan Richardson
 

What's hot (20)

Mocking in python
Mocking in pythonMocking in python
Mocking in python
 
An Overview of automated testing (1)
An Overview of automated testing (1)An Overview of automated testing (1)
An Overview of automated testing (1)
 
Overview of Testing Talks at Pycon
Overview of Testing Talks at PyconOverview of Testing Talks at Pycon
Overview of Testing Talks at Pycon
 
@LinkingNote annotation in YATSPEC
@LinkingNote annotation in YATSPEC@LinkingNote annotation in YATSPEC
@LinkingNote annotation in YATSPEC
 
Effective Unit Testing
Effective Unit TestingEffective Unit Testing
Effective Unit Testing
 
Cuis smalltalk past present and future
Cuis smalltalk past present and futureCuis smalltalk past present and future
Cuis smalltalk past present and future
 
Lessons Learned When Automating
Lessons Learned When AutomatingLessons Learned When Automating
Lessons Learned When Automating
 
Delhi second draft
Delhi second draft Delhi second draft
Delhi second draft
 
Code Quality with Magento 2
Code Quality with Magento 2Code Quality with Magento 2
Code Quality with Magento 2
 
Delhi first draft_2
Delhi first draft_2Delhi first draft_2
Delhi first draft_2
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017
 
xUnit test patterns 0
xUnit test patterns 0xUnit test patterns 0
xUnit test patterns 0
 
An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1
 
The working architecture of NodeJs applications
The working architecture of NodeJs applicationsThe working architecture of NodeJs applications
The working architecture of NodeJs applications
 
Codeception @ New Business Dept Adira Finance
Codeception @ New Business Dept Adira FinanceCodeception @ New Business Dept Adira Finance
Codeception @ New Business Dept Adira Finance
 
Computational thinking
Computational thinkingComputational thinking
Computational thinking
 
DevOps Anti-Patterns
DevOps Anti-PatternsDevOps Anti-Patterns
DevOps Anti-Patterns
 
Tdd in swift
Tdd in swiftTdd in swift
Tdd in swift
 
Test-Driven Development (TDD) in Swift
Test-Driven Development (TDD) in SwiftTest-Driven Development (TDD) in Swift
Test-Driven Development (TDD) in Swift
 
Open source tools - Test Management Summit - 2009
Open source tools - Test Management Summit - 2009Open source tools - Test Management Summit - 2009
Open source tools - Test Management Summit - 2009
 

Similar to Keeping business logic out of your UIs

Viktor Turskyi "Effective NodeJS Application Development"
Viktor Turskyi "Effective NodeJS Application Development"Viktor Turskyi "Effective NodeJS Application Development"
Viktor Turskyi "Effective NodeJS Application Development"
Fwdays
 
From class to architecture
From class to architectureFrom class to architecture
From class to architecture
Marcin Hawraniak
 
The working architecture of node js applications open tech week javascript ...
The working architecture of node js applications   open tech week javascript ...The working architecture of node js applications   open tech week javascript ...
The working architecture of node js applications open tech week javascript ...
Viktor Turskyi
 
The working architecture of NodeJS applications, Виктор Турский
The working architecture of NodeJS applications, Виктор ТурскийThe working architecture of NodeJS applications, Виктор Турский
The working architecture of NodeJS applications, Виктор Турский
Sigma Software
 
'Effective node.js development' by Viktor Turskyi at OdessaJS'2020
'Effective node.js development' by Viktor Turskyi at OdessaJS'2020'Effective node.js development' by Viktor Turskyi at OdessaJS'2020
'Effective node.js development' by Viktor Turskyi at OdessaJS'2020
OdessaJS Conf
 
DDD with Behat
DDD with BehatDDD with Behat
DDD with Behat
Anton Serdyuk
 
Designing salesforce solutions for reuse - Josh Dennis
Designing salesforce solutions for reuse - Josh DennisDesigning salesforce solutions for reuse - Josh Dennis
Designing salesforce solutions for reuse - Josh Dennis
Sakthivel Madesh
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code clean
Brett Child
 
OutSystems Tips and Tricks
OutSystems Tips and TricksOutSystems Tips and Tricks
OutSystems Tips and Tricks
OutSystems
 
Baby steps to Domain-Driven Design
Baby steps to Domain-Driven DesignBaby steps to Domain-Driven Design
Baby steps to Domain-Driven Design
Žilvinas Kuusas
 
Advanced web application architecture - Talk
Advanced web application architecture - TalkAdvanced web application architecture - Talk
Advanced web application architecture - Talk
Matthias Noback
 
Enterprise PHP Architecture through Design Patterns and Modularization (Midwe...
Enterprise PHP Architecture through Design Patterns and Modularization (Midwe...Enterprise PHP Architecture through Design Patterns and Modularization (Midwe...
Enterprise PHP Architecture through Design Patterns and Modularization (Midwe...
Aaron Saray
 
Sharable of qualities of clean code
Sharable of qualities of clean codeSharable of qualities of clean code
Sharable of qualities of clean code
Eman Mohamed
 
Taming the Legacy Beast: Turning wild old code into a sleak new thoroughbread.
Taming the Legacy Beast: Turning wild old code into a sleak new thoroughbread.Taming the Legacy Beast: Turning wild old code into a sleak new thoroughbread.
Taming the Legacy Beast: Turning wild old code into a sleak new thoroughbread.
Chris Laning
 
Intro to ember.js
Intro to ember.jsIntro to ember.js
Intro to ember.js
Leo Hernandez
 
Clean architecture
Clean architectureClean architecture
Clean architecture
.NET Crowd
 
Microservices patterns
Microservices patternsMicroservices patterns
Microservices patterns
Vikram Babu Kuruguntla
 
Carlo Bonamico, Sonia Pini - So you want to build your (Angular) Component Li...
Carlo Bonamico, Sonia Pini - So you want to build your (Angular) Component Li...Carlo Bonamico, Sonia Pini - So you want to build your (Angular) Component Li...
Carlo Bonamico, Sonia Pini - So you want to build your (Angular) Component Li...
Codemotion
 
Build Your Own Angular Component Library
Build Your Own Angular Component LibraryBuild Your Own Angular Component Library
Build Your Own Angular Component Library
Carlo Bonamico
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
Inductive Automation
 

Similar to Keeping business logic out of your UIs (20)

Viktor Turskyi "Effective NodeJS Application Development"
Viktor Turskyi "Effective NodeJS Application Development"Viktor Turskyi "Effective NodeJS Application Development"
Viktor Turskyi "Effective NodeJS Application Development"
 
From class to architecture
From class to architectureFrom class to architecture
From class to architecture
 
The working architecture of node js applications open tech week javascript ...
The working architecture of node js applications   open tech week javascript ...The working architecture of node js applications   open tech week javascript ...
The working architecture of node js applications open tech week javascript ...
 
The working architecture of NodeJS applications, Виктор Турский
The working architecture of NodeJS applications, Виктор ТурскийThe working architecture of NodeJS applications, Виктор Турский
The working architecture of NodeJS applications, Виктор Турский
 
'Effective node.js development' by Viktor Turskyi at OdessaJS'2020
'Effective node.js development' by Viktor Turskyi at OdessaJS'2020'Effective node.js development' by Viktor Turskyi at OdessaJS'2020
'Effective node.js development' by Viktor Turskyi at OdessaJS'2020
 
DDD with Behat
DDD with BehatDDD with Behat
DDD with Behat
 
Designing salesforce solutions for reuse - Josh Dennis
Designing salesforce solutions for reuse - Josh DennisDesigning salesforce solutions for reuse - Josh Dennis
Designing salesforce solutions for reuse - Josh Dennis
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code clean
 
OutSystems Tips and Tricks
OutSystems Tips and TricksOutSystems Tips and Tricks
OutSystems Tips and Tricks
 
Baby steps to Domain-Driven Design
Baby steps to Domain-Driven DesignBaby steps to Domain-Driven Design
Baby steps to Domain-Driven Design
 
Advanced web application architecture - Talk
Advanced web application architecture - TalkAdvanced web application architecture - Talk
Advanced web application architecture - Talk
 
Enterprise PHP Architecture through Design Patterns and Modularization (Midwe...
Enterprise PHP Architecture through Design Patterns and Modularization (Midwe...Enterprise PHP Architecture through Design Patterns and Modularization (Midwe...
Enterprise PHP Architecture through Design Patterns and Modularization (Midwe...
 
Sharable of qualities of clean code
Sharable of qualities of clean codeSharable of qualities of clean code
Sharable of qualities of clean code
 
Taming the Legacy Beast: Turning wild old code into a sleak new thoroughbread.
Taming the Legacy Beast: Turning wild old code into a sleak new thoroughbread.Taming the Legacy Beast: Turning wild old code into a sleak new thoroughbread.
Taming the Legacy Beast: Turning wild old code into a sleak new thoroughbread.
 
Intro to ember.js
Intro to ember.jsIntro to ember.js
Intro to ember.js
 
Clean architecture
Clean architectureClean architecture
Clean architecture
 
Microservices patterns
Microservices patternsMicroservices patterns
Microservices patterns
 
Carlo Bonamico, Sonia Pini - So you want to build your (Angular) Component Li...
Carlo Bonamico, Sonia Pini - So you want to build your (Angular) Component Li...Carlo Bonamico, Sonia Pini - So you want to build your (Angular) Component Li...
Carlo Bonamico, Sonia Pini - So you want to build your (Angular) Component Li...
 
Build Your Own Angular Component Library
Build Your Own Angular Component LibraryBuild Your Own Angular Component Library
Build Your Own Angular Component Library
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
 

Recently uploaded

UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
Peter Muessig
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
gapen1
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
Octavian Nadolu
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
Rakesh Kumar R
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
Rakesh Kumar R
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Julian Hyde
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
Alina Yurenko
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
dakas1
 
YAML crash COURSE how to write yaml file for adding configuring details
YAML crash COURSE how to write yaml file for adding configuring detailsYAML crash COURSE how to write yaml file for adding configuring details
YAML crash COURSE how to write yaml file for adding configuring details
NishanthaBulumulla1
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
Marcin Chrost
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
kalichargn70th171
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
Remote DBA Services
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
XfilesPro
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
ShulagnaSarkar2
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
Alberto Brandolini
 
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
kalichargn70th171
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
Liberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptxLiberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptx
Massimo Artizzu
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
Quickdice ERP
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
Green Software Development
 

Recently uploaded (20)

UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s EcosystemUI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
UI5con 2024 - Keynote: Latest News about UI5 and it’s Ecosystem
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)Measures in SQL (SIGMOD 2024, Santiago, Chile)
Measures in SQL (SIGMOD 2024, Santiago, Chile)
 
All you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVMAll you need to know about Spring Boot and GraalVM
All you need to know about Spring Boot and GraalVM
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
YAML crash COURSE how to write yaml file for adding configuring details
YAML crash COURSE how to write yaml file for adding configuring detailsYAML crash COURSE how to write yaml file for adding configuring details
YAML crash COURSE how to write yaml file for adding configuring details
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
 
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf8 Best Automated Android App Testing Tool and Framework in 2024.pdf
8 Best Automated Android App Testing Tool and Framework in 2024.pdf
 
Oracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptxOracle 23c New Features For DBAs and Developers.pptx
Oracle 23c New Features For DBAs and Developers.pptx
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
 
14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision14 th Edition of International conference on computer vision
14 th Edition of International conference on computer vision
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
 
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
The Key to Digital Success_ A Comprehensive Guide to Continuous Testing Integ...
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
Liberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptxLiberarsi dai framework con i Web Component.pptx
Liberarsi dai framework con i Web Component.pptx
 
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian CompaniesE-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
E-Invoicing Implementation: A Step-by-Step Guide for Saudi Arabian Companies
 
GreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-JurisicGreenCode-A-VSCode-Plugin--Dario-Jurisic
GreenCode-A-VSCode-Plugin--Dario-Jurisic
 

Keeping business logic out of your UIs

  • 1. Keeping business logic out of your UIs Petter Holmström, Vaadin Architect petter@vaadin.com
  • 2. What is “business logic”? ● Every information system exists to solve a real-world problem (I hope...) ● A real-world problem consists of static parts (things, concepts) and dynamic parts (events, processes) ● A system uses a model of the real world to solve its problems ○ The domain model ● The static parts of the domain model are often the easy ones ● The dynamic parts are easily forgotten even though they are just as important ● The business logic is the implementation of the dynamic parts of the domain model
  • 3. Disclaimer: Business logic creeping into the UI is not really an architectural problem but a problem with developer discipline
  • 4. Let’s look at a “classic” architecture Persistence layer (JPA) Service layer (EJB) UI layer (Vaadin) Entities SQL database
  • 5. Problem 1 UI Operation Service 1 Service 2 Service 3 Actual transaction boundaries Logical transaction boundary
  • 6. Problem 2 UI CRUD Service+ Business logic in the user’s head and in the UI Service layer is only a frontend to the database
  • 7. Problem 3 UI 1 UI 2 UI 3 God Service
  • 8. Problem 4 Service A Service B New method ?
  • 10. Think about the transaction boundary ● Pay attention while coding and while reviewing others’ code ● The transaction boundary should IMO go between the UI and your backend ● Rules of thumb: ○ One UI operation, one backend call ○ One backend call, one transaction ○ One transaction, one thread ● Imagine your backend is running on a remote machine and you want to minimize the traffic
  • 11. Take care of your services ● Strive for highly cohesive services ● Avoid too general names like CustomerService ● Instead, opt for more specific names like CustomerOnboarding ● If you are adding a new service method and you find you could add it to more than one service, you should probably create a completely new one or refactor the existing ones
  • 12. Separate reading and writing ● Place your queries in separate components ● Create UI specific query components instead of adding more and more query methods to existing ones ● Allows for some neat optimizations and scaling
  • 13. Avoid CRUD ● CRUD UIs and services basically move the business logic into the head of the user ● Base your UIs and services on actual business processes and operations ● Exception: management UIs for reference and master data
  • 15. ● Think in terms of Commands and Queries ● A Command tells the system to perform a specific business operation ○ A command may return some result ○ A command either completes successfully or fails with an error ● A Query asks the system for specific data ○ A query always returns something ○ A query only fails because of external runtime errors such as network problems ● Commands and queries can be synchronous or asynchronous ● You can model each command and each query as a separate class Forget about services!
  • 16. The query/command class ● The name is always a verb in its imperative form ● The parameters are class attributes ● Use a generic parameter for the result, if any ● Try to make your class immutable ○ Not always possible, e.g. if you are binding a form directly to your command ● Validate the parameters inside your class ○ If you do this in the constructor, you cannot create a command or query with invalid parameters
  • 17. Examples <<command>> CreatePatient<Patient> firstName: string lastName: string gender: Gender birthDate: LocalDate <<query>> FindOrders<List<Order>> from: LocalDate to: LocalDate customer: Optional<Customer>
  • 18. Examples (code) public class CreatePatient implements Command<Patient> { final String firstName; final String lastName; final Gender gender; final LocalDate birthDate; public CreatePatient(String firstName, String lastname, Gender gender, LocalDate birthDate) { this.firstName = Objects.requireNonNull(firstName); … } }
  • 19. Examples (code) public class FindOrders implements Query<List<Order>> { final LocalDate from; final LocalDate to; final Optional<Customer> customer; public FindOrders(LocalDate from, LocalDate to) { … } public FindOrders(LocalDate from, LocalDate to, Customer customer) { … } }
  • 20. What about the implementation? ● Technically, queries and commands are implemented in the same way ● Each command/query has a handler ● The handler contains a single method that accepts the command/query as a single parameter and returns the result of the command/query ● Handlers are transactional ● Similar queries/commands can share a common base class ● Handlers can invoke other handlers
  • 21. Finding the correct handler ● A client will never invoke another handler directly ● Instead, commands/queries are passed to a broker or gateway ● The broker/gateway will look up the correct handler, invoke it and return the result
  • 22. Advantages ● An easy way of modeling the dynamics of the problem domain ● Forces you to think in terms of the domain while designing your UI ○ Since your UI should only be constructing and invoking commands/queries, the risk of business logic ending up there should be smaller (I hope) ● Your business logic consists of small, highly cohesive classes ● No more god classes ● Easy to distribute, e.g. by using a service bus ○ Command and queries could be serialized into JSON ○ Command and query handlers can be running anywhere ● Easy to run asynchronously ● You can use separate data sources for reading and writing ○ CQRS
  • 23. Master SlaveSlave Query Handler Query Handler Command Handler Broker UI
  • 24. Disadvantages ● More boilerplate code: for each operation, you have to write a command/query class and a handler class ● The client does not invoke the handler directly so if the handler is missing, it will not be detected until during runtime ● You have to write the command/query framework yourself ● You can still write too coarse or too fine grained commands
  • 25. Time for a code example!
  • 26. What about long-running transactions? ● Many commands participate in the same conversation ● If your container supports it, you can make your handlers conversation scoped ○ This makes it possible to store state in the handlers ● If you want your handlers to remain stateless, create a conversation object that is returned by and passed to every command handler ● The conversation object contains the state necessary to move the conversation forward
  • 27. Summary ● Discipline vs patterns ● Transaction boundary ● High cohesion ● Separate read and write ● Avoid CRUD ● Consider using commands and queries in business driven user interfaces