{"action":"created","issue":{"url":"https://api.github.com/repos/angular/angular/issues/5622","labels_url":"https://api.github.com/repos/angular/angular/issues/5622/labels{/name}","comments_url":"https://api.github.com/repos/angular/angular/issues/5622/comments","events_url":"https://api.github.com/repos/angular/angular/issues/5622/events","html_url":"https://github.com/angular/angular/issues/5622","id":120501874,"number":5622,"title":"DI: Adding a provider to a class (not component)","user":{"login":"yfain","id":2254842,"avatar_url":"https://avatars.githubusercontent.com/u/2254842?v=3","gravatar_id":"","url":"https://api.github.com/users/yfain","html_url":"https://github.com/yfain","followers_url":"https://api.github.com/users/yfain/followers","following_url":"https://api.github.com/users/yfain/following{/other_user}","gists_url":"https://api.github.com/users/yfain/gists{/gist_id}","starred_url":"https://api.github.com/users/yfain/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yfain/subscriptions","organizations_url":"https://api.github.com/users/yfain/orgs","repos_url":"https://api.github.com/users/yfain/repos","events_url":"https://api.github.com/users/yfain/events{/privacy}","received_events_url":"https://api.github.com/users/yfain/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-12-04T22:45:39Z","updated_at":"2015-12-05T10:38:53Z","closed_at":null,"body":"I have a service, which is a class that internally uses Http:\r\n\r\n```\r\n@Injectable()\r\nexport class ProductService{\r\n\r\n    constructor( private http: Http){}\r\n\r\n    getProductByID(productID: string): Observable{\r\n       return this.http.get(`http://localhost:8000/products/${productID}`)\r\n           .map(res => res.json());\r\n    }\r\n}\r\n```\r\n\r\nTo use this service, an external component must specify [HTTP_PROVIDERS]. This breaks encapsulation of my ProductService: now it's not a reusable black box but has strings attached. If I decide to replace Http with another service in the above code, the external component must modify its code as well. It would be nice if there was a way to allow a class (not a component) to specify its own providers.\r\n\r\nAm I asking for too much?  :)\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/angular/angular/issues/comments/162171546","html_url":"https://github.com/angular/angular/issues/5622#issuecomment-162171546","issue_url":"https://api.github.com/repos/angular/angular/issues/5622","id":162171546,"user":{"login":"pkozlowski-opensource","id":973550,"avatar_url":"https://avatars.githubusercontent.com/u/973550?v=3","gravatar_id":"","url":"https://api.github.com/users/pkozlowski-opensource","html_url":"https://github.com/pkozlowski-opensource","followers_url":"https://api.github.com/users/pkozlowski-opensource/followers","following_url":"https://api.github.com/users/pkozlowski-opensource/following{/other_user}","gists_url":"https://api.github.com/users/pkozlowski-opensource/gists{/gist_id}","starred_url":"https://api.github.com/users/pkozlowski-opensource/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pkozlowski-opensource/subscriptions","organizations_url":"https://api.github.com/users/pkozlowski-opensource/orgs","repos_url":"https://api.github.com/users/pkozlowski-opensource/repos","events_url":"https://api.github.com/users/pkozlowski-opensource/events{/privacy}","received_events_url":"https://api.github.com/users/pkozlowski-opensource/received_events","type":"User","site_admin":false},"created_at":"2015-12-05T10:38:53Z","updated_at":"2015-12-05T10:38:53Z","body":"> Am I asking for too much? :)\r\n\r\nYes :). If you don't want _users_ of your function to provide Http's implementation you could do: `constructor( private http: Http = new YourHttpImpl())`.\r\n\r\nBut the whole point of DI is that you say \"Hey, I need _something_ that implement Http _interface_. I don't care what is the exact implementation, just give me one that makes sense for your app and fulfils the interface\". As an app owner I want to have control over services used by components I'm brining in.\r\n\r\nIf you don't want people to give this control you can use my suggestion or drop DI altogether. But you can't have a cake and it a cake :-)"}}