Enhance decorator support and syntax
See original GitHub issueThere are several issues around how we handle decorators and enhancements that are desired in that space. Updating the syntax and adding features sort of go hand-in-hand since, for the most part, it’ll all require some changes to the internals and will likely introduce some overall breaking changes.
This issue is to round up discussions about what we want decorator syntax to look like and what additional features we want to support. After that’s determined, let’s get it done. Fixing up decorator syntax has been a long time coming.
Issues:
- #529: Handle
.As<T>
and.AsImplementedInterfaces
correctly for decoration targets - #727: Conditional decorator registration
- #776: Composite pattern support
- #874: Decorator registration without keys
I think @alexmg had a spike going with some ideas on an updated decorator syntax. I’ll let him chime in as able. In the meantime, I’ll close these other issues and we can handle it here.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:12
- Comments:41 (20 by maintainers)
Top Results From Across the Web
Documentation - Decorators
Decorators provide a way to add both annotations and a meta-programming syntax for class declarations and members. Further Reading (stage 2): A Complete...
Read more >Basic Syntax and Usage of Decorators in Python | by Jane
In this article, I want to discuss a powerful feature in Python called decorators and how they can be used to enhance your...
Read more >Understanding Decorators in JavaScript: An Essential Tool ...
TypeScript, a superset of JavaScript, has native support for decorators. The syntax and behaviour of decorators in TypeScript closely ...
Read more >Using modern decorators in TypeScript
Here's an example of a simple class method decorator, demonstrating the enhanced ergonomics of the new syntax: function debugMethod(_target: ...
Read more >TypeScript Decorators: Enhance Your Code With Ease
TypeScript decorators are a powerful feature that allows developers to add extra functionality to classes, methods, properties, ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks for the feedback everyone. I have taken it on board and made some changes.
I managed to remove the requirement for using
RegisterDecorated
as a marker registration. You can register the implementation service as normal and then use the new decorator syntax for registering your decorators.When adding a registration in a child lifetime scope, if there is a decorator already registered in a parent scope, then it will be applied to the new registration.
A decorator can be registered in a child lifetime scope and will only be applied to resolutions within that scope.
You can now register decorators using a lambda syntax as shown below, where
c
isIComponentContext
,p
isIEnumerable<Parameter>
, andi
isIDecoratedService
.I also introduced the same functionality for open generic decorators (with the exception of lambda based registrations). There is no need to call a
RegisterDecorated
method for open generics either. The registrations are made as normal using the existingRegisterGeneric
method.The decorators (regular and open generic) work in conjunction with the existing relationship types like
Func
,Owned
,Lazy
andIEnumerable
.Decorator services are tracked when added to the component registry so that when resolving a service decoration is only attempted if the service is known to be decorated.
The new decorator support lives alongside the existing decorator implementation and there are no breaking changes in the API.
You can test out the updates by grabbing the
4.6.2-decorators-00449
package from our MyGet feed.https://www.myget.org/F/autofac/api/v3/index.json
I would love for people to take this for a test and drive and help lookout for edge cases. Depending on feedback I will merge the changes from the
decorators
feature branch into thedev
branch.The
DecoratorTests
andOpenGenericDecoratorTests
fixtures in thedecorator
branch are the currently best place to find additional usage examples.https://github.com/autofac/Autofac/tree/decorators/test/Autofac.Test/Features/Decorators
This is now available in the
4.9.0
release. Thank you to everyone for your feedback and contributions.