andrewlock / StronglyTypedId

A Rosyln-powered generator for strongly-typed IDs
MIT License
1.52k stars 80 forks source link

Implicit Conversion of a backing type value to its ID #73

Closed kcadduk closed 2 years ago

kcadduk commented 2 years ago

Firstly, let me say, wonderful library.

Have you considered adding an implicit converter so that if you try to assign a string value to a property of type FooId struct, it will implicitly convert that string to the ID Value?

Something along the lines of

[StronglyTypedId(backingType: StronglyTypedIdBackingType.String)]
public partial struct FooId {}

public class Foo {
    public FooId FooId { get; init; } = "Initial_Id_String";
}

We're using Nanoid-Net to generate ID strings that are just as safe and almost as quick as UUID but are only 22 characters instead of 36. They're a bit more URL friendly (Youtube style IDs).

This would allow me to do this

[StronglyTypedId(backingType: StronglyTypedIdBackingType.String)]
public partial struct FooId {}

public class Foo {
    public FooId FooId { get; init; } = Nanoid.Generate();
}

Instead of this

[StronglyTypedId(backingType: StronglyTypedIdBackingType.String)]
public partial struct FooId {}

public class Foo {
    public FooId FooId { get; init; } = new FooId(Nanoid.Generate());
}

Am I making sense? Has this use case been considered. Happy to look at a PR.

kcadduk commented 2 years ago

Closed, Duplicate of #68