I am looking at a code base that has multiple APIs and traditional server rendered website. The business logic is duplicated between each of the APIs. I would like to clean this up and share the business logic.
The flow I am looking at is the controller would be called. The json would be mapped to the Person class (in [api|api2|website]/model/request/person). The Person object would be validated and then passed into the RequestMapper. The result of that would be the person class (model/request/person). This object would be passed into Service in the same directory which would do whatever it needs to and then call the service in the service directory. That service does whatever it needs to do, calls the repository, etc. It would then create the person (model/response/person) and return that. The service in api/api2/website directories would then use the ResponseMapper to map it to [api|api2|website]/model/response/person which would be returned by the controller.
To maybe better illustrate what I am saying the directories would like look this
src/Person api Controller Service RequestMapper ResponseMapper Model Request Person Response Whatever api2 Controller Service RequestMapper ResponseMapper Model Request Person Response Whatever website Controller Service RequestMapper ResponseMapper Model Request Person Response Whatever Model Request Person Response Person Service / Business Layer PersonService Repository PersonRepository
This seems to work ok, but I have issues with how many different Person objects there are. There are a lot of directories and there is a lot of mapping between objects. It feels like this is significantly more complicated than it needs to be. Is this the correct way to architecture such an application? What should the various Person objects be called? Just person?