legacy code + new code
living together
Tommaso Allevi
@allevo
R&D TechLeader at Mia Platform
Everytime we need to interact with a legacy
but we want to survive
HOW?
Building software near by to a legacy one is not simple.
We need to find a way!
Let's start with an example!
A simple implementation of this pattern could be using HTTP protocol on which the decorator accepts the request/response in input and reply according the the action it would make
Business Logic
API
Decoratore pre1
Decoratore pre2
Decoratore pre3
...
Decoratore post1
Decoratore post2
Decoratore post3
...
Flow
{
"/incoming_url": {
"GET": {
"pre": [ "pre1", "pre2" ],
"call": "http://business-service/proxied-url",
"post": [ "post1" ]
}
},
"decorators": {
"pre1": {
"call": "http://some-service2/decorators/path1",
},
"pre2": {
"call": "http://other-service/decorators/path2",
},
"post1": {
"call": "http://some-service3/decorators/path3",
}
}
}
GET /incoming_url?foo=bar
pre1
pre2
business-service
GET /proxied-url?bar=foo
post3
The Microservice Decorator impose an HTTP interface that each decorator has to implement
A microservice is not its API
but its Business Logic
Is this a limit?
No
Ports and Adapter Pattern
Microservice Decorator
Microservice Decorator
Legacy
Login
New Login Service
POST /login
Microservice
Decorator
POST /login
POST
/decorators/newLogin
POST /login
{ username: "aa", password: "bb" }
---> Set-Cookie: sid="abcd"
---> { userId: "1234" }
POST /decorators/newLogin
Set-Cookie: sid="abcd
{ userId: "1234" }
---> Set-Cookie: sid="abcd"; sid2="9876"
---> { userId: "1234" }
POST /login
{ username: "aa", password: "bb" }
---> Set-Cookie: sid="abcd"; sid2="9876"
---> { userId: "1234" }
{
"/login": {
"POST": {
"pre": [ ],
"call": "http://legacy-service/login",
"post": [ "makeNewLogin" ]
}
},
"decorators": {
"makeNewLogin": {
"call": "http://new-login-service/decorators/newLogin",
}
}
}
No legacy lines were changed!
We have added only the lines for the new component and change a configuration
Microservice Decorator
Microservice Decorator
Legacy
Login
New Login Service
Microservice
Decorator
CSRF
Service
1
2
3
{
"/login": {
"POST": {
"pre": [ "checkCSRF" ],
"call": "http://legacy-service/login",
"post": [ "makeNewLogin" ]
}
},
"decorators": {
"makeNewLogin": {
"call": "http://new-login-service/decorators/newLogin",
},
"checkCSRF": {
"call": "http://csrf-service/decorators/checkCSRF",
}
}
}
(we are hiring!)