What is the better way of organising your typical MVC web-application or REST services – Repository or Dao or DataAccess, Service and Controller classes.
Context – ways of organising MVC application’s Services & Repository
You may have few 10s of tables or entities. Most of these co-relating with your Repository classes or interfaces (in case of SpringData). You have approximately 5-10 modules from for which you would need your REST services or Controllers. How to make these modules talk with the repositories. Note: It is assumed that Repository i.e. DataAccess layer i.e. dumb layer with primary responsibility to encapsulate DB specific logic ONLY. Service layer i.e. business logic layer. Here are several options for organisations.
- Service per Module: Create a Service interface & class per module, which will inject all the dependent repositories. Services will inject repositories – NOT other services.
- Service per Repository: Create a Service interface & class for each repository. REST controller will typically inject many service implementations. Services will inject Repository it is responsible for or it MAY in inject other services. And of course, any other reusable components like EmailService, ImageProcessor, Indexer and so on.