HiveBrain v1.2.0
Get Started
← Back to all entries
debugcsharpMinor

Simple structure that would contain validation errors messages

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
simplecontainvalidationwouldthatstructuremessageserrors

Problem

Before I go too far, does this look right?

This is mostly to have a "central" place that hold a "hierarchical/structured" errors message.

```
public static class ValidationError
{
static ValidationError()
{
Prefix = "VE";
Notice = new _Notice();
Exporter = new _Exporter();
Importer = new _Importer();
Shipping = new _Shipping();
//few others one
}

public static string Prefix { get; private set; }

public static _Notice Notice { get; private set; }
public class _Notice
{
public _Notice()
{
Prefix = ValidationError.Prefix + "NO";
MustHaveOneTreatmentOption = Prefix + "001";
}

public string Prefix { get; private set; }
public string MustHaveOneTreatmentOption { get; private set; }
}

public static _Exporter Exporter { get; private set; }
public class _Exporter
{
public _Exporter()
{
Prefix = ValidationError.Prefix + "EX";
Mailing = new Address(Prefix, "MA");
Physical = new Address(Prefix, "PH");
}

public string Prefix { get; private set; }

public Address Mailing { get; private set; }
public Address Physical { get; private set; }
}

public static _Importer Importer { get; private set; }
public class _Importer
{
public _Importer()
{
Prefix = ValidationError.Prefix + "IM";
}

public string Prefix { get; private set; }
}

public static _Shipping Shipping { get; private set; }
public class _Shipping
{
public _Shipping()
{
Prefix = ValidationError.Prefix + "SH";
}

public string Prefix { get; private set; }
}

public class Address
{
public Address(string _Prefix, string _Suffix)
{
Prefix = _Prefix + _Suffix + "AD";
}

public string Prefix { get; private set;

Solution

Naming

Class names in C#, whether they're for a static, non-static, internal, public, private, abstract, nested or on-its-own class, should be PascalCase.

Find the intruder:

public class _Notice
public class _Exporter
public class _Importer
public class _Shipping
public class Address


Only Address is correct!

Keep the underscore prefix for private fields, it seems you're using prefixes only to avoid name clashes - this smells, because in naming what's important is consistency - using arbitrary prefixes is bad practice.

Also, it's possible the nested types are not warranted, but it's very hard to tell without seeing how you're using this ValidationError static class.

Code Snippets

public class _Notice
public class _Exporter
public class _Importer
public class _Shipping
public class Address

Context

StackExchange Code Review Q#45937, answer score: 7

Revisions (0)

No revisions yet.