Scenario
I’m building a system where each item gets reviewed by 2 different people. Whenever the first reviewer saves an item review it’s then turn for the second checker to complete their individual review. If I submitted the first review and I open the item again then the item is put into a read only state because you can’t review your own work. Also, the first reviewer can put the item into a pending state if more information is required, which the second review can’t. Each user is given a specific reviewer roll whenever they select an item from a list.
What I have so far
Each time an item is loaded into the editor the person is given one of 2 roles, InitialReviewer or SecondReviewer.
public class Reviewer
{
public void AddReview(string review) { }
}
public class InitialReviewer : Reviewer, ICanPutIntoPendingState
{
public void PutIntoPendingState(string pendingState) { }
}
public class SecondReviewer : Reviewer
{
// Just use base class to add review
}
public class ReviewerService
{
private readonly Reviewer _reviewer;
public void AddReview(string review)
{
_reviewer.AddReview(review);
}
public void PutIntoPendingState(string pendingState)
{
_reviewer.PutIntoPendingState(pendingState);
}
}
A trimmed down version of my editor.
public class Editor
{
private readonly ReviewerService _reviewerService;
public Editor(ReviewerService reviewerService)
{
_reviewerService = reviewerService;
}
public void SaveCommand()
{
if(user chose a pending state && _reviewerService.Reviewer is ICanPutIntoPendingState) // Pending state is a dropdown.
_reviewerService.PutIntoPendingState("pending state");
else // the user made a complete review
_reviewerService.AddReview("user review");
}
}
Problem
The issue I’m having is that I can’t seem to escape from having logic inside Save() from the Editor class that doesn’t belong there.
Question
How do I rid myself of the logic inside of the Save() function from the Editor class? It appears to be violating the SRP principle. Checking whether the current reviewer object is of type ICanPutIntoPendingState is the big problem, I think.
Note that I’ve ommited all logic because there’s quite a bit.
Woudn’t be enough to give to ReviewerService one Save() method, which internaly call one Save() method of, at this point, abstract class Reviewer, whom abstact Save() method’s concrete implementation is made in InitialReviewer and SecondReviewer. So you push decisional logic to classes with concrete implementation.
Hope this helps.