IxSoftware.Generators.IdGenerator
1.2.3
dotnet add package IxSoftware.Generators.IdGenerator --version 1.2.3
NuGet\Install-Package IxSoftware.Generators.IdGenerator -Version 1.2.3
<PackageReference Include="IxSoftware.Generators.IdGenerator" Version="1.2.3"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add IxSoftware.Generators.IdGenerator --version 1.2.3
#r "nuget: IxSoftware.Generators.IdGenerator, 1.2.3"
// Install IxSoftware.Generators.IdGenerator as a Cake Addin #addin nuget:?package=IxSoftware.Generators.IdGenerator&version=1.2.3 // Install IxSoftware.Generators.IdGenerator as a Cake Tool #tool nuget:?package=IxSoftware.Generators.IdGenerator&version=1.2.3
Id Generator
This project adds a source generator to your project that creates identity types. It's used for types that identifies some resource. For example a user id or application id.
IEquatable
The source generator adds casting operators (explicit cast to, implicit cast from), equality checks, hash code, equals and ToString()
(if it is not already provided).
It will get applied to a partial readonly struct that specifies IEquality
on itself and has a single private readonly field with no constructors defined.
The struct currently must be declared in a namespace, i.e. you cannot declare them as inner types.
Example:
public readonly partial struct UserId : IEquatable<UserId>
{
private readonly Guid value;
}
This is all you have to write. Notice that this struct would not normally compile, so if it doesn't there's probably something wrong. If you need more than a single field you can use a tuple on the value field. Note that the field doesn't have to be named value.
You can add additional methods or static fields.
Note that the default ToString()
method uses InvariantCulture. This means that if the underlying type is a DateTime
or DateTimeOffset
the result will be the wonky US date format.
IComparable
This library can also implement IComparable<T>
. Simply add it to the list of interfaces and it should just work.
This will implement int CompareTo(T other)
and the four value comparison operators.
Building
This project, or more specifically the test project, requires Visual Studio 16.8 to build and/or the 5.0 (or newer) version of the .NET Core SDK toolchain.
If this is in order the project should build either through Visual Studio or dotnet build
. A NuGet package will get built.
License
This program is licensed under the MIT license. Please see license.txt for details.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp.Workspaces (>= 3.8.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
# 1.2.3
- Fixed `StackOverflowException` when using CompareTo.
# 1.2.2
- Fixed `NullRefereneException` when using `default` as a comparison base with reference types as base type.
# 1.2.1
- Added support for generic types.
# 1.2
- Add `?` to Equals parameter if nullable is enabled.
- Implements `IComparable<T>` automatically as well.
- Casting operator now calls `bool Validate()` if such a method is defined and throws an `System.ArgumentException` if it returns `false`.
- Using a DateTime or DateTimeOffset as a backing field will now return ISO 8601 date time in ToString by default.
# 1.1.1
- Fix for release of Visual Studio 2019 16.8 which changed the interface for source generators.
- Removed error messages for old versions of Visual Studio.
# 1.1
- `Equals` for when the type is a string now use ordinal compare rather than culture dependent comparison which is the default
- Small performance and memory improvements
- Added error message if trying to build with an old version of MSBuild or .NET Core toolchain
# 1.0
- First version
- Added IdGenerator