Attribute Dependency Injection: Challenges and Better Solutions
Injecting dependencies directly into attributes presents significant difficulties because the Common Language Runtime (CLR) manages their instantiation. This also applies to property injection, creating tight coupling and making the code brittle.
Such practices can interfere with dependency injection container verification, complicate framework caching mechanisms, and result in tightly coupled dependencies, increasing the likelihood of bugs.
Consequently, it's best to avoid dependency injection within attributes.
Superior Alternatives:
Passive Attributes (Separating Data and Behavior):
Humble Objects (Logic Extraction):
Suggested Approach:
Option 1, separating data and behavior, generally leads to cleaner designs and broader applicability. However, Option 2 provides a more practical solution in certain contexts.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3