andrewlock / StronglyTypedId

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

Roslyn Source Code Generator #34

Closed ignatandrei closed 2 years ago

ignatandrei commented 3 years ago

Hello What about refactoring the code on https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/ ?

( The AArnot code generator is now obsolete)

I want to put into my list of examples - https://github.com/ignatandrei/RSCG_Examples

dzmitry-lahoda commented 3 years ago

https://github.com/andrewlock/StronglyTypedId/issues/16

ignatandrei commented 3 years ago

Waiting for this ;-)

andrewlock commented 3 years ago

I've released a I've released a beta version of a major update to the library that converts to using Source generators. There's quite a few breaking changes in the release, so please make sure to check the release notes for how to update, but I'd appreciate any feedback you have before I do a final release!

I've also written a blog post announcing the update that goes into a bit more details: https://andrewlock.net/rebuilding-stongly-typed-id-as-a-source-generator-1-0-0-beta-release

Thanks! 🙂

florianbader commented 3 years ago

@andrewlock I really love the source generator approach. One thing I noticed when using them is that the converters of course require a dependency on the respective framework. So if I need a converter for EF Core I need a dependency on EF Core in the project I'm declaring my id type. This is a bit inconvenient if the id type is in a different project than the one using it with e.g. EF Core.

So my suggestion would be to have additionally a source generator for the converters like this:

[StronglyTypedIds.Converters(converters: StronglyTypedIdConverter.EfCoreValueConverter)]
public partial class OrderIdConverters { }

If you're open for that idea I'd love to try and submit a PR for that if you want.

andrewlock commented 3 years ago

Thanks for the feedback @florianbader!

Yeah, the need to reference assemblies in the same project is a limitation that currently exists, and will exist for the Json Converters too. I'm not averse to implementing something similar to your suggestion, but I'm not sure whether this is the best approach. For example, the json converters kind of have to be in the same assembly as the id, because the attribute decorating the resulting ID needs to decorate them.

I'm wondering if a better approach (in fact, a better approach in general), is to not create a separate converter (json or ef core etc) for each ID. If I made the converters generic, it would reduce the overall code size, and would make implementing something similar to your suggestion a lot easier 🤔

dzmitry-lahoda commented 3 years ago

in rust, if you want your id to have specific serde, you will refrence crate. same for orm. so it seems okey. but unlike csharp, rust has cfg if to cross compile source generators with proper deps and features. wait for that feature in csharp. it is like partials or conditional compilation, but more structured and nuget integrated.

andrewlock commented 2 years ago

I've just merged the spike I was working on for source generators, so I'll close this for now. It's still in before as I want to fix/explore a few things before full 1.0.0, but I'd appreciate any feedback you have after trying it out

ignatandrei commented 2 years ago

It is released on nuget?

andrewlock commented 2 years ago

@ignatandrei - yep 🙂 https://www.nuget.org/packages/StronglyTypedId/1.0.0-beta05

ignatandrei commented 2 years ago

I will try ASAP