Skip to content
This repository has been archived by the owner. It is now read-only.

Repository files navigation

Thorium Defensive Extensions

A library that makes defensive programming easier.

What is defensive programming?

"Defensive programming is about protecting yourself from being hurt by something dangerous (If bad data is sent to a routine, it will not hurt the routine). By writing code that will protect yourself from bad data, unexpected events, and other programmers mistakes, will in most case reduce bugs and create a high quality software. Good programmers will not let bad data through. It’s important to validate input parameters to not let the garbage in. It’s also important to make sure that if garbage does come in, noting will goes out or an exception will be thrown. "

https://weblogs.asp.net/fredriknormen/defensive-programming-and-design-by-contract-on-a-routine-level

Getting Started

  1. Install the Thorium.FluentDefense package https://www.nuget.org/packages/Thorium.FluentDefense
  2. Include the using statement for the defensive extensions: using Thorium.FluentDefense

Example Code

publicvoidEnableAuthentication(stringtokenBaseUrl,stringclientId,stringclientSecret,stringappName){_tokenBaseUrl=tokenBaseUrl;tokenBaseUrl.Defend(nameof(tokenBaseUrl)).ValidUri().Throw();// throw an exception if any of the validations failclientId.Defend(nameof(clientId)).NotNullOrEmpty().ErrorMessage;// Get a single string newline seperated list of errors.varerrors=clientSecret.Defend(nameof(clientSecret)).NotNullOrEmpty().Errors;// get a list of errorsappName.Defend(nameof(appName)).NotNullOrEmpty().Throw();_tokenAuthenticationCredentials=newTokenAuthenticationCredentials{AppName=appName,ClientId=clientId,ClientSecret=clientSecret};}

About

Defensive programming with fluent syntax

Topics

Resources

Stars

0 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages