question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inject dependency into a class attr

See original GitHub issue

Hello,

Sorry if this question already has been answered somewhere but I went through the doc extensively and couldn’t find anything.

I would like to inject a dependency for a class attribute, but couldn’t find any way of doing it.

Something similar from python-inject:

class User(object):
    cache = inject.attr(Cache)

Thanks for your help

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
rmk135commented, Feb 27, 2021

Hello @brunopereira27 ,

You can do it with Factory provider:

cache_provider = providers.Singleton(Cache)

user_factory = providers.Factory(User)
user.add_attributes(cache=cache_provider)

user = user_factory()
assert isinstance(user.cache, Cache)

You didn’t find it because I didn’t document that. I’m sorry about that. I already have a backlog item to update documentation.

0reactions
rmk135commented, Mar 2, 2021

Closing this issue. Feel free to comment if anything.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use dependency injection with an attribute?
Strictly speaking, we cannot use dependency injection to inject a dependency into an attribute. Attributes are for metadata not behavior. [ ...
Read more >
Dependency Injection in ASP.NET Core Attributes
When working with ASP.NET core one of the features now front and centre is dependency injection, built into the framework.
Read more >
Dependency Injection in Attributes: don't do it! - NET Junkie
By registering a custom IFilterProvider and relying on property injection for the dependencies, you are able to inject dependencies into ...
Read more >
C# – How to use dependency injection with an attribute
What's the best way to inject something into an attribute class? Strictly speaking, we cannot use dependency injection to inject a dependency into...
Read more >
InjectAttribute Class (Microsoft.AspNetCore.Components)
Indicates that the associated property should have a value injected from the service ... Inherited=true)] public sealed class InjectAttribute : Attribute
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found