【问题标题】:Class composition issue when data service returns objects数据服务返回对象时的类组合问题
【发布时间】:2014-07-14 10:22:54
【问题描述】:

在一个项目中,我有以下类关系。 EmployeeClientCompany 具有组合关系。所以实现如下。

class Company
{
    private Employee _Employee {get;set;} // private fields as composition
    private Client _Client {get;set;}

    public Company()
    {
        _Employee = new Employee();
        _Client = new Client();
    }

    public AddEmploees() //Employee objects are controlled by Company
    {
        //
    }

    public DeleteEmploees()
    {
        //
    }

    public AddClients() //Client objects are controlled by Company
    {
        //
    }

    public DeleteClients()
    {
        //
    }
}


class Employee
{
    string Name {get;set;} 
    int ID {get;set;}
    string Address {get;set;}
    string Department  {get;set;}
    DateTime DOB {get;set;}

    private Employee() // constructor private
    {
    }
}


class Client
{
    string CID {get;set;}
    string Name {get;set;}
    string Type {get;set;}
    DateTime StartDate {get;set;}
    string Address {get;set;}

    private Client() // constructor private
    {
    }    
}

当我想在 UI 上显示 client / employee 详细信息时,我的 DataService 应该返回 Company 对象,而不是返回 Employee/Client 对象,因为关系是组合。所以我可以在我的DataService 中有一个类似GetDetails() 的方法,然后从数据库中获取所需的详细信息以分配EmployeeClient 的属性。但现在的问题是,我将无法访问 Company 对象的私有字段(_Employee_Client)来设置属性值,如下所示

public Company GetDetails()
{
Company company = new Company();
string selectStatement = "SELECT...";
// Get data from DB
company.client.name = rdr["name"].value;  // This is not possible.
.
.
.
}

虽然我几乎没有解决这个问题的想法,但它们似乎都不适合这种类关系(组合)或违反关注点分离原则。感谢您在这方面的帮助?

【问题讨论】:

  • 为什么这不可能?
  • 因为包含的类 Company 的 Client 属性是私有的。
  • 要么创建一个公共属性来包装它public Client Client { get { return _client; } },要么你有一个公共方法来访问各个属性GetClientName/SetClientName。在我看来,SoC 与这种设计无关。您相当受 SOLID 原则的约束,这并不违反任何这些规则。

标签: c# .net uml mvp composition


【解决方案1】:

看起来你想使用

MSDN: Factory Design Pattern

为了实现相互可见性(又名 C++ friend class),您可以使用

internal visibility modifier

而不是私有可见性。这将允许 r/w 访问其他对象的属性。 Microsoft 也大量使用它(内部类、内部命名空间,参见例如 http://referencesource.microsoft.com/#PresentationFramework/src/Framework/MS/Internal/Data/CollectionViewGroupInternal.cs),因此只要有帮助,它就不是被禁止的设计实践

【讨论】:

    【解决方案2】:

    您可以通过从 Company 对象本身而不是从外部调用 GetDetails() 来解决您的问题。像这样的:

    class Company
    {
        // other class features
    
        public static getDetails(params) // params are there as a filter (if needed)
        {
            // connect to DB and get the data...
            //
            // here you can access private members of Company
    
            // return array of companies (or a single company)
    
        }
    }
    

    我发现这个解决方案在面向对象的意义上比你的要好得多。 Class Company 依靠自己通过静态方法获取数据。在获取实例之前,您不需要实例化公司。还有更多 - 私人数据受到很好的保护并且对外部世界不可见。在您的解决方案中,即使您可以从外部访问私有成员,也会破坏封装,这并不好。

    我还会重新考虑使用组合来存储客户和员工,因为他们与公司的自然关系较弱。如果一家公司“死亡”,这绝对不意味着其客户员工也会死亡。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多