我需要帮助来重构这个代码示例的一些部分,从哪里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");
}
}发布于 2012-04-25 21:44:38
你可能应该使用一些接口,我没有给你完整的代码,而是一个可以遵循的模式。
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());
}发布于 2012-04-25 21:46:41
有两件事你可以很容易地改进:
首先,反转一些if以减少嵌套。特别是:
if (_obj != null) { ... the entire function ... }可以是
if (null == _obj) { return; }
... the rest ...另外,将每个if/else主体提取到单独的方法中(您可以简单地选择主体,然后从菜单中选择Refactor...Extract Method。
最后,您应该能够将所有这些方法概括为接受更多参数的单个方法。
发布于 2012-04-25 21:49:48
在这种情况下,您可以创建一个生产对象的工厂。
这就是我重构的方式:
SomethingFactory生成AbstractSomething派生类(ConcreteSomethingA、ConcreteSomethingB等)。工厂根据"_obj“和
public override void Body(object _obj, object _objInPreviousState)将在具体的类中实现,因此系统可以很容易地扩展。
https://stackoverflow.com/questions/10316943
复制相似问题