I made a tiny wrapper for fluent precondition assertions in c#.
Now basically in all public / contract methods i assert the values this way:
Precondition
.For(()=>Model)
.NotNull();
Precondition
creates a ValidationRule
Instance on which I have the given validation methods.
Now this happens very often and I think that using a struct might be an advantage, also because that ValidationRule only has values, there will be no boxing at all, it is sealed by definition and it is immutable. Also they aren't used as parameters or event outside the scope of the calling methods.
But the contra is that it has multiple values and there for not that small footprint.
The ValidationRule looks basically like this:
public sealed class ValidationRule<T>
{
public T Value { get; }
public string Name { get; }
public string File { get; }
public string Source { get; }
public int Line { get; }
public ValidationRule(T value, string name, string file, string source, int line)
{
Value = value;
Name = name;
File = file;
Source = source;
Line = line;
}
public ValidationRule<T> NotNull()
{
if (Value == null)
{
throw new ArgumentNullException(Name, "Value must not be null!");
}
return this;
}
public ValidationRule<T> NotDefault()
{
T val = default(T);
if (Value.Equals(val))
{
throw new ArgumentException(Name, "Value must have default value!");
}
return this;
}
So to struct or not ? And most important why?