我正在尝试重构一些旧代码并希望创建更多逻辑继承。
我们有 struct Custom Class,我们将其分为 (3) 个级别:
AccountView > 详细信息 > 完整的继承。我们根据需要设置每个属性。
在查看了设置器之后,我们想将它们组合成一个带有设置属性的方法的类“SetAccountProp”。
我们有“CustomerBaseView”,我们在其中传递有效的模型账户数据。
现在为 CustomerDetailView 传递相同的模型 ACCOUNT 数据,但我们想使用函数“CustomerBaseView”填充“CustomerBaseView”的属性,然后填写详细信息。
此外,对于 CustomerFullView,传递模型帐户数据,然后填写“CustomerBaseView”的属性,然后填写“CustomerBaseView”,然后填写 CustomerFullView 的其余字段。
如何在“CustomerDetailView”函数中调用和填写“CustomerBaseView”?我是否初始化 new AccountsView(); 在每个功能中?
不确定如何在不重复的情况下完成重构:
// -- CustomView <--- replace with func?
view.Email = data.Email;
view.Active = data.Active;
view.FirstName = data.FirstName;
view.LastName = data.LastName;
在详细信息和完整功能中。
代码
namespace BLL.Presenters
{
public class AccountsView
{
public string Email { get; set; }
public bool Active { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Details : AccountsView
{
public bool Administrator { get; set; }
public DateTime? LastLogin { get; set; }
}
public class Full : Details
{
public Guid ID { get; set; }
public DateTime Created { get; set; }
public DateTime Modified { get; set; }
public string FullName { get; set; }
}
public class SetAccountProp
{
public static AccountsView CustomerBaseView(Account data)
{
var view = new AccountsView();
view.Email = data.Email;
view.Active = data.Active;
view.FirstName = data.FirstName;
view.LastName = data.LastName;
return view;
}
public static Details CustomerDetailView(Account data)
{
var view = new Details();
// -- CustomView <--- replace with func?
view.Email = data.Email;
view.Active = data.Active;
view.FirstName = data.FirstName;
view.LastName = data.LastName;
// -- Details
view.Administrator = data.Administrator;
view.LastLogin = data.LastLogin;
return view;
}
public static Full CustomerFullView(Account data)
{
var view = new Full();
// -- CustomView <--- replace with func?
view.Email = data.Email;
view.Active = data.Active;
view.FirstName = data.FirstName;
view.LastName = data.LastName;
// -- Details <--- replace with func?
view.Administrator = data.Administrator;
view.LastLogin = data.LastLogin;
// -- Full
view.ID = data.ID;
view.Created = data.Created;
view.Modified = data.Modified;
view.FullName = data.LastName + ", " + data.FirstName;
return view;
}
}
}