This is one of a classes (that sends email based on object type to AM (role)) that is executed by an engine. I also have commands like SendEmailToAOCommand
that send email to a role AO based on object type and so one.
namespace SFE.Workflow.Commands
{
public class SendEmailToAMCommand : ActionCommand
{
private readonly Func<IApplicationRepository> applicationRepository;
private readonly IClientRepository clientRepository;
private readonly Func<IEmailService> emailService;
private readonly Func<IUserRepository> userRepository;
public SendEmailToAMCommand(
Func<IEmailService> _emailService,
Func<IUserRepository> _userRepository,
Func<IApplicationRepository> _applicationRepository,
IClientRepository _clientRepository)
{
emailService = _emailService;
CommandFor = Modules.All;
userRepository = _userRepository;
applicationRepository = _applicationRepository;
clientRepository = _clientRepository;
}
public override string CommandDescription
{
get { return "Sends email to AM"; }
}
public override void Body(object _obj, object _objInPreviousState)
{
if (_obj == null)
throw new ArgumentNullException("first param is null");
string message = string.Empty;
string subject = string.Empty;
if (_objInPreviousState == null)
{
var emailParams = Param as Dictionary<string, string>;
if (emailParams != null)
{
message = emailParams["Message"];
subject = emailParams["Subject"];
}
}
if (_obj is Application)
{
var app = (Application)_obj;
var email = userRepository().GetManagerForUser(app.UserID).Email;
Client borrower = clientRepository.GetMainBorrower(app.ID);
if (_objInPreviousState == null)
{
emailService().SendEmail("", email, message, subject);
}
else
{
emailService().SendEmail("", email,"Application: " + borrower.CompanyName + " (" + app.ID +") changed decision status: " +Enum.GetName(typeof(AppStatus), app.ApplicationStatus),"Check following application: " + app.ID);
}
}
else if (_obj is Product)
{
var product = (Product)_obj;
var email = userRepository().GetManagerForUser(product.Application.UserID).Email;
Client borrower = clientRepository.GetMainBorrower(product.ApplicationID);
if (_objInPreviousState == null)
{
emailService().SendEmail("", email, message, subject);
}
else
{
emailService().SendEmail("", email,"Product: " + product.ID + " for application " + borrower.CompanyName +" (" + product.ApplicationID + ") changed decision status: " +Enum.GetName(typeof(AppStatus), product.ProductStatusType),"Check following application: " + product.ApplicationID);
}
}
else if (_obj is CES)
{
var ces = (CES)_obj;
User user = applicationRepository().GetByID(ces.ApplicationID).User;
var email = userRepository().GetManagerForUser(user.UserName).Email;
Client borrower = clientRepository.GetMainBorrower(ces.ApplicationID);
if (_objInPreviousState == null)
{
emailService().SendEmail("", email, message, subject);
}
else
{
emailService().SendEmail("", user.Email,"CES for application " + borrower.CompanyName + " (" +ces.ApplicationID + ") changed decision status: " +Enum.GetName(typeof(CesStatuses), ces.Status),"Check following application: " + ces.ApplicationID);
}
}
else if (_obj is Comment)
{
var comment = (Comment)_obj;
Client borrower = clientRepository.GetMainBorrower(comment.ApplicationID);
emailService().SendEmail("", comment.User.Email,"Comment for the following application: " + borrower.CompanyName + " (" +comment.ApplicationID + ") with message: " + comment.Message + " on date: " +comment.CreatedDate,"Comment for the following application: " + comment.ApplicationID);
}
else if (_obj is Memo)
{
var memo = (Memo)_obj;
var email = userRepository().GetManagerForUser(memo.UserID).Email;
Client borrower = clientRepository.GetMainBorrower(memo.ApplicationID);
if (_objInPreviousState == null)
{
emailService().SendEmail("", email, message, subject);
}
else
{
emailService().SendEmail("", email, "Memo for application : " + borrower.CompanyName + " (" + memo.ApplicationID + ") changed decision status: " + Enum.GetName(typeof(AppStatus), memo.Status), "Check following memo for application: " + memo.ApplicationID);
}
}
Logger.InfoLine("Sending Email done" + " @" + CommandName);
}
}
}
The problem: this pattern is used in all of other commands. How can I improve this so this will not have same code in over and over again?