S
S
stas82015-02-25 17:23:31
Software design
stas8, 2015-02-25 17:23:31

How to develop a competent application architecture: working with a database?

How to properly design a layer responsible for working with a database in an application?
Why is the approach when all read / write operations are performed by a separate DbManager class bad?
4090a77f001b3f482f0d57e5fb60e3.png
How to do it right? Relevant classes UserService, ArticleService, etc., which will be responsible for this? But then you still need a class that will be tied to a specific implementation (for example, working with the MySQL database through the JDBC driver), right?
a2e6782c411b6144e046cff2d22ad4.png
What are the pros and cons of both solutions? Maybe it needs to be done in a different way?
Previously, the approach from the first diagram seemed correct and logical to me, but recently I come across mentions that this should not be done (literally today, for example, here ).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Kiselev, 2015-05-13
@stas8

The first approach is bad in that you create a God-object in it, it takes on too much responsibility, which is contrary to the SRP principle . The class will have a large size and will constantly change as functionality changes.
The second approach is almost correct. The only missing interfaces are UserServiceInterface and ArticleServiceInterface, which describe data access methods. Usually such interfaces are called Repository, not Service. These interfaces must be implemented in concrete classes such as OracleUserService and OracleArticleService. To interact with the database, these classes use only session (Session) or connection (Connection). In your example it is DbHandler.
Read about the Hibernate appliance. He uses the second approach.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question