Use a sql.Connection class for each task or only one referenced globally? [closed]

0

I have a class that is responsible for checking the user's account and password when logging in, and in the future I will have to implement another class to get other data in the bank and create a new connection.

Would it be a good idea to use only a sql.Connection class in terms of code performance and readability? Could it lead to future problems?

    
asked by anonymous 07.11.2016 / 02:30

1 answer

4

Hello, young man!

It is recommended that you create a Database Connections Factory, a Data Access Object (DAO) to manipulate the access and acquisition of the information to the bank, and finally, one that allows you to validate whether the information is valid or not .

In this way you will be able to maintain a high cohesion a low coupling.

See, at the top layer, what I will call Validator, you receive your object with user properties such as login and password. In this validator you request the information for the bank through DAO - which I would recommend injecting - and then approve whether the values are consistent or not. In DAO you check which bank you should access, and then call your factory. This can be by reading some system property, reading some external file, or even just requesting another class.

Following this idea, if you have any problem in the validation, you only change in the Validator; if you have problems with the query, change in DAO; Finally, if it is a problem in creating the connection, change in the factory.

Any questions, just talk!

Greetings

Edited

Here you can find an example: Example

    
07.11.2016 / 12:20