Author Topic: Microsoft Guard Class  (Read 2484 times)

0 Members and 1 Guest are viewing this topic.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2159
  • class keyThumper<T>:ILazy<T>
Microsoft Guard Class
« on: July 11, 2023, 07:37:42 PM »

Has anyone played with this ??

https://learn.microsoft.com/en-us/dotnet/communitytoolkit/diagnostics/guard

Quote
The CommunityToolkit.Diagnostics package contains APIs to efficiently validate method parameters and to throw exceptions in faulting code paths. It is meant to be used to help simplify all argument checks and to make them more expressive and easier to read, while at the same time improving codegen quality and performance.

This package can be installed through NuGet, and it has the following multi-targets:

.NET Standard 2.0
.NET Standard 2.1
.NET 6
This means the package can be used on any available runtime (including .NET Framework, .NET Core, UWP, Unity, Xamarin, Uno, Blazor, etc.).


Quote
The Guard can be used to validate method arguments in a streamlined manner, which is also faster, less verbose, more expressive and less error prone than manually writing checks and throwing exceptions.

>>>> . .

Code - C#: [Select]
  1. public static void SampleMethod(int[] array, int index, Span<int> span, string text)
  2. {
  3.     Guard.IsNotNull(array);
  4.     Guard.HasSizeGreaterThanOrEqualTo(array, 10);
  5.     Guard.IsInRangeFor(index, array);
  6.     Guard.HasSizeLessThanOrEqualTo(array, span);
  7.     Guard.IsNotNullOrEmpty(text);
  8. }
  9.  


Prior to C#10 the syntax is :

Code - C#: [Select]
  1. Guard.IsNotNull(array, nameof(array)))).
  2. //instead of :
  3. Guard.IsNotNull(array);
  4.  

It's basically a ready built specific assert API with a supporting 'ThrowHelper' class
I haven't had a play yet.
Looks interesting, to my mind.
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2159
  • class keyThumper<T>:ILazy<T>
Re: Microsoft Guard Class
« Reply #1 on: July 11, 2023, 10:51:50 PM »

Fluent Assertions also looks attractive.
Recommended by Scott McFarlane ( Being a Remarkable C# .NET AutoCAD Developer SD12077 )

https://fluentassertions.com/introduction

https://fluentassertions.com/basicassertions/

https://github.com/fluentassertions/fluentassertions

https://www.nuget.org/packages/FluentAssertions/6.11.0?_src=template
or through NuGet Packages in Visual Studio

Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2159
  • class keyThumper<T>:ILazy<T>
Re: Microsoft Guard Class
« Reply #2 on: July 11, 2023, 11:04:56 PM »
Nice and clean

Added packages.config :
Code - XML: [Select]
  1. <packages>
  2.   <package id="FluentAssertions" version="6.11.0" targetFramework="net48" />
  3.   <package id="System.Runtime.CompilerServices.Unsafe" version="4.5.0" targetFramework="net48" />
  4.   <package id="System.Threading.Tasks.Extensions" version="4.5.0" targetFramework="net48" />
  5. </packages>
  6.  
« Last Edit: July 11, 2023, 11:26:14 PM by kdub_nz »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2159
  • class keyThumper<T>:ILazy<T>
Re: Microsoft Guard Class
« Reply #3 on: July 11, 2023, 11:55:32 PM »

and a test of Guard Class from MS

Code - XML: [Select]
  1. <packages>
  2.   <package id="CommunityToolkit.Diagnostics" version="8.2.1" targetFramework="net48" />
  3.   <package id="System.Buffers" version="4.5.1" targetFramework="net48" />
  4.   <package id="System.Memory" version="4.5.5" targetFramework="net48" />
  5.   <package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" />
  6.   <package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net48" />
  7.   <package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" />
  8. </packages>
  9.  

Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2159
  • class keyThumper<T>:ILazy<T>
Re: Microsoft Guard Class
« Reply #4 on: July 12, 2023, 01:24:26 AM »
Some days are Diamonds :)

beer o'clock


Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8857
  • AKA Daniel
Re: Microsoft Guard Class
« Reply #5 on: July 12, 2023, 08:27:39 AM »
I’m still on the fence about exceptions vs something like std::expected
Wonder if .net has something similar

57gmc

  • Bull Frog
  • Posts: 367
Re: Microsoft Guard Class
« Reply #6 on: July 12, 2023, 11:14:29 AM »
Has anyone played with this ??

Interesting...looks like I should.

Jeff H

  • Needs a day job
  • Posts: 6151
Re: Microsoft Guard Class
« Reply #7 on: July 12, 2023, 03:17:59 PM »
Cool stuff