Home >Backend Development >C++ >Should I Inject Dependencies into Attributes?
Property Dependency Injection: Not Recommended
Property injection should generally be avoided due to limitations of CLR and frameworks such as MVC and Web API.
Question
In your code, injecting IPermissionService
into AuthorizeAttribute
via property injection will be problematic because at runtime permissionService
will be null.
Solution
Please consider the following:
Option 1: Separate data and behavior
Option 2: Use Humble Objects
AuthorizeCore
method resolve the service and call its methodWhich option to choose?
The above is the detailed content of Should I Inject Dependencies into Attributes?. For more information, please follow other related articles on the PHP Chinese website!