{"id":34196584,"url":"https://github.com/olivierphi/minidic","last_synced_at":"2026-03-10T08:31:20.031Z","repository":{"id":57493348,"uuid":"105203654","full_name":"olivierphi/minidic","owner":"olivierphi","description":"A Go small Dependency Injection Container, ported from PHP's Pimple","archived":false,"fork":false,"pushed_at":"2017-09-30T22:26:12.000Z","size":19,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-18T18:44:46.419Z","etag":null,"topics":["dependency-injection-container","golang","microlib","php-inspired","port"],"latest_commit_sha":null,"homepage":null,"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/olivierphi.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-09-28T22:05:55.000Z","updated_at":"2018-04-24T15:25:41.000Z","dependencies_parsed_at":"2022-08-28T15:13:03.387Z","dependency_job_id":null,"html_url":"https://github.com/olivierphi/minidic","commit_stats":null,"previous_names":["drbenton/minidic"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/olivierphi/minidic","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olivierphi%2Fminidic","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olivierphi%2Fminidic/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olivierphi%2Fminidic/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olivierphi%2Fminidic/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/olivierphi","download_url":"https://codeload.github.com/olivierphi/minidic/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/olivierphi%2Fminidic/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30328251,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-10T05:25:20.737Z","status":"ssl_error","status_checked_at":"2026-03-10T05:25:17.430Z","response_time":106,"last_error":"SSL_read: 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-container","golang","microlib","php-inspired","port"],"created_at":"2025-12-15T17:17:38.520Z","updated_at":"2026-03-10T08:31:20.020Z","avatar_url":"https://github.com/olivierphi.png","language":"Go","readme":"# ![Minidic logo](https://rougemine.com/public/minidic.png) Minidic\n\n[![Build Status](https://travis-ci.org/DrBenton/minidic.svg?branch=master)](https://travis-ci.org/DrBenton/minidic)\n\nMinidic is a small Dependency Injection Container for the Go language, ported from PHP's [Pimple](https://github.com/silexphp/Pimple/tree/1.1), that consists\nof just one file and two public interfaces (about 200 lines of code).\n\nThe test suite, and even this README file, are basically a copy-n-paste of Pimple's ones,\nwith only a light adaptation to Go and the addition of a way define services to with pre-resolved dependencies.\n\nSo all kudos go to [Fabien Potencier](http://fabien.potencier.org/) and to Pimple contributors!\n\n\nInstall it:\n\n```bash\n$ go get -u github.com/DrBenton/minidic\n```\n\nThen import it in your code, and you're good to go:\n\n```go\nimport dic \"github.com/DrBenton/minidic\"\n```\n\nCreating a container is a matter of instating the a `Container` interface:\n\n```go\ncontainer := dic.NewContainer()\n```\n\nAs many other dependency injection containers, Minidic is able to manage two\ndifferent kind of data: *services* and *parameters*.\n\n(note that a quick look at [the test suite](minidic_test.py) can also give you\n a pretty good overview of this module features)\n\n### Defining Parameters\n\nDefining a parameter is as simple as using the Container `Add(newInjection Injection)` method:\n\n```go\n// define some parameters\ncontainer.Add(dic.NewInjection(\"cookie_name\", \"SESSION_ID\"))\ncontainer.Add(dic.NewInjection(\"cookie_ttl\", 3600))\n```\n\n### Defining Services\n\nA service is an object that does something as part of a larger system.\nExamples of services: Database connection, templating engine, mailer. Almost\nany object could be a service.\n\nServices are defined by functions that return an instance of an object:\n\n```go\n// define some services\nfunc getSessionStorageConfig(c dic.Container) SessionStorageConfig {\n    cookieName := c.Get(\"cookie_name\").(string)\n    cookieTTL := c.Get(\"cookie_ttl\").(int)\n    return SessionStorageConfig{cookieName, cookieTTL}\n}\ncontainer.Add(dic.NewInjection(\"session_storage_config\", getSessionStorageConfig))\n\ncontainer.Add(dic.NewInjection(\"session_storage\", func getSessionStorageConfig(c dic.Container) SessionStorage {\n    return NewSessionStorage(c.Get(\"session_storage_config\").(SessionStorageConfig))\n})\n```\n\nNotice that the function has access to the current container\ninstance, allowing references to other services or parameters.\n\nAs objects are only created when you get them, the order of the definitions\ndoes not matter, and there is no noticeable performance penalty.\n\nUsing the defined services is also very easy:\n\n```go\n// get the session storage object\nsession := container.Get(\"session_storage\").(SessionStorage)\n\n// the above call is roughly equivalent to the following code:\n// sessionStorage := SessionStorageConfig{\"SESSION_ID\", 3600}\n// session := NewSessionStorage(sessionStorage)\n```\n\n#### Services functions with pre-resolved dependencies\n\nYou can also set a slice of services ids, with the `WithInjectedDependencies([]string)` method\nof the Injection interface. That way, you don't have to inject the container and\nmanually retrieve your service dependencies: they are already resolved when your service function\nis called!\n```go\ncontainer.Add(dic.NewInjection(\n    \"mailer.transport\",\n    func(smtpServer string, port uint) MailerTransport {\n        return \u0026MailerSmtpTransport{smtpServer, port}\n    },\n).WithInjectedDependencies([]string{\"mailer.transport.smtp\", \"mailer.transport.port\"}))\n\ncontainer.Add(dic.NewInjection(\n    \"mailer\",\n    func(transport *MailerTransport, defaultSender string, debugMode bool) Mailer {\n        return Mailer{transport, defaultSender, debugMode}\n    },\n).WithInjectedDependencies([]string{\"mailer.transport\", \"mailer.default_sender\", \"app.debug\"}))\n\ncontainer.Add(dic.NewInjection(\"mailer.transport.smtp\", \"smtp.google.com\"))\ncontainer.Add(dic.NewInjection(\"mailer.transport.port\", 10052))\ncontainer.Add(dic.NewInjection(\"mailer.default_sender\", \"olivier@rougemine.com\"))\n\n// So any service can now have a shared instance of the Mailer:\nmailer := container.Get(\"mailer\").(Mailer)\n```\n\n\n### Defining Factory Services\n\nBy default, each time you get a service, Minidic returns the same instance of it.\nIf you want a different instance to be returned for all calls, mark the service as being a \"factory\":\n\n```go\ncontainer.Add(dic.NewInjection(\"incident_context\", generateNewIncidentContext).MarkAsFactory())\n```\n\nNow, each call to `container.Get(\"incident_context\")` returns a new instance of the service.\n\n### Protecting Parameters\n\nBecause Minidic sees functions as service definitions, you need to\nmark the service as being a \"protected\" one to store them as\nparameter:\n\n```go\ncontainer.Add(dic.NewInjection(\"random_generator\", myRandonGeneratorFunction).MarkAsProtected())\n```\n\n### Modifying services after creation\n\nIn some cases you may want to modify a service definition after it has been\ndefined. You can use the `extend()` method to define additional code to\nbe run on your service just after it is created:\n\n```go\ncontainer.Add(dic.NewInjection(\"mailer\", func (c dic.Container) Mailer {\n    return NewMailJetMailer(c.Get(\"mailjet.login\").(string), c.Get(\"mailjet.password\").(string))\n})\n\nif debug {\n    container.Extend(\"mailer\", func(c dic.Container, decoratedMailer Mailer) Mailer {\n        return DebugMailer{decoratedMailer, c.Get(\"app.logs_dir\").(string)}\n    })\n    // in \"debug\" mode, the \"mailer\" service is now decorated with a DebugMailer\n}\n```\n\nThe first argument is the name of the object, the second is a function that\ngets access to the decorated object instance and the container, and returns a new\nservice definition.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Folivierphi%2Fminidic","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Folivierphi%2Fminidic","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Folivierphi%2Fminidic/lists"}