I have 3 projects
Project A (Client)
Project B (Server)
Project C (Shared)
Within Project B I have the DB connection using the entity framework as well as all the entity classes, Project A (Client) should have no access to Project B (Server). I want to rely heavily on DTOs for the client/server communication. These DTOs should be created in Project C so both client and server can use them. The source generators are perfect for the generation of the dtos, but is it possible for a source generator to use the entity classes from Project B to generate DTOs in Project C?
CodePudding user response:
You can only generate things for the current project being built, but as long as you have visibility of the types you need via a reference, you can still access the info from the other project.
The GeneratorExecutionContext
has context.Compilation.References
, which gives you all the MetadataReference
hooks. This is high-level, but you can use context.Compilation.GetAssemblyOrModuleSymbol(MetadataReference)
, which gives you the root of the type-model for each reference; from there, you can inspect all the types along with their properties, attributes, etc - presumably to generate your DTOs.
However, I wonder if it is easier / preferable to be explicit, and do something explicit in B
such as:
[module:GenerateDtoWhateverFor(typeof(B.Foo))]
[module:GenerateDtoWhateverFor(typeof(B.Bar))]
// etc
and detect / respond to that.