我需要帮助从if (_obj is Application)
重构此代码示例的某些部分,因此这将是通用的。
public override void Body(object _obj, object _objInPreviousState)
{
if (_obj != null)
{
string Message = "";
string Subject = "";
if (_objInPreviousState == null)
{
var emailParams = this.Param as Dictionary<string, string>;
if (emailParams != null)
{
Message = emailParams["Message"];
Subject = emailParams["Subject"];
}
}
var emails = userRepository().GetForRoles("RM").Select(c => c.Email);
if (_obj is Application)
{
var app = (Application)_obj;
var appInPreviousState = _objInPreviousState as Application;
if (appInPreviousState == null)
{
emailService().SendEmails("aps@somedomain.com", emails.ToArray(), Message, Subject);
}
else if (app.ApplicationStatus != appInPreviousState.ApplicationStatus)
{
emailService().SendEmails("aps@somedomain.com", emails.ToArray(), "Application: " + 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 prodInPreviousState = _objInPreviousState as Product;
if (prodInPreviousState == null)
{
emailService().SendEmails("aps@somedomain.com", emails.ToArray(), Message, Subject);
}
else if (product.ProductStatusType != prodInPreviousState.ProductStatusType)
{
emailService().SendEmails("aps@somedomain.com", emails.ToArray(), "Product: " + product.ID + " for application " + 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;
var cesInPreviousState = _objInPreviousState as CES;
if (cesInPreviousState == null)
{
emailService().SendEmails("aps@somedomain.com", emails.ToArray(), Message, Subject);
}
else if (ces.Status != cesInPreviousState.Status)
{
emailService().SendEmails("aps@somedomain.com", emails.ToArray(), "CES for application " + 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;
emailService().SendEmails("aps@somedomain.com", emails.ToArray(), "Comment for the following application: " + comment.ApplicationID + " with message: " + comment.Message + " on date: " + comment.CreatedDate, "Comment for the following application: " + comment.ApplicationID);
}
mLog.InfoLine("Sendet Email");
}
}
答案 0 :(得分:4)
你应该使用一些接口,我没有给你完整的代码,但是要遵循一个模式。
interface IStatusItem
{
void SendEmails(EmailService service);
}
public class Product : IStatusItem
{
public void SendEmails(EmailService service)
{
// Send Email
}
}
public class Application : IStatusItem
{
public void SendEmails(EmailService service)
{
// Send Email
}
}
然后你的主代码不需要所有的if块。它只是调用IStatusItem实例上的实现。显然你需要在那里添加以前的状态。
override void Body(object _obj, object _objInPreviousState)
{
IStatusItem sItem = obj as IStatusItem;
if(sItem != null)
sItem.SendEmails(emailService());
}
答案 1 :(得分:1)
您可以轻松改进的两件事:
首先,反转一些if
以减少嵌套。特别是:
if (_obj != null) { ... the entire function ... }
可以
if (null == _obj) { return; }
... the rest ...
此外,将每个if / else主体提取到单独的方法中(您只需选择主体并从菜单中选择Refactor ... Extract Method。
最后,您应该能够将所有这些方法概括为一个只需要更多参数的方法。
答案 2 :(得分:1)
在这种情况下,您可以建立一个生产对象的工厂。
这就是我重构的方式:
SomethingFactory生成AbstractSomething派生类(ConcreteSomethingA,ConcreteSomethingB等)。工厂生产ConcreteSomethings取决于&#34; _obj&#34;和
public override void Body(object _obj, object _objInPreviousState)
将在具体类中实现,因此系统可以轻松扩展
答案 3 :(得分:1)
if ( _obj == null ) return;
""
声明替换为string.Empty
创建项目的界面
public interface IEmail{
string GetMessage();
string GetSubject();
}
创建工厂以生成IEmail
个实例
通过一次电话发送电子邮件
public void Body(object obj, object objInPreviousState)
{
const string Address= "aps@somedomain.com"; //extract to configuration
IEmail item = GetEmailItem(_obj, _objInPreviousState);
if(item != null) emailService().SendEmails( Address, emails.ToArray(), item.GetMessage(), item.GetSubject() );
}