First let me explain how I am currently processing validation, for example for an IPv4 address:
public struct IPv4Address { private string value; private IPv4Address(string value) { this.value = value; } private static IPv4Address CheckSyntax(string value) {
I have a bunch of structures like this.
They often have additional private members to handle things, but no methods are publicly disclosed.
Here is a usage example:
IPv4Address a; IPv4Address b = "1.2.3.4"; a = b; b = "5.6.7.8"; string address = a; // c contains "1.2.3.4" IPv4Address c = address; // See if it valid try { IPv4Address d = "111.222.333.444"; } catch (ApplicationException e) { // Handle the exception... }
I feel like something is really bothering there, so I'm thinking about switching to a static class using methods like IsIPv4Address, etc.
Now, I think itโs wrong with the above approach:
New team members will have to wrap their heads around this
This may interfere with third-party code integration.
Exceptions are expensive
I have never seen anything like it, and I am of a conservative nature :)
And then I like:
Very close to having many specialized primitives since you have value types.
In practice, they can often be used as primitive types, for example, this is not a problem by passing the above structure to a method that accepts a string.
And, my love, you can pass these structures between objects and make sure they contain a syntactically valid value. It also avoids the need to always verify the correctness, which can be costly if done unnecessarily and even forgotten.
I cannot find the fatal flaw of the approach above (just a beginner here), what do you think?
Edit: since you can infer from the first line, this is just an example, I am not asking for a method of checking the IP address.
source share