Let's say I want to store date which cannot be changed by the user in code. For example, my application supports ten types of reports, and I want to store metadata about it. Usually, I'd use a constant dictionary for that:
Private Shared ReadOnly ReportConfigurations As New Dictionary(Of Reports, ReportMetadata)() From
{
{Reports.Foo, New ReportMetadata("Foo Report", PaperSize.A4, 8, 12, ...)},
{Reports.Bar, New ReportMetadata("Bar Report", PaperSize.A4, 8, 14, ...)},
...
}
(Obviously, I'd use Const
if .NET supported constant dictionary fields and I'd use ImmutableDictionary if I weren't stuck with an older version of the framework.)
This works fine, but it gets hard to read quickly once the number of metadata fields increases (you can't see what 8 and 12 refer to unless you add named parameters, and the fields are nicely aligned only as long as you call your reports "Foo" and "Bar", which I don't). An option would be to use inline XML instead and parse it (which is quite easy if your language supports it):
Private Shared ReadOnly ReportConfigurations As XElement =
<Reports>
<Report Name="Foo Report" PaperSize="A4"
FontSizePx="8" BarcodeWidthCm="12"
... />
...
</Reports>
but I miss the tabular structure. Ideally, I'd have something like this
ReportName | PaperSize | FontSizePx | BarcodeWidthCm | ...
---------------------------------------------------------------
Foo Report | A4 | 8 | 12 | ...
Bar Report | A4 | 8 | 14 | ...
...
but adding an Excel sheet as a resource to my project and parsing it at run time seems like terrible overhead, just for a bit of increased readability.
Is there some elegant solution that I've missed?
Related question:
- Storing data in code (This is about two-field data, for which the Dictionary solution or a resource file would work fine.)