Giter Site home page Giter Site logo

lecaillon / preconditions Goto Github PK

View Code? Open in Web Editor NEW
24.0 5.0 6.0 69 KB

Preconditions.NET provides convenience static methods to help check that a method or a constructor is invoked with proper parameter or not.

License: MIT License

C# 100.00%
preconditions check argument-checks guard

preconditions's Introduction

Preconditions.NET AppVeyor build status NuGet

Preconditions.NET is a personal, free-time project with no funding. If you use Evolve in your daily work and feel that it makes your life easier, consider supporting its development via GitHub Sponsors ❤️ and by adding a star to this repository ⭐

Introduction

Preconditions.NET provides convenience static methods to help check that a method or a constructor is invoked with proper parameter or not. In other words it checks the pre-conditions. The goal of this class is to improve readability of code.

Preconditions.NET returns the tested value on success, allowing to check and call a method at the same time.

On failure it always throws an ArgumentException, ArgumentNullException or ArgumentOutOfRangeException.

Why Preconditions.NET v2 ?

Preconditions.NET v2.0.0 is a complete rewrite that takes advantage of nullable types, the NotNull attribute and the new .NET 6 CallerArgumentExpression attribute. It is no more mandatory to pass the parameter name being checked.

It also adds new methods (cf. checklist) and remove Check.Zero().

How to use it ?

Because Preconditions.NET is only one code file, you can either copy the Check.cs class or include the NuGet package to your project :

PM> Install-Package Preconditions.NET

Example

public class Employee : Person
{
    public Employee(string name, string id) : base(Check.NotNullOrEmpty(name))
    {
        Id = Check.NullableButNotEmpty(id);
    }

    public string Id { get; }
}

The checklist

  • Check.NotNull(object)
  • Check.NullableButNotEmpty (string)
  • Check.NotNullOrEmpty(string)
  • Check.NotNullOrEmpty(IEnumerable)
  • Check.HasNoNulls(IEnumerable)
  • Check.NotEmpty(Guid)
  • Check.FileExists()
  • Check.DirectoryExists()
  • Check.Positive()
  • Check.PositiveOrNull()
  • Check.Negative()
  • Check.NegativeOrNull()
  • Check.NotNegative()
  • Check.NotNegativeOrNull()
  • Check.True(Func)

Feedback and issues

Feedback, improvements, ideas are welcomed. Feel free to create new issues at the issues section.

preconditions's People

Contributors

lecaillon avatar strvmarv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

preconditions's Issues

Enable conditional compilation

I would like to have the ability to turn off the checks in certain conditions or areas of the code. Would you be open to a pull request that adds [Conditional("PRECONDITIONS")] to the methods, so they could be turned on and off by a define?

It would require all users to define the symbols of course, which isn't ideal.

.NET framework 4.5.2 not supported

The project currently targets .NETStandard1.3. That means companies like the one I work for that still use .NET framework 4.5.2 cannot use this library. Assuming there is no code that requires standard 1.3, please lower the nuspec target so that more of us can use the library through nuget rather than copying your code directly into our projects.

The actual error I get is:

Could not install package 'Preconditions.NET 1.2.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.5.2', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

Please sign or provide signed alternative

Only signed assemblies are allowed to run in our production environment. Attempting to run an executable that has any dependent unsigned assemblies throws an exception like

Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'Foo, Version=4.3.2.1, Culture=neutral, PublicKeyToken=null' or one of its dependencies. A strongly-named assembly is required. (Exception from HRESULT: 0x80131044)

Please sign your assembly or provide a signed alternative (as YamlDotNet does).

Support .NET 4.5

Ya, I know it's out of support as of Jan 2016, but folks still use it. No behavioral changes required, just additional targets and a new set of packages. PR shortly...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.