在基函数中设置属性,在子函数继承中调用函数

本文关键字:继承 函数 调用 子函数 属性 基函数 设置 | 更新日期: 2023-09-27 18:03:36

我正在尝试重构一些旧代码,并希望创建更多的逻辑继承。

我们有一个结构自定义类,我们已经分成(3)层:

AccountView> Details>完全继承。我们根据需要设置每个属性。

在查看了setter之后,我们想要将它们组合成一个类SetAccountProp,其中包含设置属性的方法。

我们有'CustomerBaseView',我们在模型帐户数据传递工作。

现在对于CustomerDetailView传递相同的模型帐户数据,但我们想填充'CustomerBaseView'的属性使用函数'CustomerBaseView'然后填充细节。

另外,对于CustomerFullView传递模型帐户数据,并填写'CustomerBaseView'然后'CustomerBaseView'的属性,然后为CustomerFullView的剩余字段。

我怎么能调用和填充'CustomerBaseView'内的'CustomerDetailView'功能?是否初始化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;
        }
    }
}

在基函数中设置属性,在子函数继承中调用函数

使用构造函数链,你可以得到这样的东西:

每个构造函数首先调用它的基类的构造函数,所以你不必重复代码。

  namespace BLL.Presenters
  {
    using System;
    public class Account // dummy to make it compile
    {
      public string Email;
      public bool Active;
      public string FirstName;
      public string LastName;
      public bool Administrator;
      public DateTime? LastLogin;
      public Guid ID;
      public DateTime Created;
      public DateTime Modified;
    }
    public class AccountsView
    {
      public string Email { get; set; }
      public bool Active { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public AccountsView(Account data)
      {
        this.Email = data.Email;
        this.Active = data.Active;
        this.FirstName = data.FirstName;
        this.LastName = data.LastName;
      }
    }
    public class Details : AccountsView
    {
      public bool Administrator { get; set; }
      public DateTime? LastLogin { get; set; }
      public Details(Account data) : base(data)
      {
        this.Administrator = data.Administrator;
        this.LastLogin = data.LastLogin;
      }
    }
    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 Full(Account data) : base(data)
      {
        this.ID = data.ID;
        this.Created = data.Created;
        this.Modified = data.Modified;
        this.FullName = data.LastName + ", " + data.FirstName;
      }
    }
    class Program
    {
      static void Main()
      {
        Console.WriteLine();
        Console.ReadLine();
      }
    }
  }

为什么不这样呢:

public class CustomerBase
{
    public string Email { get; private set; }
    public bool Active { get; private set; }
    public string FirstName { get; private set; }
    public string LastName { get; private set; }
    protected void SetAccountInfo(Account account)
    {
        this.Email = account.Email;
        this.Active = account.Active;
        this.FirstName = account.FirstName;
        this.LastName = account.LastName;
    }
}
public class CustomerA : CustomerBase
{
    public string IsAdmin { get; private set; }
    public DateTime? LastLogin { get; private set; }
    public void SetAccountInfo(Account account)
    {
        base.SetAccountInfo(account);
        this.IsAdmin = account.IsAdmin;
        this.LastLogin = account.LastLogin;
    }
}
 public class Account
{
     //your properties
    public string Email { get; set; }
    public bool Active { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string IsAdmin { get; set; }
    public DateTime? LastLogin { get; set; }
}

或者让SetAccountInfo()返回this

public CustomerA SetAccountInfo(Account account)
    {
        base.SetAccountInfo(account);
        this.IsAdmin = account.IsAdmin;
        this.LastLogin = account.LastLogin;
        return this;
    }