简体   繁体   中英

Service and DAO classes in JAVA

I have 3 DAO and service classes for project , customer , and issues . I have one more jiraService class in which I use the JIRA API. The use case is that I use APIs to create the issue in JIRA for a customer's project. Once the issue is created I store the customer info. in the customer table, the response info. from JIRA in issue table and then store the issue id, customer id in project table so that I know which issue is related to a which customer and project. Now I have some questions -

  1. How should I call the DAO classes from jiraService class? Is it good to directly call the required DAO class as I am in a service class or should I call it through the service class which I have created for that DAO class?
  2. DAO classes are supposed to contain CRUD methods only. But there can be variations in say READ operation. For example, I may want to get projects on the basis of customer name or on the basis of issue id or both. We cannot create a generic GET method for this. So is it considered a good practice to include all required variations of CRUD operations in DAO classes?
  3. How should one DAO class call methods within other DAO class? For example, I have the customer name and I am fetching all project info for that customer. In my project table I have customerId stored so I need to first get the customerID for that customer and then fetch all the projects from project table using customer ID. How should I call the getCustomerID method within customerDAO from projectDAO class?
  1. Why you have a service class in DAO? You should be calling your DAO class methods from your JiraService class.
  2. If you can't create a generic method, you should just create different for each case. I don't think that it has anything to do with good practice on CRUD operations in DAO classes.
  3. I think it's better to have service method for each DAO call. It's much cleaner. You can maybe use a single service class to call both DAO methods. Calling DAO from other DAO may cause circular dependency in future.

Ok, first of all, you shouldn't call one DAO class directly from another one (3), as that would break the security. The correct would be to create both objects and instantiate the first from the second. For example, you get your customerID searching by the name of a customer and that is stored withing your customerDAO object. Then, with that info you fetch all projects for that customer on a projectDAO object usign the info from the customerDAO object, but not directly calling the customerDAO into de projectDAO object, as that would make a dependency on projectDAO.

You want them to be as indepent as possible, so you connect them on another class that is used as a controller, for example your jiraService class. With that, the looks of the code should be something like this:

CustomerDAO cdao= new CustomerDAO();
ProjectDAO pdao = new ProjectDAO();
//I will suppose that you used numeric ID and use a long variable to store the result
long id=cdao.getCustomerID("MyCustomerName");//This is just an example
List<Project> lp= pdao.getProjectsByCusID(id);

Then, whith that said, you just need to call the DAO objects from jour jiraService class (1) as this will not affect your code.

Lastly, as you said before, DAO are supposed to contain CRUD methods (2), this means that you can implement every single CRUD method that you consider you'll use. If you think you'll need a getProjectsByName(String) method, just implement it, is ok as is your own code and is a CRUD method, so is ok on the DAO class.

Hope it helped :)

From the above, it is clear that that this 2N-Tiered design. The following is assumed: Services are named ProjectService, CustomerService, IssueService and JiraService; the DAO classes are ProjectDAO, CustomerDAO and IssueDAO. Please keep in mind that the dependancy needs to be on the abstraction not concretions. Nice article on the Solid Principles

  1. The purpose of the JiraService is to update JIRA through the use of an API. It may not be prudent to save to the Data Access layer from the JiraService as this is a violation of the single responsibility principle .

    • a. Controller class to call the JiraService. On completion control is returned back to Controller
    • b. The Controller can then call any of the other Services(ProjectService, CustomerService, IssueService) which connects the DAO Layer(Preferably by an Interface eg IProjectDAO) to persist the data.
  2. Time for some Inheritance - The Parent class would have the basic CRUD operations - example BaseDAO which can be extended by all Child DAO classes. Specific behavior can be added to the child classes. Example: CustomerDAO.getCustomerByID(), CustomerDAO.getCustomersBySurname().

3 The relationship between the Entities needs to be defined. JPA can be used to achieve this. An alternate approach would be to have a DAO which retrieves all data from the related tables and maps the data to the Model/Value Object - Example Customer, Project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM