Coding Faster with dotNetTips.com Spargine 6: Aug 2023 Release

I am pleased to announce the release (v2023.6.8.1) of Spargine on August 1st, 2022. Spargine is my open-source project and NuGet packages for .NET 6 & 7, which has been updated with new classes, methods, benchmarks, and unit tests. This release comes with speed improvements based on benchmark tests from the 3rd edition of “Rock Your Code: Code & App Performance for Microsoft .NET,” available on Amazon. These improvements have been implemented in all my projects, including many currently in production. I encourage you to explore these changes and provide feedback on what more you would like to see added.

GitHub: https://github.com/RealDotNetDave/dotNetTips.Spargine/releases
NuGet: http://bit.ly/dotNetDaveNuGet

You can find all the performance data for these assemblies on GitHub. I constantly seek assistance with these projects, particularly writing more unit tests. If you are interested in contributing, please feel free to contact me via email at dotnetdave@live.com.

SimpleResult<T>

Many software engineers prefer using a result type for method returns instead of relying on throwing exceptions, as exceptions can have a negative impact on application performance. I strongly believe that this approach can be highly beneficial in certain scenarios, and I have been eagerly anticipating the addition of a result type by the .NET team. However, it appears that the team currently has no plans to introduce such a type.

Not willing to wait, I decided to take matters into my own hands and create a result type in Spargine. To develop my design, I began with a prototype provided by the team. You can access and review the prototype by visiting the following link: https://github.com/dotnet/dotNext/blob/master/src/DotNext/Result.cs

Let’s now delve into the methods, properties, and extension methods that I have implemented in the result type.

Methods
FromResult(in SimpleResult<T>)Extracts the value from SimpleResult<T> .
Or(T?)Returns the value if present; otherwise return the default value.
OrDefault()Returns the value if present; otherwise return the default value.
SimpleResult(Exception)Initializes a new instance of SimpleResult<T> that accepts Exception as the parameter.
SimpleResult(ExceptionDispatchInfo)Initializes a new instance of SimpleResult<T> that accepts ExeptionDispatchInfo as the parameter.
SimpleResult(T)Initializes a new instance of SimpleResult<T> that accepts T obj as the parameter.
ToString()Returns the error message or the string representation of the value.
TryGet(out T)Attempts to extract the value if it is present.
Properties
ErrorGets the exception associated with the result.
ErrorMessagesReturns the Exception message, including the InnerExceptions as a comma delimited string.
IsSuccessfulIndicates that the result is successful.
ValueExtracts the actual result.
Extension Methods
Coalesce<T>Returns the second result if the first is unsuccessful.
FromException<T>Creates a new instance of SimpleResult<T> from the specified exception.
FromValue<T>Creates a new instance of SimpleResult<T> from the specified value.
GetReference<T>Gets a reference to the underlying type.
OrNull<T>If the result is successful, returns it, otherwise it returns null.

Next, I would like to demonstrate how I utilized the SimpleResult<T> in a method within Spargine. In the Spargine library, there is an extension method called ToJson() that I have implemented. Here’s the code snippet for better understanding:

public static string ToJson([NotNull] this object obj)
{
     return JsonSerializer.Serialize(obj.ArgumentNotNull());
}

If an error occurs in the ToJson() function while calling Serialize() in the JsonSerializer, an exception will be thrown. Now, let’s explore how I have overloaded this message and utilized SimpleResult<T>.

public static SimpleResult<string> ToJson([NotNull] this object obj,
                                                         JsonSerializerOptions options = null)
{
     try
     {
          return SimpleResult.FromValue(JsonSerializer.Serialize(obj, options));
     }
     catch (NotSupportedException ex)
     {
          return SimpleResult.FromException<string>(ex);
     }
}

The NotSupportedException is the exception that could be thrown from Serialize(). However, in the current implementation, instead of throwing an exception, it returns SimpleResult<T>. If you’re interested in experimenting with this pattern of returning results, I encourage you to give it a try.

Odds & Ends

Here are additional features that have been added for this release:

  • ExceptionThrower: A new method called ThrowNetworkConnectionException has been included, which returns a default message stating, “Unknown network connection issue.”
  • EnumerableExtensions: The following new methods have been introduced.
    • HasDuplicates<T>: This method checks whether the collection contains any duplicates. The original code for this method was contributed by Milan Jovanović.
    • RemoveDuplicates<T>: With this method, duplicate items can be removed from a collection.

Summary

I trust that these methods in Spargine will be of greater utility to your projects. The benchmark results are available on GitHub, and you can access the links to them in the ReadMe file. If you require additional functionality, feel free to submit a pull request. I would love to hear your feedback and suggestions; kindly post them in the comments section below.

If you’re interested in enhancing your code performance, you may wish to explore my new book on the subject, available via this link: https://bit.ly/CodePerf3.

Keep coding geeks!


Discover more from dotNetTips.com

Subscribe to get the latest posts sent to your email.

One thought on “Coding Faster with dotNetTips.com Spargine 6: Aug 2023 Release

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.