7

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?

0

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.