0
votes

Using Spring MVC to develop a web application. Earlier I was using Controller layer, Service layer(business logic), Model layer(entity) and DAO(DB) layer.

But someone pointed out that i should introduce two more layer ie. dto layer for collecting data from front end and transforming layer which will convert than dto into model(entity) layer objects.

Now I am using:

  1. Controller layer (which will send the data to DTO layer)
  2. DTO layer (which will send the its data to transforming layer)
  3. Transforming layer(for converting dto layer objects into entity layer objects)
  4. Service layer(Business logic)
  5. Entity layer(POJO which will map with database)
  6. DAO(which will use entity objects to store the database)

In this way we can keep front end and backend data different. Please help me out, is this a proper structure for Spring MVC ?

3
DTO is not a layer. Someone pointed you to the wrong direction.Roman C
@RomanC So for form backing objects which layer i should use, if its entity layer then again i am using entity layer to store data in the db. Can you just explain a little more ?Sagar Rout
It's called a view layer, you can use its objects that you return from the backend. I don't know how it's related to MVC where the view and model is communicated bidirectional, but it's used in resent MVVM and MVW architectures.Roman C
@RomanC So we don't need anything like DTO in our project ? We can simply use entities to collect data from the front end ? and both front end and database will use entities for their purposeSagar Rout
@blackOcean What kind of entities? Do you mean ORM entities? You can use entities and DTOs whatever you need, but everything should be baked up on it's own layer. I've just pointed out that your understanding layers is incorrect.Roman C

3 Answers

0
votes

There is an interesting question answer thread on MVC here:https://softwareengineering.stackexchange.com/questions/127624/what-is-mvc-really

Some of the key points to remember when designing an application should be; layered and loosely coupled.

In your scenario, having additional transform layer does not necessarily make our break MVC pattern. It is just an additional layer you have introduced in MVC; a design strategy followed by many.

0
votes

DTO is just a pattern to encapsulate data. Normally is used to be returned in your controllers, but you don't need always to create a DTO, you can use your entities for this. I just use a Dto when i need a different data structure, that any entity support, like a report for example.

About your project layers Model/Dao/Service/Controller it's correct, i strongly recommend for you the book Domain-driven design, it's will help you to build your software architectures!

0
votes

But someone pointed out that i should introduce two more layer ie. dto layer for collecting data from front end and transforming layer which will convert than dto into model(entity) layer objects.

No, it's incorrect. You should use only layers that are necessary.

Earlier I was using Controller layer, Service layer(business logic), Model layer(entity) and DAO(DB) layer.

This is also incorrect. There's no strict definition of MVC is a pattern, but it's used only on one layer called a view layer, or in other terms presentation layer. Where the model, view, and controller reside. There's another layer called a service layer, which is optional. Then persistence layer, in this layer you model your objects and save them in the database or somewhere else. No more layers are necessary to the simple web application.

A few words about model, it's a set of classes that you want to persist or use in the view layer to show/store the data. Here you can play with it, because someone prefer to use it's own model to show/store the data, and others prefer their own model to persist it. These transformations are usually done in persistence or service, or in both layers. Keep in mind that all layers a loosely coupled to each other, but you can transfer data beans from one layer to another and back without problems. Entities in the detached state or other beans like DTOs are examples of such objects that you can pass and bake between layers. And no additional layers are necessary.