Defensive programming framework for .NET Framework


#1

I’ve created a framework to help programmers write more robust code in a short and easily understandable way. Feel free to check it out on GitHub (https://github.com/aljazsim/defensive-programming-framework-for-net/) and let me know, what you think.


#2

While interesting, I would rather see a solution where you could use attributes for parameter validations, so you’d get:

[NotNull(buffer)]
[NotNull(data)]
[MinValue(startIndex, 0)]
public void Write(byte[] buffer, int startIndex, byte[] data)
{
    // input validation that would be too complex for attributes:
    data.Length.MustBeLessThanOrEqualTo(buffer.Length - startIndex);
    // actual execution code
    ...
}

Attributes would be better as they would be placed before the method, not inside.


#3

Thanks for your feedback. While I was considering using attributes, at the end I’ve decided against it, since they would require me using reflection for every method call, parameter names would have to be strings (making refactoring difficult) and attributes impose a long list of limitations as well, thus reducing the flexibility of usage. I have to agree, it would look a cleaner and the validation logic would be separated from the execution logic.


.NET Foundation Website | Blog | Projects | Code of Conduct