Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler-generated type as a service #451

Closed
yallie opened this issue Feb 9, 2022 · 11 comments
Closed

Compiler-generated type as a service #451

yallie opened this issue Feb 9, 2022 · 11 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@yallie
Copy link
Contributor

yallie commented Feb 9, 2022

Hi Maksim,

I've updated my app to the last version of DryIoc and found that it's not starting up.
The application couldn't resolve its settings class generated from App.settings file.

I found the exact commit where the breaking change was introduced: 2aaedc4
Then I tried to extract the failing setup into a minimal reproducible unit test and it turned out to be tricky.

I was thinking it was the unusual setup (exported static property) that was problematic.
But finally I discovered that the issue was the CompilerGeneratedAttribute.
I'll create the pull request demonstrating the issue.

Can we disable the rule that ignores the compiler-generated classes as a services?
Does it really help speeding up service resolution?

yallie added a commit to yallie/DryIoc that referenced this issue Feb 9, 2022
@dadhi
Copy link
Owner

dadhi commented Feb 9, 2022

I will look.

@yallie
Copy link
Contributor Author

yallie commented Feb 9, 2022

Thank you! 🙏

@yallie
Copy link
Contributor Author

yallie commented Feb 9, 2022

Looks like SettingsSingleFileGenerator tool incorrectly uses CompilerGeneratedAttribute instead of
the GeneratedCodeAttribute which should be used. The same issue with StronglyTypedResourceBuilder.

Related MSDN blog post: Correct usage of the CompilerGeneratedAttribute and the GeneratedCodeAttribute.
Even Microsoft's own tools don't use the right attributes.

Unfortunately, there seems to be no way to instruct the tools to avoid emitting the attribute...

@dadhi
Copy link
Owner

dadhi commented Feb 9, 2022

Thanks for the link. It is always good to have a broader look at the problem.

@yallie
Copy link
Contributor Author

yallie commented Feb 9, 2022

I'm disappointed that CompilerGenerated attribute doesn't actually mean that the code is compiler-generated.
Sometimes C# compiler doesn't apply it to the generated code, and sometimes the attribute is misused by code generation tools.

@dadhi
Copy link
Owner

dadhi commented Feb 9, 2022

Off-topic rant:

Code generation story is lost in the details and history now.
I am in disarray in general where to go here in this direction.
The Source Generators even with the last incremental addition are very specific way to solve the problem with no much flexibility.

I would prefer to go to the T4 way of things, but with better tooling and compiler support.
Or have the pay-for-play story starting from the macros and building all the way to the full code-gen with some things in between.
I would like the Expression Trees to be the basis for such thing - partially because I know them well.
I have also like the way of LeMP/Extended C# which I have explored here. Given that the work was done by one guy it makes Microsoft look like not paying enough attention and effort.

@yallie
Copy link
Contributor Author

yallie commented Feb 9, 2022

Hmm, interesting...
Is there any benefit from static compile-time service registrations, apart from the faster start-up?
As far as I've seen, DryIoc service discovery and registration is blazing fast, compared to other containers.
For typical applications service registration would take less than a second, and other startup tasks
like looking up something in the database would require much more time anyway. Am I missing something?

@dadhi
Copy link
Owner

dadhi commented Feb 9, 2022

It is mainly about not doing at runtime what can be done at compile-time.

Benefits are having compilation errors upfront instead of runtime blow-up.
You may introspect the generated code and educate yourself that there is no magic induced by IoC. You may search the generated code, go to references, etc. etc.
Performance too, especially at start-up.

But, the sole usage of compile-time generation in real solutions is a myth. I believe you need both compile and run-time tooling and them being good at playing together to get the max of it.

@yallie
Copy link
Contributor Author

yallie commented Feb 9, 2022

Oh, I see, thanks for the insight.
Compile-time errors vs runtime errors look like the most important point to me :)
I've seen too much cryptic runtime errors in MEF times...

@dadhi
Copy link
Owner

dadhi commented Feb 19, 2022

@yallie What is the name of the class generated from App.settings?
Does it start with '<>'?

@yallie
Copy link
Contributor Author

yallie commented Feb 19, 2022

No, class name is derived from the settings file name: MyAppConfig.settingsMyAppConfig.Designer.cs.
Similar to resx resource file generator's behavior.

@dadhi dadhi closed this as completed in 3fc4df6 Feb 20, 2022
@dadhi dadhi self-assigned this Feb 27, 2022
@dadhi dadhi added the bug Something isn't working label Feb 27, 2022
@dadhi dadhi added this to the v4.8.7 milestone Feb 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants