{"action":"closed","issue":{"url":"https://api.github.com/repos/Kodein-Framework/Kodein-DI/issues/78","repository_url":"https://api.github.com/repos/Kodein-Framework/Kodein-DI","labels_url":"https://api.github.com/repos/Kodein-Framework/Kodein-DI/issues/78/labels{/name}","comments_url":"https://api.github.com/repos/Kodein-Framework/Kodein-DI/issues/78/comments","events_url":"https://api.github.com/repos/Kodein-Framework/Kodein-DI/issues/78/events","html_url":"https://github.com/Kodein-Framework/Kodein-DI/issues/78","id":254774367,"number":78,"title":"Support and/or make clear how to make bindings via reflection","user":{"login":"kinghajj","id":3409,"avatar_url":"https://avatars0.githubusercontent.com/u/3409?v=4","gravatar_id":"","url":"https://api.github.com/users/kinghajj","html_url":"https://github.com/kinghajj","followers_url":"https://api.github.com/users/kinghajj/followers","following_url":"https://api.github.com/users/kinghajj/following{/other_user}","gists_url":"https://api.github.com/users/kinghajj/gists{/gist_id}","starred_url":"https://api.github.com/users/kinghajj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kinghajj/subscriptions","organizations_url":"https://api.github.com/users/kinghajj/orgs","repos_url":"https://api.github.com/users/kinghajj/repos","events_url":"https://api.github.com/users/kinghajj/events{/privacy}","received_events_url":"https://api.github.com/users/kinghajj/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2017-09-01T23:53:25Z","updated_at":"2018-04-13T06:58:57Z","closed_at":"2018-04-13T06:58:57Z","author_association":"NONE","body":"We have a REST API implemented with DropWizard. It uses the resource/repository pattern. We currently aren't using any DI, which is starting to get rather hairy as we keep adding more. I've been experimenting using Kodein, and had hoped that I could use reflection to dynamically find all classes that inherit from our `Repository`/`Resource` interfaces and set bindings dynamically.\r\n\r\n            val kodein = Kodein {\r\n                val reflections = Reflections(\"com.stuff.api\")\r\n\r\n                bind() from setBinding<Resource>()\r\n\r\n                val resourceClasses = reflections.getSubTypesOf(Resource::class.java).filter { !Modifier.isAbstract(it.modifiers) }\r\n                for (resourceClass in resourceClasses) {\r\n                    // doesn't work\r\n                    bind() from singleton { resourceClass.getConstructor(Kodein::class.java).newInstance(kodein) }\r\n                    bind<Resource>().inSet() with provider { kodein.Instance(TT(resourceClass)) }\r\n                }\r\n            }\r\n\r\nThe problem with the line under the comment is that the binding is made to `Resource` instead of the specific `T` that `resourceClass` represents, so the second iteration of the loop causes an exception. I found `Bind(TypeToken)` and started trying something like\r\n\r\n    for (resourceClass in resourceClasses) {\r\n        val typeToken = TT(resourceClass)\r\n        Bind(typeToken). // what now?\r\n    }\r\n\r\nThe problem, though, is that all of the implementations of the `Binding` interface take a type parameter, which I can't provide because of using reflection.\r\n\r\nMaybe I'm missing something, and there's a better way to go about this? I'd really just like to avoid needing to write this for each of our dozens of resources:\r\n\r\n    bind() from singleton { FooResource(kodein) }\r\n    bind<Resource>().inSet() with provider { kodein.instance<FooResource>() }\r\n\r\n"}}
{"actor":{"display_login":"SalomonBrys"}}