Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In a project I have following class relationship. `Employee` and `Client` have a composition relationship with `Company`. So implemented this as follows.


C#
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
    {
    }
}



When I want to show `client` / `employee` details on the UI my `DataService` is supposed to return a `Company` object rather than returning `Employee/Client` objects as the relationship is composition. So I could have a method like `GetDetails()` in my `DataService` and then get the required details from the database to assign for properties of `Employee` and `Client`. But now the problem is, I will not be able to access private fields (`_Employee` , `_Client`) of `Company` object to set values for the properties as follows


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

Though I have few ideas to sort out this problem but non of them are seems adaptable to this class relationship (composition) or either violating separation of concerns principle. Appreciate your help in this regard?
Posted
Updated 25-May-14 5:37am
v2

1 solution

Make your property public not private
C#
public Client _Client {get;set;}


or add addtional property With public access
 
Share this answer
 
Comments
Chathur 25-May-14 12:51pm    
Usually it is said that in composition containing objects should be private!
NewPast 30-May-14 15:22pm    
You could make private set and public get.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900