I am working on legacy code where a method is supposed to return results based on mulitple parameters. The pattern used in code works but I'm thinking if it could be improved further. I'll give an example
Let's say I have a class JobPosting which has various properties
class JobPosting {
int jobPostingId;
String description;
Dept dept; // A posting ca belong to 1 department at a time
Location loc; // A job posting can belong to 1 location at a time
String created By;
int headcount; // number of people required to fill the job posting
List<User> assignedUsers; // number of people who have applied for the posting already
List<User> reservedUsers; // users who are eligible to apply for the posting, if this is empty, then anyone can apply to posting
DateTime visibleDate; // date from when posting is visible to users
Date endDate; // posting's deadline
...
}
These details for a posting is stored in DB in one table
Now I have various use cases to get the postings:
- Get job postings for a loc & dept- I only care about description and headcount.
- Get job posintgs for a loc & dept with users who have filled the postings
- Get job postings for a loc & dept with users who have filled and users who are eligible for the postings
- Get job postings for a loc & dept which are visibleToUsers (visibleDate < curdate)
and so on, I can have various possibilities for getting the postings depending on the scenario.
Current implementation is that we have builder pattern for parameters
class postingsParams {
boolean excludeFilledPostings;
boolean excludeNonVisiblePostings;
boolean returnAssignedUsers;
boolean returnEligibleUsers;
}
and one method which takes in parameters and determines what fields in JobPostings to fill and what postings to exclude
getPostingsByLocDept(loc, dept, postingsParamas) { ...// code calls dynamic SQL queries to return results and if conditions to filter }
This seems to work fine, but what I don't like is
- Each caller must be aware of all the possible crtireia to search
- If in future I want to add another criteria like excludePostingsWithPassedDeadline, I need to make changes to this method and need to test all the places where it is called to make sure nothing breaks
One approach is to create specific get methods for the use case, but with that approach we may end up with multiple get methods, which may not be desirable.
Is there a way to improve this design or is the current approach best way to deal with this kind of scenario?
Aucun commentaire:
Enregistrer un commentaire