Is having 3 layers Controller, BO and DAO a standard way? why not just Controller and DAO?

1.1k Views Asked by At

In REST Web Service, is having 3 layers - Controller, BO (Service) and DAO a standard way? Why do we need a separate BO layer. Why not to write our logic in Controller class which, calls different DAO classes ??? So, basically I want to have all JAX-RS annotations like -

@GET

@Path("/{parameter}")

@Produces("application/json")

in Controller interface and only root @Path in Controller implementation. I don't want to unnecessarily have another layer of BO. I want to reuse code by calling the Controller classes itself from other Controller classes. I know the classes are annotated but that's for JAX-RS runtime to handle it appropriately. Can I still instantiate those Controller classes and call different methods from other Controllers???

2

There are 2 best solutions below

8
On

I see two main reasons :

  1. Separation of concerns
  2. Reusability : Many controllers might need to use the same business logic
1
On

To separate business logic from controller, as DAO only interact with database, so it does not contain any business logic ideally.

You can consider the business logic being used by multiple modules like controller, rest service, SOAP service etc.

Example: Suppose you have an add functionality and you want it to work from GUI as well as SOAP service. If you write your business logic (of validation, conversion, calculation etc) in controller then you will have to rewrite it for SOAP service also. Which will result in redundancy and you will not be able to reuse your code.


For updated question:

In the starting phase of development it looks ok to include things at one place, but as time passes code becomes so large that it creates problem to manage the code. In your case, consider you are using one controller inside another controller. In future it might create cyclic dependency among controllers.

Example: ControllerA requires a functionality present inside ControllerB. ControllerB requires a functionality present inside ControllerA. So in this case both are dependent on each other. So it will be difficult to understand the code flow. And also at the time of Injection this will create problem.