Coding Faster With dotNetTips.com Spargine 6 - August 2022 Release

I am pleased to announce the new release (v2022.6.8.9) of Spargine on August 1st, 2022 - my open-source projects, and NuGet packages for .NET 6. I have added new classes, methods, benchmarks, and unit tests! I use these in all the projects I work on, including many that are in production! I hope you will check them out and let me know what you would like to see added.

This release includes performance changes, too. All the performance data for these assemblies can be found on GitHub. I am always looking for help with these projects, especially writing more unit tests. If you would like to help, please email me at [email protected].

Splitting Lines of Text with Zero Allocations

If you are worried about allocations in your application, splitting lines of text could contribute to that. I have added a new method to StringExtentions called SplitLines(). This method will not create any allocations in memory. Here is an example of how to use it.

foreach (LineSplitEntry lse in text.SplitLines())
{
   Console.WriteLine(lse.Line.ToString());
}

Logging HTTPClient Requests

If you need to log when HTTPClient starts or stops a request, I found code that Gérald Barré wrote to do this and have added it to Spargine. It’s very simple to use with the HttpEventListener.

using (var listener = new HttpEventListener(logger))
{
    await _httpClient.GetStringAsync("https://c-sharpcorner.com");
}

This is an example of the data logged via the ILogger.

HTTP RequestStart: 00000011-0000-0000-0000-00008ed19d59 
https://dotnettips.com:443/ HTTP/1.1 POLICY: RequestVersionOrLower
HTTP RequestStop: 00000011-0000-0000-0000-00008ed19d59

There is also HttpEventListenerAsyncLocal, which will log the length of a request.

HTTP Request: https://dotnettips.com:443/ executed in 1408.0ms

The HttpHandlerDiagnosticListener will log the request with the response.

HTTP Observer: GET https://dotnettips.com/ 1.1 UserAgent:
HTTP Observer: OK https://dotnettips.com/

Checking Directory and File Permissions

One thing code needs to do before trying to access a directory or file is to check its permissions. This will prevent exceptions! I’ve added new methods to FileHelper.cs and DirectoryHelper.cs, called CheckPermission() that will do the trick. Here is an example of how to use it.

var path = Environment.GetFolderPath(Environment.SpecialFolder.System);
bool result = DirectoryHelper.CheckPermission(path, FileSystemRights.Write)

Finding the Index of an Item in List<>

I also found code online that will find the index of an item in IList<> without multiple enumerations.

Person result = people.IndexAtLooped(5);

Odds and Ends

One thing that I shored up for this release is ensuring that extension methods for collections have consistent HasItems() and DoesNotHaveItems(). These methods do not throw an Exception if the list is null. Here is an example of how I use DoesNotHaveItems().

if (items.DoesNotHaveItems())
{
	return array;
}

New Extension Methods

Here are some more new extension methods.

  • Add() & Subtract(): I had to add these for use in testing.
  • FastEquals: Determines if two Guid’s are equal using vectors that make it more performant than Equals.
  • IsCurrencyCode: Returns true or false if the string input is a proper three-letter currency code.
  • IsOneToSevenAlpha: Determines if the string input is an alpha character and is one to seven characters in length.
  • IsPrime: Determines if the integer is a prime number.
  • ToConcurrentDictionary: Converts a Dictionary to a ConcurrentDictionary.
  • ToRomanNumeral: Converts an integer to a Roman Numeral character.
  • ToSortedDictionary: Converts a Dictionary to a SortedDictionary.

Summary

I hope you will find these methods and more in Spargine useful for your projects. Benchmark results are published on GitHub and the links to them are in the ReadMe file.

Coding Faster with dotNetTips.com Spargine 6
Stats are from NDepend

If you would like anything added, please do a pull request, or submit an issue. If you have any comments or suggestions, please make them below.

Happy coding, geeks!


McCarter Consulting
Software architecture, code & app performance, code quality, Microsoft .NET & mentoring. Available!