I recently discover by reading some blog topics on web that 90% of code lines I produced in my career was procedural and not OOP oriented (big slap in the head). So now, I try to lobotomy my brain in order to produce only OOP codes. The problem is that I have a lot of questions about OOP practice.
Here is one :
I got a factory RawLineCollectionFactory
that take string lines and create from it a domain entity called RawLineCollection
. My question is about argument type pass to the Create
method.
Should I pass it the object (IDataSource
) that allows me to retrieve the string lines like that :
public class RawLineCollectionFactory : IRawLineCollectionFactory
{
public IRawLineCollection Create(IDataSource dataSource)
{
ICollection<RawLine> lineGroup = new List<RawLine>();
if (dataSource.GetLines() != null)
{
foreach (string line in dataSource.GetLines())
{
lineGroup.Add(new RawLine(line));
}
}
return new RawLineCollection(lineGroup);
}
}
Or call getLines()
of the dataSource
object on higher abstraction level and pass the result as argument of Create
method ?
public class RawLineCollectionFactory : IRawLineCollectionFactory
{
public IRawLineCollection Create(IEnumerable<string> lines)
{
ICollection<RawLine> lineGroup = new List<RawLine>();
if (lines != null)
{
foreach (string line in lines)
{
lineGroup.Add(new RawLine(line));
}
}
return new RawLineCollection(lineGroup);
}
}
Edit :
The context is very close to the application entry point. RawLine is an object representation of a line in a file that contain data settings for a game creation.
public class RawLine
{
private string value;
public RawLine(string value)
{
this.value = value;
}
public IEnumerable<string> Split(string separator)
{
if (string.IsNullOrEmpty(separator))
{
throw new ArgumentException("The separator is required");
}
if (!string.IsNullOrEmpty(value))
{
return value
.Replace(" ", "", StringComparison.CurrentCulture)
.Split(separator)
.Where(item => !string.IsNullOrEmpty(item));
}
return Array.Empty<string>();
}
}
The behavior of this class is to split an actual representation of his state for the application-specific rules purpose while RawLineCollection is a collection of this entity type.
public class RawLineCollection : IRawLineCollection
{
private readonly IEnumerable<RawLine> lines;
private readonly string separator = "-";
public RawLineCollection(IEnumerable<RawLine> lines)
{
this.lines = lines;
}
public SplittedLineCollection Split()
{
ICollection<IEnumerable<string>> result = new List<IEnumerable<string>>();
if (IsValidStringGroup(lines))
{
foreach (RawLine line in lines)
{
if (line.Split(separator).Any())
{
result.Add(line.Split(separator));
}
}
}
return new SplittedLineCollection(result);
}
private bool IsValidStringGroup(IEnumerable<RawLine> settingsToSplit)
{
return settingsToSplit != null && settingsToSplit.Any();
}
}
The RawLineCollectionFactory is used just one time like DataSource.GetLines() method.
RawLine
andRawLineCollection
classes as well as how the factory is used. We need to know what they are and why they exist to give you a good review. \$\endgroup\$SplittedLineCollection
is missing, as well as an example file that your code is trying to read. \$\endgroup\$