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 -
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
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 .
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.