postsharp / Metalama

Metalama is a Roslyn-based meta-programming framework. Use this repo to report bugs or ask questions.
176 stars 5 forks source link

Validation: Ensure all utilized types are registered in DI #316

Closed WhitWaldo closed 3 months ago

WhitWaldo commented 3 months ago

In a similar vein to what I'm trying to do in #315 , another of the validation checks I'd like to do spans multiple types and it's similarly unclear to me how I'd do it.

I would like to validate that every Dapr Workflow and Workflow Activity has been properly registered in the dependency injection framework (assuming Microsoft.Extensions.DependencyInjection for my immediate purposes). As part of the registration pipeline, this generally looks something like:

builder.Services.AddDaprWorkflow(opt => {
  opt.RegisterWorkflow<MyWorkflow>();
  opt.RegisterActivity<MyActivity>();
});

And that's it to registration. What I'm trying to identify at compile time is that each has been properly registered so I might prevent a runtime exception indicating that DI failed to inject one of the types.

Now, as it's possible developers could have types in their project that they don't actually mean to use, this is where I'd like to be able to do two checks:

1) Identify every type that implements Workflow and within that type's RunAsync method, identify each invocation of CallActivityAsync and pull the name of the activity type out of the first argument (would generally just be a string value, but that's where #315 comes in). Validate that a registration exists in the DI registration matching opt.RegisterActivity<WhateverThisActivityTypeIs>().

2) Identify every type that implements Workflow. Scan the rest of the project for any invocation against a DaprWorkflowClient instance of ScheduleNewWorkflowAsync (or there are a few other invocation method names as well) in which the name of the Workflow type is passed into the first argument. Validate that for each of these Workflows used in the project, it's similarly registered in the DI framework via opt.RegisterWorkflow<MyWorkflow>();

Is it possible to do a full-project architectural validation like either of these goals?

Thank you!

svick commented 3 months ago

I don't think there is an easy way to do this using Metalama. You could get access to the Roslyn syntax nodes and check those, but at that point, Metalama is not helping you much.