Skip to content

DAO Factory pattern for Issue #1270 #2105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from

Conversation

rrreynaldo
Copy link

@rrreynaldo rrreynaldo commented Oct 17, 2022

Closes #1270

@rrreynaldo
Copy link
Author

Hi @iluwatar, this is the implementation for the DAO Factory based on the following resources and it is my first issue; kindly inform me if there is incorrect logic/concept or things which could be improved. Thanks a lot!

Copy link
Contributor

@burno1 burno1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would only suggest to change that switch case statement, unless you have a fair position to have it like that

Copy link
Contributor

@burno1 burno1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@burno1
Copy link
Contributor

burno1 commented Oct 17, 2022

Thank you, @rrreynaldo!

@rrreynaldo
Copy link
Author

Hi @burno1, thanks for the review and I agree that enum would be a neater implementation. Just made the changes and Thanks a lot!

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@burno1
Copy link
Contributor

burno1 commented Oct 17, 2022

This closes #1270

@burno1
Copy link
Contributor

burno1 commented Oct 17, 2022

@rrreynaldo, add the issue to the description of the PR, so we can have the two linked

@rrreynaldo
Copy link
Author

@burno1 Done!

Comment on lines +2 to +9
layout: pattern
title: Data Access Object Factory (DAO Factory)
folder: dao-factory
permalink: /patterns/dao-factory/
categories: Architectural
language: en
tags:
- Data access
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check out the updated yaml frontmatter requirements at https://github.com/iluwatar/java-design-patterns/wiki/01.-How-to-contribute. In essence, you can drop some of the tags here.


DAO Factory is a design pattern which utilise the Abstract Factory and Factory Method to create and produce a new instance of DAOs that are needed by the client application by creating a concrete Factory for a specific data source type.

## Explanation
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, please add here the standard items

  • real world example
  • in plain words
  • wikipedia (or some other authoritative source) says

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.1</version>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the version from the parent pom.xml

Comment on lines +20 to +22
public int getId() {
return id;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Lombok where possible to reduce boilerplate

Comment on lines +3 to +5
/**
* An implementation example for a client code
*/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please explain the pattern briefly and how this example code implements it.

Comment on lines +9 to +12
// Connection variable information to Postgres database
private static final String conn = "jdbc:postgresql://localhost:5432/dao_factory";
private static final String user = "postgres";
private static final String pass = "123";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understood correctly, running the example requires that a local PostgreSQL database server is running? Usually what we do is use an in-memory database (like H2) by default and then it's possible to configure the code to use an external database, but not required.

Comment on lines +39 to +40
e.printStackTrace();
System.err.println(e.getClass().getName()+": "+e.getMessage());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use logger

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check this throughout the code

Comment on lines +10 to +19
@Test
void testInsertOrder() {
Method method = null;
try {
method = PostgresOrderDAO.class.getMethod("insertOrder", String.class, String.class, String.class);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
assertNotNull(method);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see much value in this kind of reflection-based tests

Comment on lines +60 to +98
Order template = new Order();
Connection con = PostgresDAOFactory.createConnection();
Statement stmt;
ResultSet rs;
String sql = "SELECT * FROM orders WHERE (";
boolean firstCondition = true;

// Combining the query with the variable value depending on the
// attribute that has been passed to the function
if (senderName == null && rcvrName == null && destination == null) {
return template;
}
// Combining the senderName attribute
if (senderName != null && !senderName.equals("")) {
senderName = "'" + senderName + "'";
sql += "sender_name = " +senderName;
firstCondition = false;
}
// Combining the receiverName attribute
if (rcvrName != null && !rcvrName.equals("")) {
rcvrName = "'" + rcvrName + "'";
if (firstCondition) {
sql += "receiver_name = " + rcvrName;
firstCondition = false;
} else {
sql += " AND receiver_name = " + rcvrName;
}
}
// Combining the destination attribute
if (destination != null && !destination.equals("")) {
destination = "'" + destination + "'";
if (firstCondition) {
sql += "destination = " + destination;
} else {
sql += " AND destination = " + destination;
}
}
// Closing the sql statement
sql += ");";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General note about this kind of SQL query building using string concatenation

  • High risk of SQL injection if you accidentally concatenate user input to your SQL queries
  • Difficult to avoid syntax errors in non-trivial cases, when dynamic SQL gets more complex

I would recommend using JPA criteria queries or jOOQ to make the code cleaner and safer.

Comment on lines +6 to +8
public enum DataSourceType {
POSTGRES // And other data source type
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could have here another data source H2

@stale
Copy link

stale bot commented Nov 23, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the status: stale issues and pull requests that have not had recent interaction label Nov 23, 2022
@iluwatar
Copy link
Owner

iluwatar commented Dec 1, 2022

Closed due to inactivity

@iluwatar iluwatar closed this Dec 1, 2022
@iluwatar iluwatar added this to the 1.26.0 milestone Dec 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: stale issues and pull requests that have not had recent interaction status: under review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DAO Factory pattern
3 participants