In a project I have following class relationship. Employee
and Client
have a composition relationship with Company
. So implemented this as follows.
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.
.
.
.
}
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?