andrewlock / StronglyTypedId

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

[Proposal] Add shared interfaces to identify ids #63

Open hankovich opened 2 years ago

hankovich commented 2 years ago

I suggest to add an additional project (StronglyTypedId.Interfaces) with

public interface IId<TBacking>
{
    TBacking Value { get; }
}

and probably with

public interface IId<TId, TBacking>
    where TId : IId<TId, TBacking>
{
    static abstract TId New();

   // Parse/TryParse
}

IId interfaces will drammatically help us to minimize boilerplate code:

  1. Ids are stored in redis (and in rdbms via dapper) and we write the same (de)serialization code again and again, shared Parse/TryParse will help here
  2. We configure swagger to treat ids as guids and we have to explicitely enumerate all ids during configuration. With IId in place it will be possible to enumerate all ids via reflection.

We store ids in separate libraries with contracts and we do not to pollute ids with additional attributes or add swagger/dapper packages to contract libraries.

andrewlock commented 2 years ago

I'm on board with the interface as an optional extra (though I think you could do this now relatively easily?) but I'm not sure about the extra project 🤔 If you need the interface to be in a separate project from the IDs then I think your best bet is to write those yourselves, and then just add them to your ID definitions e.g.

[StronglyTypedId]
public partial struct MyId : IId<MyId , Guid> {}