覆盖List <t>属性</t>

时间:2012-04-19 09:54:59

标签: c# list override

我需要覆盖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类的属性?我不想覆盖列表设置,而是覆盖列表中每个项目的各个属性。

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

即使你想要实现的目标还不是很清楚,但考虑到你的意见:

  

然后我会创建另一个类公共类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

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

答案 2 :(得分: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会更好