haps icon indicating copy to clipboard operation
haps copied to clipboard

Defining injectable where egg is same as base

Open JosXa opened this issue 5 years ago • 0 comments

I often run into the case where I don't actually want to define an interface for my implementation, but still make use of the autodiscovery and DI for instances of that class. The way I usually go about doing this with haps is to define both the base and egg decorators:

@base
@egg
@scope(SINGLETON_SCOPE)
class MySimpleService:
    pass

A quick search on my current project yields 12 occurences of this combination of decorators and I'm wondering if it might make sense to introduce a simplified decorator for marking a class as "injectable" (i.e. acting as both the base and egg). I am aware that one of the purposes of dependency injection is to decouple an interface from its implementation (e.g. for testability), but especially for private projects with fewer test cases I'd still like to make use of the control inversion and let a library figure out the autowiring of my application.

I couldn't come up with a very nice API for this concept, so I'll just throw "@injectable(scope=SINGLETON_SCOPE)" and "@base_egg" in the room.

JosXa avatar Mar 02 '20 19:03 JosXa