我有几个类,其中一些是抽象的,我希望方法的基类版本在同一个类中调用另一个方法的派生版本最多,然后在链上工作并执行'完成'方法的工作。我有基础和派生方法的原因是因为不同的级别具有不同的信息访问权限。如果'Order'对象为null,我不想在尝试获取信息之前测试null。结果是一系列“级联”类,每个派生版本的方法调用基本方法,然后构建基本方法为实现其目标所做的工作。
public abstract class EmailTemplates
{
....
protected virtual string ReplaceVariables(KeyValuePair<string, string> namevalue, string body)
{
// This builds an email body
protected void BuildBody()
{
if(NamesValues != null)
{
foreach (KeyValuePair<string, string> namevalue in NamesValues)
{
// This gives back eg "order details"
string subsectionName;
// Test if this value is a subsection
// If subsection not in the list, keep unchanged
if (Subsections.TryGetValue(namevalue.Key, out subsectionName))
{
// This retrieves the subsection details
this.emailBody = this.emailBody.Replace(namevalue.Value, GetSubsection(subsectionName, namevalue.Value));
}
// This is a regular variable not a subsection
else
{
this.emailBody = ReplaceVariables(namevalue, this.emailBody);
}
}
}
}
}
protected virtual string ReplaceVariables(KeyValuePair<string, string> namevalue, string body)
{
switch(namevalue.Key)
{
case "url":
body = body.Replace(namevalue.Value, Url);
break;
case "username":
body = body.Replace(namevalue.Value, HttpContext.Current.User.Identity.Name);
break;
}
return body;
}
....
}
public abstract class CustomerEmailTemplates : EmailTemplates
{
...
protected new string ReplaceVariables(KeyValuePair<string, string> namevalue, string body)
{
body = base.ReplaceVariables(namevalue, body);
switch (namevalue.Key)
{
case "forename":
// If they don't have a profile, just use the username
if ((Profile != null) && (Profile.IsAnonymous || Profile.DeliveryAddress1.FirstName == null || Profile.DeliveryAddress1.FirstName == ""))
{
body = body.Replace(namevalue.Value, Username);
}
// If user is changing their password, etc.
else if (Profile != null)
{
body = body.Replace(namevalue.Value, Profile.DeliveryAddress1.FirstName);
}
// To display template to admin don't replace anything
break;
case "surname":
// If they don't have a profile, just use nothing as username will already be there
if ((Profile != null) && (Profile.IsAnonymous || Profile.DeliveryAddress1.LastName == null || Profile.DeliveryAddress1.LastName == ""))
{
body = body.Replace(namevalue.Value, "");
}
else if (Profile != null)
{
body = body.Replace(namevalue.Value, Profile.DeliveryAddress1.LastName);
}
// To display template to admin don't replace anything
break;
}
return body;
}
...
}
public class OrderEmailTemplates : CustomerEmailTemplates
{
...
protected new string ReplaceVariables(KeyValuePair<string, string> namevalue, string body)
{
body = base.ReplaceVariables(namevalue, body);
switch (namevalue.Key)
{
case "customerEmail":
body = body.Replace(namevalue.Value, Order.CustomerEmail);
break;
case "orderID":
body = body.Replace(namevalue.Value, Order.ID.ToString());
break;
....
}
...
}
很抱歉代码转储,但我不确定如何使它(更大)更小。我期待发生的是BuildEmailBody()
转到最后一个调用OrderEmailTemplates.ReplaceVariables()
的派生类并返回基类,但现在它只是调用EmailTemplates.ReplaceVariables()
而不是替换我想要的所有值。
答案 0 :(得分:2)
public abstract class EmailTemplates
{
protected void BuildBody()
{
ReplaceVariables();
}
protected virtual string ReplaceVariables()
{
//code
}
}
public abstract class CustomerEmailTemplates : EmailTemplates
{
protected override string ReplaceVariables()
{
//code
base.ReplaceVariables();
}
}
public class OrderEmailTemplates : CustomerEmailTemplates
{
protected override string ReplaceVariables()
{
//code
base.ReplaceVariables();
}
}