how to organize the business layer in software

46
Business Layer Architecture Arnaud LEMAIRE @lilobase

Upload: arnaud-lemaire

Post on 03-Jul-2015

1.975 views

Category:

Software


1 download

DESCRIPTION

how to organize your business code

TRANSCRIPT

Page 1: How to organize the business layer in software

Business Layer Architecture

Arnaud LEMAIRE

@lilobase

Page 2: How to organize the business layer in software

Business layer ?

this is why you get paid (usually)

Page 3: How to organize the business layer in software

WHERE TO PUT THE BUSINESS CODE ?

OK, important business, so…

Page 4: How to organize the business layer in software

In MVC application

Page 5: How to organize the business layer in software

In the View ?

Page 6: How to organize the business layer in software

In the Controller ?

Page 7: How to organize the business layer in software

Controller I/O are HTTP requests

• Hard to write unit test

• Impossible reuse

Page 8: How to organize the business layer in software

In the Model ?

Page 9: How to organize the business layer in software

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

the most misunderstood layer

Page 11: How to organize the business layer in software

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

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

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

Business Layer

Page 15: How to organize the business layer in software

People thinks its complicated

• Its about:

– Framework

– Services libraries

– [insert other complicated things]

Page 16: How to organize the business layer in software

its just OOP

Page 17: How to organize the business layer in software

POJO / POPO / PORO

Page 18: How to organize the business layer in software

WHAT DOES “GOOD OOP” MEAN ?

Page 19: How to organize the business layer in software

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

Vehicle extends Motor

Page 21: How to organize the business layer in software

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

Dont use inheritance to decouplebusiness layer !

Page 23: How to organize the business layer in software

These things are also inheritance:

• Module/Mixin (ruby)

• Traits (java, python, php)

• Roles (perl)

Page 24: How to organize the business layer in software

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

LET’S START DECOUPLING

Things are getting real

Page 26: How to organize the business layer in software

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

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

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

Validation object

• About validation

• Also known as Form object

Page 30: How to organize the business layer in software

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

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

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

Service Object

• API part of a concept

• High value business part

• Tested !

Page 34: How to organize the business layer in software
Page 35: How to organize the business layer in software

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

In fact… wherever you want

It’s just OOP, remember?

Page 37: How to organize the business layer in software

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

Warning

Page 39: How to organize the business layer in software

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
Page 41: How to organize the business layer in software

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

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

End

Page 44: How to organize the business layer in software

Wait,

Page 45: How to organize the business layer in software

TRY TO TDDIT COULD SAVE YOUR LIFE, REALLY !

Page 46: How to organize the business layer in software

Thank you!