athena-framework / athena

An ecosystem of reusable, independent components
https://athenaframework.org
MIT License
211 stars 17 forks source link

Migrate DI features to annotations #385

Closed Blacksmoke16 closed 5 months ago

Blacksmoke16 commented 6 months ago

The DI component currently has some features that are implemented at top level macro calls. Specifically .bind and .auto_configure. These were implemented like that for reasons, mainly not having thought of a better way, and due the increased complexity of the alternatives.

However with the introduction of #337, the DI component is a lot more robust and flexible when it comes to adding in features. As such, I propose we start to deprecate the "legacy" macros calls where possible into annotations that can be applied closer to the source.

E.g. something like:

module ConfigInterface; end

ADI.auto_configure ConfigInterface, {tags: ["config"]}

Could become:

@[ADI::Autoconfigure(tags: ["config"])]
module ConfigInterface; end

There is also room to improve how other DI features work by adding additional annotations. Service aliasing for example. Instead of:

module TransformerInterface; end

@[ADI::Register(alias: TransformerInterface)]
struct ShoutTransformer
  include TransformerInterface
end

You could do

module TransformerInterface; end

@[ADI::Register]
@[ADI::AsAlias(TransformerInterface)]
struct ShoutTransformer
  include TransformerInterface
end

This is nice not only from a readability perspective, but also from an implementation POV as it decouples the alias logic from the Register annotation itself.

Blacksmoke16 commented 5 months ago

ADI.bind is going to have to stick around, as there isn't really a place to add an annotation or something to for global bindings.

Blacksmoke16 commented 5 months ago

Going to keep this open, are a few more annotations I think we can add. However they're all new functionality, so can release them whenever.

Blacksmoke16 commented 5 months ago

Actually going to close this as the new annotations won't need the breaking label and can be their own issues if needed.