Git Product home page Git Product logo

strongtypedid's Introduction

StrongTypedId

A super small library for providing strong typed Ids (as opposed to using primitives).

The benefit of this is simple: You don't run the risk of accidentally using the wrong type of id. (e.g. sending a UserId into a query for products)

This works through the use of an abstract base class (StrongTypedId<TStrongTypedId, TPrimitiveId>) which is inherited to gain the id functionality.

This project is inspired by Andrew Lock's StronglyTypedId. However I needed support for .Net 5 and thus this project was born. It has since evolved to .Net 7.

Installation

I recommend using the NuGet package: StrongTypedId however feel free to clone the source instead if that suits your needs better.

Usage

Specify your class like this:

[TypeConverter(typeof(StrongTypedValueTypeConverter<UserId, Guid>))]
[StrongTypedIdJsonConverter<UserId, Guid>]
public class UserId: StrongTypedId<UserId, Guid>
{
	public UserId(Guid value) : base(value)
	{
	}
}

This specifies that the class UserId is in fact a Guid and can be used in place of a Guid. And that's basically all there is to it, now you just use UserId in place of Guid where you're dealing with an User's Id.

You can omit the JsonConverter if you don't use json serialization as well as the TypeConverter if you're not using WebAPI or MVC.

Furthermore there are a couple of base classes available to you:

  • StrongTypedValue for anything that's not an id, this supports string as a primitive value.
  • StrongTypedId for anything that IS an id, this only supports struct types as primitives (therefore no strings).
    • Adds the static Parse(string) and TryParse(string, out TStrongTypedId) methods.
  • StrongTypedGuid a further specialization of StrongTypedId.
    • Adds the static New() method for instantiating new ids with random values as well as the static Empty property.

Finally you can recognize a StrongTyped value by calling the extension method IsStrongTypedValue(). All strong typed values furthermore implements both IStrongTypedValue<TPrimitiveValue> as well as IStrongTypedValue (The latter being strictly a marker interface).

Compatibility

Dapper.DDD.Repository

This can work without any extensions, however it's a bit simpler to use via the package StrongTypedId.Dapper.DDD.Repository.

Entity Framework

This is supported through the package StrongTypedId.EntityFrameworkCore.

WebAPI

This is supported through the use of the built-in JsonConverter and TypeConverter.

MVC

This is supported through the use of the built-in TypeConverter.

NewtonSoft.Json

This is supported through the package StrongTypedId.NewtonSoft.

Swagger

This is supported through the package StrongTypedId.Swagger.

strongtypedid's People

Contributors

dependabot[bot] avatar steffenskov avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.