Recently i did some refactoring on the old code. The code works fine as expected. Except that it was hard to test since there were too many dependencies on the implementation.
Here what it look like:
public static void Accept(Task task)
{
task.ActualStartDate = CGDateTime.Now;
task.Completed = false;
if (task.ResponsibleResource.ResourceType == ResourceType.Group && task.GetSetting(RuleResource.Rules.Personalize_On_Acceptance))
task.ResponsibleResource = AccessFactory.CurrentUser;
task.Save();
Comment acceptance = new Comment();
acceptance.CreatedBy = acceptance.ModifiedBy = AccessFactory.CurrentUser;
acceptance.CreatedDate = acceptance.ModifiedDate = CGDateTime.Now;
acceptance.Type = CommentType.Acceptance;
acceptance.Resource = task;
acceptance.Save();
UpdateAcceptedChildren(task, acceptance);
TaskInformer.RegisterForCatTask(task, ApplicationVirtualPath, TaskInformer.Action.Accept);
}
I am not going to explain more about that implementation and why and how i did the refactoring since it belongs to another topic.
In general, after creating some interface i had this one:
public static void Accept(Task task)
{
// #1 – The implementation
task.ActualStartDate = CGDateTime.Now;
task.Completed = false;
if (task.ResponsibleResource.ResourceType == ResourceType.Group &&
task.GetSetting(RuleResource.Rules.Personalize_On_Acceptance))
task.ResponsibleResource = AccessFactory.CurrentUser;
task.Save();
// #2 – The abstraction
Comment comment= _commentcreator.CreateAcceptedComment(task);
_childenupdator.UpdateAcceptedChildren(task, comment);
_informer.RegisterForCatTask(task,ApplicationVirtualPath, TaskInformer.Action.Accept);
}
_commentcreator is interface responsible for creating comment based on task
_chidrenupdator is interface responsible for updating its children information
_informer is interface responsible for informing others in the system.
It seems that we’ve alread done or at least at that time i can easily test the function without caring too much on the other parts. However, if you look at the code again, which i have marked with #1 and #2.
WE ARE MIXING THE IMPLEMENTATION AND ABSTRACTION IN ONE FUNCTION.
It should be in the same level.
How to solve the problem? Simply just abstract the implementation part. Here comes with the final code:
public static void Accept(Task task)
{
_taskinfor.UpdateTaskInfoOnAcceptance(task);
Comment comment= _commentcreator.CreateAcceptedComment(task);
_childenupdator.UpdateAcceptedChildren(task, comment);
_informer.RegisterForCatTask(task,ApplicationVirtualPath, TaskInformer.Action.Accept);
}
The benefit of doing this is obvious.
Like this:
Like Loading...