How to deal with class composition when components cannot be accessed from the outside?
Posted
by
Chathuranga
on Stack Overflow
See other posts from Stack Overflow
or by Chathuranga
Published on 2014-05-22T19:00:21Z
Indexed on
2014/05/28
3:27 UTC
Read the original article
Hit count: 268
For example if I say I have three classes A
, B
, and C
where B
and C
have a composition relation ship with A
. That means the life of B
and C
is handled by A
, and also B and C cannot access directly from the outside.
For some reason my DataService
class needs to return objects of B
and C
as It cant return a object of A
as B
and C
cannot be initialized at the same time. (to be able to initializeC
you have to initializeB
first).
So that I'm returning DataTables
from DataService
and then inside the class A
those data tables are converted to B
/ C
objects.
If
B
andC
objects cannot be initialized at the same time is it valid to say thatB
andC
have a composition relationship withA
?If its composition is it must to generate
A
withB
andC
inside?What is the proper way to handle this sort of a problem?
EDIT:
Following code explains the way I'm doing it now with DataTables.
Example:
class A
{
private List<B> B;
private List <C> C;
public A()
{
B= new List<B>();
C= new List<C>();
}
public List<B> GetB( DataTable dt)
{
// Create a B list from dt
return B;
}
}
class Presenter
{
private void Show B()
{
_View.DataGrid = A.GetB(DataService.GetAListOfB());
}
}
The actual scenario is I have a class called WageInfo
and classes Earning
and Deduction
having a composition relationship in the design. But for you to generate Deductions
first you should Generate earnings
and should be saved in a table. Then only you can generate deductions
for the earnings
to calculate balance wages.
Also note that these contained classes have a one to many relationship with the containing class WageInfo. So actually WageInfo has a List<Earnings>
and List<Deduction>
My initial question was, is it ok if my DataService
class returns Deductions
/ Earnings
objects (actually lists) not a WageInfo
?
Still not clear?
© Stack Overflow or respective owner