根据最初的设计,只有糟糕的解决方案:
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 类相互独立要好得多,这样您就可以保持两个继承链独立,并决定在其链中可以继承和应该继承什么。
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