1

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

我们有 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;
        }

    }

}
4

2 回答 2

1

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

每个构造函数首先调用它的基类构造函数,因此您不必重复代码。

  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();
      }
    }
  }
于 2013-03-12T14:26:28.293 回答
0

为什么不这样:

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;
    }
于 2013-03-12T14:11:19.577 回答