gene b.
gene b.

Reputation: 11974

Writing a Mostly Client-Side App Without Controllers (but still within the Spring framework)

We are writing a mostly single-page, client-side app, but server-side/DB endpoints are still required of course, so the natural choice is SpringMVC (since we are a Java / Spring shop).

But this got me thinking why we need the cluttered, very old design for this app:

  - Controller layer
  - Service layer
  - DAO layer

This app is mostly just the client side making AJAX calls with JSON for DB retrieval/persistence. Do I really need to go thru the Controller layer to receive requests, then invoke a Service method, which in turn invokes a DAO method?

At the same time, I don't want to write a REST Service because it could result in overhead and we may not support all of the REST requirements... but is it the right choice here? If I understand correctly, I would still need a RESTController on the presentation layer?

My goal is to just directly hit a Service method or, maybe even more directly, a DAO method. Is that how modern apps are written?

Upvotes: 0

Views: 48

Answers (1)

Alessandro Santini
Alessandro Santini

Reputation: 2003

You cannot hit a DAO unless you expose it through an API of some sort that can be invoked remotely by the UI application; as a consequence, you need to write a service.

A convenient way of exposing a service is to either:

  • Use Spring MVC and use the controllers as stateless endpoints that provide a JSON/Protobuffer/XML sort of payload that is then parsed by your API (with JSON being the simplest option of them all, perhaps) or
  • Use Spring Boot, which uses Spring MVC under the hood.

Hope this helps and good luck with your project.

Upvotes: 1

Related Questions