Which do you like better? Class #1: public class SearchClass { public SearchClas
ID: 642314 • Letter: W
Question
Which do you like better? Class #1:
public class SearchClass
{
public SearchClass (string ProgramName)
{ /* Searches LocalFile objects, handles exceptions, and puts results into m_Results. */ }
DateTime TimeExecuted;
bool OperationSuccessful;
protected List<LocalFile> m_Results;
public ReadOnlyCollection<LocalFile> Results
{
get { return new ReadOnlyCollection<LocalFile>(m_Results); }
}
#region Results Filters
public DateTime OldestFileModified
{
get { /* Does what it says. */ }
}
public ReadOnlyCollection<LocalFile> ResultsWithoutProcessFiles()
{
return new ReadOnlyCollection<LocalFile> ((from x in m_Results
where x.FileTypeID != FileTypeIDs.ProcessFile
select x).ToList());
}
#endregion
}
Or class #2:
public class SearchClass
{
public SearchClass (string ProgramName)
{ /* Searches LocalFile objects, handles exceptions, and puts results into m_Results. */ }
DateTime TimeExecuted;
bool OperationSuccessful;
protected List<LocalFile> m_Results;
public ReadOnlyCollection<LocalFile> Results
{
get { return new ReadOnlyCollection<LocalFile>(m_Results); }
}
public class SearchResults : ReadOnlyCollection<LocalFile>
{
public SearchResults(IList<LocalFile> iList) : base(iList) { }
#region Results Filters
public DateTime OldestFileModified
{
get { /* Does what it says. */ }
}
public ReadOnlyCollection<LocalFile> ResultsWithoutProcessFiles()
{
return new ReadOnlyCollection<LocalFile> ((from x in this
where x.FileTypeID != FileTypeIDs.ProcessFile
select x).ToList());
}
#endregion
}
}
...with the implication that OperationSuccessful is accompanied by a number of more interesting properties on how the operation went, and OldestFileModified and ResultsWithoutProcessFiles() also have several more siblings in the Results Filters section.
Explanation / Answer
As always, these things are a matter of taste. In this case, I'm not crazy about the second solution for a few reasons:
There's always a small intellectual burden when defining classes and their relationships. It's not clear what benefit the nested class provides aside from partitioning code. The one class approach is simple and clear.
I'm not crazy about nested public classes. This implies I should be able to instantiate it outside of the SearchClass context which doesn't make a lot of sense.
Inheriting generic collections isn't my favorite. When I run into this class, it's not immediately clear that it's just a plain old ReadOnlyCollection<LocalFile>. I don't like the extra step of remembering what's behind the curtain.
Additionally, you may want some sort of static method to create your SearchClass. In your example, the class runs processing in the constructor which doesn't allow it to gracefully fail. The only option is to throw an exception. If you use a static method, you have the option of returning null on failure (or some other alternative failure scheme).
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.