0

我需要覆盖 List 类的属性。例如,我的课程设置如下:

    public class Customer
    {
        private int _ID;
        private string _CustomerName;
        private List<CustomerAddress> _CustomerAddressList;

        public int ID { get { return _ID; } set { _ID = value; } }
        public string CustomerName
        {
            get { return _CustomerName; }
            set { _CustomerName = value; }
        }
        public List<CustomerAddress> CustomerAddressList
        {
            get { return _CustomerAddressList; }
            set { _CustomerAddressList = value; }
        }
    }

    public class CustomerAddress
    {
        private string _Address1;
        private string _TelephoneNumber;

        public string Address1
        {
            get { return _Address1; }
            set { _Address1 = value; }
        }

        public virtual string TelephoneNumber
        {
            get { return _TelephoneNumber; }
            set { _TelephoneNumber = value; }
        }
    }

现在我有我的Business Layer类,它继承了客户类。

我可以覆盖客户类的属性,但我不知道如何CustomerAddress在我的类中覆盖类的属性CustomerBL?我不想覆盖列表设置,而是覆盖列表中每个项目的各个属性。

4

3 回答 3

0

我认为您不应该从其他层的实体继承您的业务实体。改用映射(手动或使用Automapper之类的工具)。

于 2012-04-19T10:04:46.630 回答
0

即使不是很清楚你想要达到什么目的,但考虑到你的评论:

所以我会创建另一个类 public class Worker : Customer { public override CustomerAddress.TelephoneNumber {get; 放; 尝试像那样覆盖它是行不通的

我会这样做:

public class Customer 
{
    protected string telNumber =string.Empty; 
    public virtual string TelephoneNumber 
    {
      get { return telNumber ; }
      set {telNumber =value;}
    }
}

public class Worker : Customer 
{
   public override string TelephoneNumber 
   {
       get 
       {
          Console.WriteLine("Worker");
          return telNumber ; 
       }
       set {telNumber = value;}
   }
}

之后,如果您在代码中使用它,如下所示:

Customer curst = new Worker(); 
var telNumber = curst.TelephoneNumber ;

它会产生这样的输出:

Worker

如果这不是您要的,请澄清。

于 2012-04-19T10:19:47.423 回答
0

根据最初的设计,只有糟糕的解决方案:

    public class Customer
    {
        protected List<CustomerAddress> _customerAddresList 
           = new List<CustomerAddress>();
    }

    public class Worker : Customer
    {

    }

    public class CustomerAddress
    {
        protected string _Address1;
        public virtual string Address1
        {
            get { return "customer address: " + _Address1; }
            set { _Address1 = value; }
        }
    }

    public class WorkerAddress: CustomerAddress
    {
        public override string Address1
        {
            get { return "Worker Address: " + _Address1; }
        }
    }

如果实体实例是继承的,我们有:

    // Inheriting instances

    public class CustomerBL : Customer
    {
        public void AddAdress(CustomerAddress address)
        {
            _customerAddresList.Add(address);
        }
    }

    public class WorkerBL: Worker
    {
        // Not inehritable, different signature
        public void AddAdress(WorkerAddress address)
        {
            _customerAddresList.Add(address);
        }
    }

如果我们继承 BL 类,我们有:

    // Inheriting BL
    public class CustomerBL2 : Customer
    {
        public virtual void AddAdress(CustomerAddress address)
        {
            _customerAddresList.Add(address);
        }
    }

    public class WorkerBL2 : CustomerBL2
    {
        public override void AddAdress(CustomerAddress address)
        {
            if (!(address is WorkerAddress))
                throw new Exception();
            base.AddAdress(address);
        }
    }

如果我们使用泛型,我们有这个

    // Using generics
    public class Generic<TAddress>
    {
        private List<TAddress> _addresList
            = new List<TAddress>();
        protected virtual List<TAddress> AddresList
        {
            get { return _addresList; }
        }
    }

    public class CustomerG : Generic<CustomerAddress>
    {
    }

    public class WorkerG : Generic<WorkerAddress>
    {

    }

毫无疑问,问题在于有两种不同的继承链:

  • 实体之一
  • 另一个 BL 课程

并且不可能对哪个是正确的做法做出正确的决定(事实上,我认为,使用这种设计,没有办法做到正确)。

让实体和 BL 类相互独立要好得多,这样您就可以保持两个继承链独立,并决定在其链中可以继承和应该继承什么。

    public class Address
    {
    }

实体可以相互继承。好吧,典型案例:

    public class Person
    {
        public List<Address> Adresses;
    }

    public class Worker: Person
    {
        // inherits adresses
    }

继承 BL 类(不喜欢它):

    public class PersonBl
    {
        // Functionality which is common fot all the inheritance chain
        public void PrintAdresses(Person person)
        {
        }

        // Functionality that can be specialized for each inherited entity
        public virtual void SaveAdresses(Person person)
        {
            // they're are treated differently in each case
        }

        // Functionality specific of person
        public void DoSomethingWithPerson(Person person)
        {
            // TODO
        }
    }

    public class WorkerBl : PersonBl
    {
        // Uses PersonBl PrintAdresses

        public override void SaveAdresses(Person person)
        {
            // do it for worker
        }

        // Functionality specific of Worker
        public void DoSomethingWithWorker(Worker worker)
        {
            // TODO
        }
    }

“使用其他 BL 类的 BL 类”(这就是我喜欢的方式):

    public class Person2Bl
    {
        // Functionality which is common for all the inheritance chain of entities
        public void PrintAdresses(Person person)
        {
        }

        public void SaveAdresses(Person person)
        {
           // specific for person
        }

        // Functionality specific of person
        public void DoSomethingWithPerson(Person person)
        {
        }
    }


    // doesn't inherit:

    public class Worker2Bl
    {
        // Use the logic in PersonBl2
        public void PrintAdresses(Worker worker)
        {
            // Really not necessary -> this could be done directly in the app code
            Person2Bl bl = new Person2Bl();
            bl.PrintAdresses(worker);
        }

        public void SaveAdresses(Worker worker)
        {
           // specific of Worker
        }

        public void DoSomethingWithWorker(Worker worker)
        {
            // specific of worker
        }
    }

实际上,最好有一个 WorkerBl2 和 PersonBl2 都可以使用的 AddressBl

于 2012-04-19T11:43:41.200 回答