{"id":13413436,"url":"https://github.com/magic003/alice","last_synced_at":"2026-01-12T00:02:56.802Z","repository":{"id":57496871,"uuid":"87647629","full_name":"magic003/alice","owner":"magic003","description":"An additive dependency injection container for Golang.","archived":false,"fork":false,"pushed_at":"2017-04-26T06:08:23.000Z","size":41,"stargazers_count":50,"open_issues_count":0,"forks_count":4,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-07-31T20:52:20.727Z","etag":null,"topics":["dependency-injection","golang","ioc-container"],"latest_commit_sha":null,"homepage":"https://godoc.org/github.com/magic003/alice","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/magic003.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-04-08T16:25:21.000Z","updated_at":"2024-06-13T21:18:05.000Z","dependencies_parsed_at":"2022-09-03T02:30:54.884Z","dependency_job_id":null,"html_url":"https://github.com/magic003/alice","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/magic003/alice","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magic003%2Falice","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magic003%2Falice/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magic003%2Falice/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magic003%2Falice/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/magic003","download_url":"https://codeload.github.com/magic003/alice/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magic003%2Falice/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28328698,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-11T22:11:01.104Z","status":"ssl_error","status_checked_at":"2026-01-11T22:10:58.990Z","response_time":60,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dependency-injection","golang","ioc-container"],"created_at":"2024-07-30T20:01:40.350Z","updated_at":"2026-01-12T00:02:56.784Z","avatar_url":"https://github.com/magic003.png","language":"Go","readme":"# Alice \n\n[![Release](https://img.shields.io/github/release/magic003/alice.svg?style=flat)](https://github.com/magic003/alice/releases/latest)\n[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat)](https://godoc.org/github.com/magic003/alice)\n[![Build Status](https://travis-ci.org/magic003/alice.png?branch=master)](https://travis-ci.org/magic003/alice)\n[![Coverage Status](https://coveralls.io/repos/github/magic003/alice/badge.svg?branch=master)](https://coveralls.io/github/magic003/alice?branch=master)\n[![Go Report Card](https://goreportcard.com/badge/github.com/magic003/alice?style=flat)](https://goreportcard.com/report/github.com/magic003/alice)\n\nAlice is an additive dependency injection container for Golang.\n\n## Philosophy\n\nDesign philosophy behind Alice:\n* The application components should **not** be aware of the existence of a DI container.\n* Use static Go files to define the object graph.\n* Developer has the freedom to choose the way to initialize objects.\n\n## Install\n\n```\n$ go get github.com/magic003/alice\n```\n\n## Usage\n\nAlice is inspired by the design of [Spring JavaConfig](http://docs.spring.io/spring-javaconfig/docs/1.0.0.M4/reference/html/). \n\nIt usually takes 3 steps to use Alice.\n\n### Define modules\n\nThe instances to be managed by the container are defined in modules. There could be multiple modules organized by the functionality of the instances. Modules are usually placed in a separate package.\n\nA typical module looks like this:\n\n```go\ntype ExampleModule struct {\n    alice.BaseModule\n    Foo Foo `alice:\"\"`\n    Bar Bar `alice:\"Bar\"`\n    Baz Baz\n}\n\nfunc (m *ExampleModule) InstanceX() X {\n    return X{m.Foo}\n}\n\nfunc (m *ExampleModule) InstanceY() Y {\n    return Y{m.Baz}\n}\n```\n\nA module struct must embed the `alice.BaseModule` struct. It allows 3 types of fields:\n* Field tagged by `alice:\"\"`. It will be associated with the same or assignable type of instance defined in other modules.\n* Field tagged by `alice:\"Bar\"`. It will be associated with the instance named `Bar` defined in other modules.\n* Field without `alice` tag. It will **not** be associated with any instance defined in other modules. It is expected to be provided when initializing the module. It is not managed by the container and could not be retrieved.\n\nIt is also common that no field is defined in a module struct.\n\nAny public method of the module struct defines one instance to be intialized and maintained by the container. It is required to use a pointer receiver. The method name will be used as the instance name. The return type will be used as the instance type. Inside the method, it could use any field of the module struct to create new instances.\n\n### Create container\n\nDuring the bootstrap of the application, create a container by providing instances of modules.\n\n```go\nm1 := \u0026ExampleModule1{}\nm2 := \u0026ExampleModule2{...}\ncontainer := alice.CreateContainer(m1, m2)\n```\n\nIt will panic if any module is invalid.\n\n### Retreive instances\n\nThe container provides 2 ways to retrieve instances: by name and by type.\n\n```go\ninstanceX := container.InstanceByName(\"InstanceX\")\n\ninstanceY := container.Instance(reflect.TypeOf((Y)(nil)))\n```\n\nIt will panic either if no instance is found or if multiple matched types are found.\n\n## Example\n\nA dummy [example](https://github.com/magic003/alice/tree/master/example) using Alice.\n","funding_links":[],"categories":["Miscellaneous","杂项","Microsoft Office","\u003cspan id=\"其他-miscellaneous\"\u003e其他 Miscellaneous\u003c/span\u003e","其他杂项","雜項","Dependency Injection","其他"],"sub_categories":["Dependency Injection","Advanced Console UIs","依赖注入","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e","依赖性注入","高级控制台界面","高級控制台界面","交流"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagic003%2Falice","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmagic003%2Falice","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagic003%2Falice/lists"}