Task 6: Refactor to DRY the Support Classes
Due to the fact that this code is being auto-graded as you work through it, make sure that you use any and all names for classes, variables, methods, etc provided to you in these directions.
Review the code in the Employer
, Location
, CoreCompetency
, and PositionType
classes. What similarities do you see?
There is a fair amount of repetition between the classes. As a good coder, anytime you find yourself adding identical code in multiple locations you should consider how to streamline the process. DRY = âDonât Repeat Yourselfâ.
Create a Base Class
Letâs move all of the repeated code into a separate class. We will then have Employer
, Location
, CoreCompetency
, and PositionType
inherit this common code.
- Create a new class called
JobField
. - Consider the following questions to help you decide what code to put in the
JobField
class:- What fields do ALL FOUR of the classes have in common?
- Which constructors are the same in ALL FOUR classes?
- Which custom methods are identical in ALL of the classes?
- In
JobField
, declare each of the common class members. - Code the constructors.
- Add in any inherited method overrides.
- Finally, to prevent the creation of a
JobField
object, make this class abstract.
Extend JobField into Employer
Now that you have the common code located in the JobField file, we can modify the other classes to reference this shared code. Letâs begin with Employer.
- Modify line 4 to extend the
JobField
class intoEmployer
. - Next, remove any code in
Employer
that matches code from JobField (e.g. theId
andValue
properties and thenextId
field are shared). - Remove any of the methods that are identical.
- The empty constructor is shared, but not the second. Replace the two constructors with the following:
4 5 6 7 8 9
public class Employer : JobField { public Employer(string value) : base(value) { } }
- Rerun your unit tests to verify your refactored code.
Finish DRYing Your Code
Repeat the process above for the Location
, CoreCompetency
, and PositionType
classes.
Rerun your unit tests to verify that your classes and methods still work.
Run TestTas6 tests
Uncomment the tests inside the TestTask6
class. Look for the TODO
s to help you find the multi-line comments marks.
Run your TestTask6
unit tests.
Refactor your code as needed. This is the last set of tests! Congrats!
Submit your assignment once you have passed all of Task 6âs auto-graded unit tests.
Now would be a good time to save, commit
, and push
your work up to GitHub.
On to your final steps .