R
RichB
I am just trying to get to grips with C# and OOP, and one of the benefits
would seem to be code reuse. However I am not sure where to draw the line. I
have the following section of code:
if (ev.locationList != null)
{
//isListNull = true ensures that we do not recheck the list
every time we add a new item
bool isListNull = false;
if (this.locationList == null)
{
this.locationList = new List<EventLocation>();
isListNull = true;
}
foreach (LocationInfoDTO loList in ev.locationList)
{
EventLocation loc = null;
if (!isListNull)
{
//listMatch is used by the Find Predicate to
match to objects already within the LocationList
listMatch = loList;
loc =
this.locationList.Find(EventLocationListMatch);
}
if (loc == null)
{
this.LocationList.Add(EventLocation.Create(loList));
}
else
{
loc.Update(loList);
}
}
}
Which is used in my constructor to take the inputs in the form of a DTO (ev)
and translates them into an list object within a business object. I find
that in another class I have a need to populate another list of a different
type from a different type of DTO. Apart from these being different types
the method of populating the list in the other class is identical and
therefore I would live to reuse the code, rather than cutting and pasting. I
therefore have 2 questions:
1. Are my expectations correct, should I be able to create a generic helper
method that does the tasks needed? i.e. :
a.. if the list does not already exist then create it.
b. iterate through each list item in the dto.
c. if the list already existed, then check if the item being added already
exists and update rather than create it
d. else add a new item to the list.
2. If I should be able to produce a generic method, then where should I be
looking for inspiration?
Thanks, Richard
would seem to be code reuse. However I am not sure where to draw the line. I
have the following section of code:
if (ev.locationList != null)
{
//isListNull = true ensures that we do not recheck the list
every time we add a new item
bool isListNull = false;
if (this.locationList == null)
{
this.locationList = new List<EventLocation>();
isListNull = true;
}
foreach (LocationInfoDTO loList in ev.locationList)
{
EventLocation loc = null;
if (!isListNull)
{
//listMatch is used by the Find Predicate to
match to objects already within the LocationList
listMatch = loList;
loc =
this.locationList.Find(EventLocationListMatch);
}
if (loc == null)
{
this.LocationList.Add(EventLocation.Create(loList));
}
else
{
loc.Update(loList);
}
}
}
Which is used in my constructor to take the inputs in the form of a DTO (ev)
and translates them into an list object within a business object. I find
that in another class I have a need to populate another list of a different
type from a different type of DTO. Apart from these being different types
the method of populating the list in the other class is identical and
therefore I would live to reuse the code, rather than cutting and pasting. I
therefore have 2 questions:
1. Are my expectations correct, should I be able to create a generic helper
method that does the tasks needed? i.e. :
a.. if the list does not already exist then create it.
b. iterate through each list item in the dto.
c. if the list already existed, then check if the item being added already
exists and update rather than create it
d. else add a new item to the list.
2. If I should be able to produce a generic method, then where should I be
looking for inspiration?
Thanks, Richard