how to organize the business layer in software
DESCRIPTION
how to organize your business codeTRANSCRIPT
![Page 1: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/1.jpg)
Business Layer Architecture
Arnaud LEMAIRE
@lilobase
![Page 2: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/2.jpg)
Business layer ?
this is why you get paid (usually)
![Page 3: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/3.jpg)
WHERE TO PUT THE BUSINESS CODE ?
OK, important business, so…
![Page 4: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/4.jpg)
In MVC application
![Page 5: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/5.jpg)
In the View ?
![Page 6: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/6.jpg)
In the Controller ?
![Page 7: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/7.jpg)
Controller I/O are HTTP requests
• Hard to write unit test
• Impossible reuse
![Page 8: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/8.jpg)
In the Model ?
![Page 9: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/9.jpg)
But what is a model ?
• A persistance Layer (ie: representation of a dbstate) ?
• A class to deal with a ressource ?
• Model is the most misunderstanding layer
![Page 10: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/10.jpg)
the most misunderstood layer
![Page 11: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/11.jpg)
Fat model approach
• People don’t understand what a model is
• Final source code is a bloated God Object
• Break Single Responsibility Principle (SOLID)
• Hard to reuse
• What about isolation ?
![Page 12: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/12.jpg)
So… Stop talking about the model
• But about “domain layer” for business things
• And “persistence layer” for database things
![Page 13: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/13.jpg)
MVC IS NOT ABOUT A BUSINESS LAYER !
And, so… where do we put the business code ?
![Page 14: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/14.jpg)
Business Layer
![Page 15: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/15.jpg)
People thinks its complicated
• Its about:
– Framework
– Services libraries
– [insert other complicated things]
![Page 16: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/16.jpg)
its just OOP
![Page 17: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/17.jpg)
POJO / POPO / PORO
![Page 18: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/18.jpg)
WHAT DOES “GOOD OOP” MEAN ?
![Page 19: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/19.jpg)
High cohesion / loose coupling
• High cohesion : SRP
– One reason to change
• Loose coupling : Seams
– Changing implementation
– DIP, EOP, …
![Page 20: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/20.jpg)
Vehicle extends Motor
![Page 21: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/21.jpg)
Composition over Inheritance
Vehicle
Car Boat
Motor is a part of and not a type of
![Page 22: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/22.jpg)
Dont use inheritance to decouplebusiness layer !
![Page 23: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/23.jpg)
These things are also inheritance:
• Module/Mixin (ruby)
• Traits (java, python, php)
• Roles (perl)
![Page 24: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/24.jpg)
API oriented code
AKA :
• Information hiding
• Open/Close principle
Only high business value methods are availablein the application
- and not the database query layer -
![Page 25: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/25.jpg)
LET’S START DECOUPLING
Things are getting real
![Page 26: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/26.jpg)
Value object
• Equality on value not on identity
– Identity: Smith ≠ William
– Value: 1€ = 1€ = 1,36$
• Currencies, Ratings, Date, …
![Page 27: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/27.jpg)
Entity Object
• Anything that has a unique and separateexistence.
• User, Book, Request, etc.
• Not only persisted data (see Request)
• Hard to have with ActiveRecord (see nextslide)
• Many of the same entities can be groupedinto a Collection Object
![Page 28: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/28.jpg)
Repository Object
• Return entity by making complex queries in the datastore (because db in not only SQL now)
• Can be a Query Object (especially in the Rails world)
![Page 29: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/29.jpg)
Validation object
• About validation
• Also known as Form object
![Page 30: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/30.jpg)
View Object
• Helpers sucks (a lot, really. You know the SRP…)
• Use domain specific object
![Page 31: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/31.jpg)
Policy Object
• About extracting something from the loadedcontext
• Like a query but in a collection of entities (or just one)
• isConnected ?, specific sort, ...
![Page 32: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/32.jpg)
Decorator Object
• Useful to add new behavior not directly linkedto the core domain of the class
• Use Wrapper objects for transformation
![Page 33: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/33.jpg)
Service Object
• API part of a concept
• High value business part
• Tested !
![Page 34: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/34.jpg)
![Page 35: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/35.jpg)
WHERE DO I PUT THIS BUNCH OF NEW CLASS ?
Yep, but you still didn’t answer the question…
![Page 36: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/36.jpg)
In fact… wherever you want
It’s just OOP, remember?
![Page 37: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/37.jpg)
OK, two main organizations
• By domain:
– Banking/Entity/
BankAccount
Service/Deposit
• By type:
– EntityBanking
BankAccount
LibraryBookShelf
the domain approach is preferred
![Page 38: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/38.jpg)
Warning
![Page 39: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/39.jpg)
Lots of pitfalls
• Anemic model
• To many abstraction
• Procedural programming
• Don’t forget « Simple design rules »
• Lots of freedom : second system pitfall
![Page 40: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/40.jpg)
![Page 41: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/41.jpg)
Implementation detail handledthrough abstraction
• More generic code
Implementation detail
Abstraction, we use this layer to
manage codeStorage
FileStorage MemoryStorage
• Don’t forget to test the contract (ie: interface)
![Page 42: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/42.jpg)
Plenty of cool other stuff
• DCI : Data Context Interaction
• EBI : Entity Boundary Interactor
• CQRS : Command Query ResponsibilitySegregation
• and soon… IDD : Iteraction Domain Model (spoiler: this is a MVC for Domain layer)
![Page 43: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/43.jpg)
End
![Page 44: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/44.jpg)
Wait,
![Page 45: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/45.jpg)
TRY TO TDDIT COULD SAVE YOUR LIFE, REALLY !
![Page 46: How to organize the business layer in software](https://reader034.vdocuments.us/reader034/viewer/2022042702/5595a6601a28ab86168b4731/html5/thumbnails/46.jpg)
Thank you!