SimpleHasher 1.6.0

.NET 5.0 .NET Standard 2.1
dotnet add package SimpleHasher --version 1.6.0
NuGet\Install-Package SimpleHasher -Version 1.6.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SimpleHasher" Version="1.6.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SimpleHasher --version 1.6.0
#r "nuget: SimpleHasher, 1.6.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SimpleHasher as a Cake Addin
#addin nuget:?package=SimpleHasher&version=1.6.0

// Install SimpleHasher as a Cake Tool
#tool nuget:?package=SimpleHasher&version=1.6.0

Hasher

NuGet version (SimpleHasher)

Simple Hashing helper for entities. Default behaviour calculates the HashCode for all public properties.

HasherIncludeAttribute can be used to include non-public ones.

HasherExcludeAttribute can be used to exclude public ones.

Usage

Normal

using Hasher;

public class Entity 
{
    public int Property1 { get; set; }
    public int Property2 { set => Console.Write(value); } // Skipped
    private int Property3 { get; set; } // Skipped
    [HasherInclude] private int Property4 { get; set; }
    [HasherExclude] public int Property5 { get; set; } // Skipped
}

var entity = new Entity();
var hashCode = entity.ResolveHash();

Async

using Hasher;

public class EntityWithTasks
{
    public Task<int> Property1 { get; set; } // Will await this value
    public int Property2 { set => Console.Write(value); } // Skipped
    private int Property3 { get; set; } // Skipped
    [HasherInclude] private int Property4 { get; set; }
    [HasherExclude] public int Property5 { get; set; } // Skipped
}

var entity = new EntityWithTasks();
var hashCode = await entity.ResolveHashAsync();

By using ResolveHashAsync we will get the HashCode of the actual Task value, if any, instead of the Task's HashCode.

As a service

serviceCollection.AddHasher();

...

public class MyService
{
    private readonly IHasher _hasher;
    public MyService(IHasher hasher)
    {
        _hasher = hasher;
    }

    public  void DoWork<T>(T value)
    {
        var hash = _hasher.ResolveHash(value);
    }
}

Null

Returns 0 for null values.

using Hasher;

string nullString = null;
var hashCode = nullString.ResolveHash(); // == 0

Nested vs Top Level Only

You can enable nested calculations during registration using:

serviceCollection.AddHasher(o => o.EnableNestedHashes());

Custom Mappings

Custom mappings are allowed for certain types. You can configure your mappings using:

serviceCollection.AddHasher(o => o.Register<string>(x => x.Length, x => x.Count(s => s == 's'), x => x.Count(s => s == 'd'))
                                  .Register<Entity>(x => x.Property1, x => x.Property2));

This, obviously, only works when using the injected service (IHashService). Simpler mapping has been supplied for the extension methods by using the HasherConfiguration attribute which offers support for top level property names:

[HasherConfiguration(nameof(MyProperty1), nameof(MyProperty3))]
public class MyClass3
{
    public int MyProperty1 { get; set; }
    public int MyProperty2 { get; set; } // Skipped
    public int MyProperty3 { get; set; }
}
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.1
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.6.0 192 12/22/2021
1.5.2 170 12/22/2021
1.5.1 169 12/22/2021