DAO recommendations (data access object) - The examples I see use the DAO object and services, and what is the best practice here?

I am creating a data access object to retrieve information from the Google App Engine for a web application based on the Spring framework (first time for everyone).

I see a series of examples that use the Controller / webapp -> Service -> DAO -> JDO / Google-app-engine template.

In this template, the DAO level is the only one that knows about JDO, so this layer is the only one that needs to be replaced if the data store has changed. The service layer invokes the DAO layer and formats / processes the necessary data.

My question is why an extra level of service? At least at first it doesn't look like the level of service adds much to the equation. Naturally, I would just think of writing a DAO layer to encapsulate JDO requests and manipulate and return data.

Can someone show me what is rational for a particular level of service? Will it become obvious as the project becomes large and complex?

+6
design-patterns dao
source share
2 answers

Usually you put the DAO at the service level, because as your application becomes more complex, you will do useful and non-trivial things in the service. For example, you can coordinate complex data operations with more than 1 DAO. Service levels also provide API boundaries that distinguish between cross-cutting issues such as transaction management, authorization checks, performance logging, etc.

Another reason you can divert your functionality to services is that it encourages the use of reusable and supported components. When you start, you may be interested in just submitting some html. You write a service that loads some data, and processes the html part in a layer above the service level (presentation level). Now you want to support the RESTful web service. Your service level can be reused to download data; all you need to worry about is json or xml returned by the endpoint of the web service (and, of course, the REST semantics).

Thus, for simple cases, the level of service may increase slightly, but as your application expands, they become useful and even important to ensure clean code.

+5
source share

Yes. Initially, it seems that the level of service does not add much to the equation. But think of it this way.

Service Layer - the layer between the presentation and the business layer.

You must have known that it is always good to share anxiety between layers. Your service level can be displayed in different business domains, presentation layers, without worrying about how your DOA level is used.

You can see this as the border between two other layers, in this case, between the presentation and business layers. Presentation-level code typically implements use cases. A typical use case is a sequence of actions performed by a user that result in interactions between one or more business objects, workflows, and services. The service level allows you to abstract these smaller interactions with the middleware API provided by the more coarsely granular services. The presentation layer makes one call to one service in the layer. The service level method invoked will coordinate the objects and workflows of the business layer to implement the required behavior.

Security concerns

  • I am creating a security layer around service stacks. This will help me identify the authenticity of the user and gain access to this service.
  • I also have a transaction level defined around a service level. It tells the database engine to make changes to the service level after it returns after successful executions.

These things should also be a concern if you determine the levels of your application.

+2
source share

All Articles