{"id":27088760,"url":"https://github.com/manticsic/unitycontainerattributeregistration","last_synced_at":"2025-04-06T06:22:22.113Z","repository":{"id":56149517,"uuid":"296707879","full_name":"ManticSic/UnityContainerAttributeRegistration","owner":"ManticSic","description":"Register types to unity containers using an attribute.","archived":false,"fork":false,"pushed_at":"2020-11-24T06:12:03.000Z","size":120,"stargazers_count":3,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-05T05:47:10.022Z","etag":null,"topics":["container","containers","dependency-injection","unity"],"latest_commit_sha":null,"homepage":"","language":"C#","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/ManticSic.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-09-18T19:05:27.000Z","updated_at":"2023-04-24T21:02:45.000Z","dependencies_parsed_at":"2022-08-15T13:40:20.362Z","dependency_job_id":null,"html_url":"https://github.com/ManticSic/UnityContainerAttributeRegistration","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ManticSic%2FUnityContainerAttributeRegistration","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ManticSic%2FUnityContainerAttributeRegistration/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ManticSic%2FUnityContainerAttributeRegistration/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ManticSic%2FUnityContainerAttributeRegistration/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ManticSic","download_url":"https://codeload.github.com/ManticSic/UnityContainerAttributeRegistration/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247441948,"owners_count":20939389,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["container","containers","dependency-injection","unity"],"created_at":"2025-04-06T06:22:21.139Z","updated_at":"2025-04-06T06:22:22.105Z","avatar_url":"https://github.com/ManticSic.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/ManticSic/UnityContainerAttributeRegistration.svg?branch=master)](https://travis-ci.org/ManticSic/UnityContainerAttributeRegistration)\n\n# Unity Container Attribute Registration\nThis project is inspired by the way Spring Boot allows developers to register services to the di. It provides the `@Service` annotation which marks classes for di.\n\nThis way you can immediate see if a type is registered or not. Additionally clears your start up procedure.\n\n\n## Usage\n\n### Register types\n\nYou can register types to an unity container using `UnityContainerAttributeRegistration.RegisterTypeAttribute`.\n\n```cs\nusing UnityContainerAttributeRegistration;\n\nnamespace My.Awesome.App\n{\n    public class Program\n    {\n        public static void Main(string[] args)\n        {\n            UnityContainerPopulator populator = new UnityContainerPopulator();\n            IUnityContainer container = populator.Populate();\n        }\n    }\n    \n    [RegisterType]\n    public class MyService\n    {\n    }\n}\n```\n\n### Register instances\n\nYou can register instances to an unity container using `UnityContainerAttributeRegistration.Attribute.RegisterProviderAttribute` and `UnityContainerAttributeRegistration.Attribute.RegisterInstanceAttribute`.\n\nClasses marked with `UnityContainerAttributeRegistration.Attribute.RegisterProviderAttribute` will be instantiated using the container which should be populated with the instances.\nSo you can use already registered services to create the instances, which should be later registered.\n\n```cs\nnamespace My.Awesome.App\n{\n    public class Program\n    {\n        public static void Main(string[] args)\n        {\n            UnityContainerPopulator populator = new UnityContainerPopulator();\n            IUnityContainer container = populator.Populate();\n        }\n    }\n    \n    [RegisterProvider]\n    public class InstanceProvider\n    {\n        [RegisterInstance]\n        public string Token = \"Hard coded token\";\n    }\n}\n```\n\n### Register Factory\n\nYou can register factory methods to an unity container using `UnityContainerAttributeRegistration.Attribute.RegisterProviderAttribute` and `UnityContainerAttributeRegistration.Attribute.RegisterFactoryAttribute`.\n\nClasses marked with `UnityContainerAttributeRegistration.Attribute.RegisterProviderAttribute` will be instantiated using the container which should be populated with the instances.\nSo you can use already registered services to create the instances, which should be later registered.\n\nIts only important to have the right parameters (see example).\n\n```cs\nnamespace My.Awesome.App\n{\n    public class Program\n    {\n        public static void Main(string[] args)\n        {\n            UnityContainerPopulator populator = new UnityContainerPopulator();\n            IUnityContainer container = populator.Populate();\n        }\n    }\n    \n    [RegisterProvider]\n    public class InstanceProvider\n    {\n        [RegisterFactory]\n        public MyClass Factory(IUnityContainer container)\n        {\n            // do some magic\n        }\n\n        [RegisterFactory]\n        public MyClass Factory(IUnityContainer container, Type typeValue, string stringvalue)\n        {\n            // do some magic\n        }\n    }\n}\n```\n\n### Using a custom container\n\nIt is possible to populate a already created container.\n\n```cs\nnamespace My.Awesome.App\n{\n    public class Program\n    {\n        public static void Main(string[] args)\n        {\n            IUnityContainer container = new UnityContainer();\n            UnityContainerPopulator populator = new UnityContainerPopulator();\n            populator.Populate(container);\n        }\n    }\n}\n```\n\n### Restrict the checked assemblies\n\nYou can restrict the assemblies to check, e. g. to create a container only containing registrations of one assembly.\n\n```cs\nnamespace My.Awesome.App\n{\n    public class Program\n    {\n        public static void Main(string[] args)\n        {\n            IAssemblyProvider assemblyProvider = new CustomAssemblyProvider();\n            UnityContainerPopulator populator = new UnityContainerPopulator(assemblyProvider);\n            IUnityContainer container = populator.Populate();\n        }\n    }\n\n    public class CustomAssemblyProvider : IAssemblyProvider\n    {\n        public IList\u003cAssembly\u003e GetAssemblies()\n        {\n            // return a list of assemblies to use\n        }\n    }\n}\n```\n\n### Register with name\n\nYou can use the overloads of `RegisterTypeAttribute`, `RegisterInstanceAttribute` and `RegisterFactoryAttriube` to name the registration.\n\n```cs\nnamespace My.Awesome.App\n{\n    public class Program\n    {\n        public static void Main(string[] args)\n        {\n            UnityContainerPopulator populator = new UnityContainerPopulator();\n            IUnityContainer container = populator.Populate();\n\n            string token = container.Resolve\u003cstring\u003e(\"token\");\n            string url   = container.Resolve\u003cstring\u003e(\"url\");\n        }\n    }\n    \n    [RegisterProvider]\n    public class InstanceProvider\n    {\n        [RegisterInstance(\"token\")]\n        public string Token = \"Hard coded token\";\n\n        [RegisterInstance(\"url\")]\n        public string Url = \"https://hardcoded.url\";\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmanticsic%2Funitycontainerattributeregistration","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmanticsic%2Funitycontainerattributeregistration","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmanticsic%2Funitycontainerattributeregistration/lists"}