Skip to content
HenkKin edited this page Oct 1, 2019 · 8 revisions

Identifiers

Build Status NuGet NuGet BCH compliance

Summary

The Identifiers library solves the problem of using int or guid as a Id. An Id should be represented by a special type. This library gives you the Identifier type.

Before creating this type I was reading some great articles which helped me a lot.

https://andrewlock.net/using-strongly-typed-entity-ids-to-avoid-primitive-obsession-part-1/
https://www.martinfowler.com/bliki/ValueObject.html/
https://lostechies.com/jimmybogard/2007/12/03/dealing-with-primitive-obsession/

This library is Cross-platform, supporting netstandard2.1.

Installing Identifiers

You should install Identifiers with NuGet:

Install-Package Identifiers

Or via the .NET Core command line interface:

dotnet add package Identifiers

Either commands, from Package Manager Console or .NET Core CLI, will download and install Identifiers. Identifiers has no dependencies.

Usage

Install via NuGet first: Install-Package Identifiers

To use it:

using Identifiers;

public class YourEntity
{
    public Identifier Id { get; set; }
    ...

ASP.NET Core

If you're using ASP.NET Core and you want to use this Identifier type in your models, then you can use Identifiers.AspNetCore package which includes a IServiceCollection.AddIdentifiers<[InternalClrType:short|int|long|Guid]>() extension method, allowing you to register all needed RouteConstraints, ModelBinders and JsonConverters.

EntityFrameworkCore.SqlServer

If you're using EntityFrameworkCore.SqlServer and you want to use this Identifier type in your entities, then you can use Identifiers.EntityFrameworkCore.SqlServer package which includes a DbContextOptionsBuilder.UseIdentifiers<[InternalClrType:short|int|long|Guid]>() extension method, allowing you to register all needed IValueConverterSelectors and IMigrationsAnnotationProviders. It also includes a PropertyBuilder<Identifier>.IdentifierValueGeneratedOnAdd() extension method, allowing you to register all needed configuration to use SqlServerValueGenerationStrategy.IdentityColumn.

Clone this wiki locally