Depend is a simple dependency injection framework to do the simplest and minimally invasive property injection. It just works with Protocol bindings since it is the right thing to do! And it’s pretty simple:
You can install using CocoaPods:
pod ‘Depend'
Or, you can drop the files inside Pod/Classes into your project.
Just provide the DPRegistry the implementation for the protocol:
[[DPRegistry sharedRegistry] registerImplementation:[DPDatasource class] forProtocol:@protocol(DPDatasourceProtocol) context:nil];
The implementation works this way:
If you provide a class: The injection class will instantiate for you with the default constructor If you provide an instance: The instance will be injected!
On the app delegate, you need to call the DPInjector inject method:
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [DPInjector inject]; return YES; }
To inject on a class you just need to write your property declaration with the 'injected' prefix to the setter and the readonly qualifier. It’s important to declare it on the private interface of your class.
@property (setter=injected1:) id<DPDatasourceProtocol> datasource;
@property (setter=injected2:) id<REDUser> user;
Done! When you access **self.datasource **, it is going to be already populated.
For you who wondered what is the context parameter on the DPRegistry registerImplementation:forProtocol:context: method. Defining a context on the injection and in the registration can be used to provide different implementations.
Just change the injection declaration to:
@property (setter=injected_post:) id<DPDatasourceProtocol> postDatasource; @property (setter=injected_stupid:) id<DPDatasourceProtocol> anotherStupidDatasource;
And the registration to:
[[DPRegistry sharedRegistry] registerImplementation:[DPPostDatasource class] forProtocol:@protocol(DPDatasourceProtocol) context:@“post”]; [[DPRegistry sharedRegistry] registerImplementation:[DPStupidDatasource class] forProtocol:@protocol(DPDatasourceProtocol) context:@“stupid”];
Any word that comes after the _ (underline) is the context name.
Check out this small project ! https://github.com/rafagonc/Reading-List/
And the registrations! https://github.com/rafagonc/Reading-List/blob/master/ReadingList/REDDepedencyInjection.m
The MIT License (MIT)
Copyright (c) 2015 Rafael Gonçalves
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.