vladislav-yashin / crystal-di Goto Github PK
View Code? Open in Web Editor NEWLightweight DI Container for Crystal
License: MIT License
Lightweight DI Container for Crystal
License: MIT License
Hey, first off, this is my new favorite shard. I had a feature request that would make it even better; the ability to tag services. For example
class FeedPartner
include DI::Taggable
getter id : String
getter name : String
def initialize(@id : String, @name : String); end
end
module Container
include DI::ContainerMixin
register FeedPartner, FeedPartner.new("GOOGLE", "Google"), tag: "feed_partner"
register FeedPartner, FeedPartner.new("FACEBOOK", "Facebook"), tag: "feed_partner"
end
partners = Container.tagged "feed_partner" # => # => [#<FeedPartner:0x7f0f7891bd00 @id="GOOGLE", @name="Google">, #<FeedPartner:0x7f0f7891bcc0 @id="FACEBOOK", @name="Google">]
My current idea to implement this would be to define a module like DI::Taggable
that can be included into types that the user wants to be able to be tagged.
Then the ContainerMixin could define a hash like @@services = Hash(String, Array(DI::Taggable)).new
, where upon adding a resolve method, add that value to hash for the given tag.
\{% if type.resolve < DI::Taggable %}
@@services[\{{tag}}] = Array(DI::Taggable).new unless @@services.has_key? \{{tag}}
@@services[\{{tag}}] << \{{value}}.as(\{{type}})
\{% end %}
Finally, implement a method to get services based on tag:
def self.tagged(tag : String)
@@services[tag]? || [] of DI::Taggable
end
I currently have this working on my fork, however I wanted to get your thoughts before making a PR.
I added this to my shards.yml
dependencies:
di:
github: funk-yourself/crystal-di
and run crystal deps
, but I encountered following error:
Updating https://github.com/funk-yourself/crystal-di.git
git command failed: git ls-tree -r --full-tree --name-only v0.1.0 -- shard.yml (). Maybe a commit, branch or file doesn't exist?
git version 2.14.1
Crystal 0.23.1
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.