The purpose of the below code is to return different types of job objects based on an argument named "jobType."
The original code for the below methods was implemented as a switch statement. I refactored the code removing the switch statement, and in place creating different classes representing different types of jobs.
I would like feedback on my below implementation. The code works, and is released in production, but perhaps there is a better approach.
public bool RunNextJob()
{
// Return true if we found a job to run
DeferredJob dJob = _jobRepository.GetNextJob();
SessionProvider sessionProvider = ObjectFactory.GetInstance<SessionProvider>();
if(dJob != null)
{
Logger.Info(string.Format("Running job type: {0} Id: {1}", dJob.JobType, dJob.Id));
dJob.ProcessedDate = DateTime.Now;
_jobRepository.Update(dJob);
sessionProvider.Flush();
var jobType = new JobType();
var job = jobType.GetJobType(dJob.JobType);
if (job != null)
job.RunJob(dJob);
}
public class JobType : IJobType
{
public IJob GetJobType(string jobTypeCode)
{
if(jobTypeCode == JobTypeToProcess.Notify.ToString())
return new NotifyJob();
if (jobTypeCode == JobTypeToProcess.ConflictedAppointmentMessageJobType.ToString())
return new ConflictedAppointmentMessageJob();
if (jobTypeCode == JobTypeToProcess.UnifiedExport.ToString())
return new UnifiedExportReportJob();
return jobTypeCode == JobTypeToProcess.FTPJobType.ToString() ? new FTPJob() : null;
}
}