iOS 依赖注入与Objection
Created by Linxi 2020/05/01
依赖注入
首先先说明什么叫做依赖注入
比方AController跳转到BController,那么这时候BController就需要在AController内部进行实例化,如下
@implementation AController : UIViewController...- (void)jump { BController *bController = [[BController alloc] init]; [self.navigationController pushViewController:bController animated:YES];}@end这么做的话,当AController被封装成组件之后,BController的配置将会被限制,外部无法改变BController任何细节,所以我们 ** 稍 加 改 进 **
@implementation AController : UIViewController...- (instancetype)initWithCreateBlock:(UIViewController *(^)(void))createBViewControllerBlock { .... self.createBViewControllerBlock = createBViewControllerBlock; ...}- (void)jump { UIViewController *bController = self.createBViewControllerBlock(); [self.navigationController pushViewController:bController animated:YES];}@end[[AController alloc] initWithCreateBlock:UIViewController* ^{ BController *bController = [[BController alloc] initWithTitle:@"xxx"]; return bController;}];将BController的创立通过Block暴露出来,AController内部不关心BController是如何被创立的,那么AController对BController的依赖将通过外部的Block进行注入。
这,就是依赖注入。
当然这是最简单的依赖注入,无法满足我们复杂的需求,所以有时候我们需要使用第三方框架,如Objection和Typhoon
Objection
接下来说明一下Objection的使用
Objection 是一个依赖注入框架,能够在你获取一个类的实例的时候,这个类内部的属性也同时会被实例化。举个例子:
//Car.h@class Engine,Break;@interface Car : NSObject@property (nonatomic, strong) Engine *engine;@property (nonatomic, strong) Break *breaks;@end//Car.m#import <Objection/Objection.h>@implementation Carobjection_requires(@"engine", @"breaks")@end创立一个默认注射器
JSObjectionInjector *injector = [JSObjection createInjector];[JSObjection setDefaultInjector:injector];实例化Car对象
Car *car = [[JSObjection defaultInjector] getObject:[Car class]];这时候所依赖的engine对象和breaks对象都会通过init方法实例化
最后打印属性
car <Car: 0x6000006d8480> engine <Engine: 0x6000004841b0> breaks <Break: 0x6000004841e0>如果说Car对象不能通过init或者者initWithXXX等自己设置构造方法去实例化,那么我们需要指定方法,让注射器在指定的方法构建依赖
@implementation Carobjection_requires(@"engine", @"breaks")- (void)awakeFromNib { [[JSObjection defaultInjector] injectDependencies:self];}@end当Car被注射器初始化完成之后,会调用- awakeFromObjection方法,这里可以额外赋少量值
- (void)awakeFromObjection { self.test = @"111";}上面的说的都是直接init出来的对象,但是更多情况下我们需要指定构造方法
@implementation Carobjection_initializer_sel(@selector(initWithObject:)) // 该宏只要且只能出现一次- (instancetype)initWithObject:(id)object{ if (self = [super init]) { self.test = object; } return self;}@end取出的时候加上argumentList:参数就可
Car *car = [[JSObjection defaultInjector] getObject:[Car class] argumentList:@[@"aaaa"]];或者者不想写objection_initializer_sel()宏的话
可以直接在取的方法那里改动一下变成
Car *car = [[JSObjection defaultInjector] getObject:[Car class] initializer:@selector(initWithObject:) argumentList:@[@"aaaa"]];效果也是一样的
对象工厂
在Car中增加一个对象工厂属性
@property(nonatomic, strong) JSObjectFactory *objectFactory;而后标记注入里面加多一个objectFactory
objection_requires(@"engine", @"breaks",@"objectFactory")而后你即可以通过
id obj = [self.objectFactory getObject:[Engine class]];获取到对应的对象
模块
你可以创立一个继承自JSObjectionModule的模块,在里面绑定相对应的事物,便可直接取到对应的值
例如 一个协议和一个模块类,对象绑定了类名和这个类所遵循的协议
@protocol APIService <NSObject>- (void)api:(NSString *)params;@end@interface ModuleA : JSObjectionModule@end@implementation ModuleA- (void)configure{ [self bindClass:[MyAPIService class] toProtocol:@protocol(APIService)];}@end这时候注射器初始化方式改为
JSObjectionInjector *injectorA = [JSObjection createInjector:ModuleA.new]; [JSObjection setDefaultInjector:injectorA];你即可以直接拿到对应遵循了这个协议的对象而不用通过ModuleA的实例对象
MyAPIService *delegate = [injectorA getObject:@protocol(APIService)];注意因为绑定的时候是用了bindClass:方法,所以每次取出都是不同的对象
除了绑定对象类名和协议外,还可以绑定一个对象和绑定一个类名
@implementation ModuleA- (void)configure{ [self bind:对象实例 toClass:[UIApplication class]]; [self bind:对象实例 toProtocol:@protocol(UIApplicationDelegate)];}@end**注意因为绑定的时候是用了bind:方法,所以每次取出都是相同的对象 **
当对象被创立的时候,可以通过bindBlock:方法进行干涉
@implementation ModuleA- (void)configure{ [self bindClass:[MyAPIService class] toProtocol:@protocol(APIService)]; [self bindBlock:^id(JSObjectionInjector *context) { MyAPIService *service = [[MyAPIService alloc] init]; service.buildByMySelf = YES; return service; } toClass:[MyAPIService class]];}@end上面这个例子表示MyAPIService被实例化后都会带上buildByMySelf = YES
但是用这种方法的话,如果用注射器取出对象的时候带上了参数,那我们就没办法拿到参数了,所以我们需要用到ObjectionProvider协议
@interface ProviderA : JSObjectionModule<JSObjectionProvider>@end@implementation ProviderA- (id)provide:(JSObjectionInjector *)context arguments:(NSArray *)arguments{ MyAPIService *service = [[MyAPIService alloc] init]; service.buildByProvider = YES; service.arguments = arguments; return service;}- (void)configure{ [self bindProvider:[ModuleA new] toClass:MyAPIService.class];}@end这样子就能手动构建对象并且得到参数了
作用域
上面提及的bindClass:、bindBlock:、bindProvider:这些方法,都有一个拓展参数inScope:(JSObjectionScope)scope;
比方:
[self bindClass:[MyAPIService class] toProtocol:@protocol(APIService) inScope:JSObjectionScopeSingleton named:@""];[self bindBlock:^id(JSObjectionInjector *context) { MyAPIService *service = [[MyAPIService alloc] init]; service.buildByMySelf = YES; return service;} toClass:[MyAPIService class] inScope:JSObjectionScopeSingleton named:@""];[self bindProvider:[ModuleA new] toClass:MyAPIService.class inScope:JSObjectionScopeSingleton];JSObjectionScopeSingleton意味着注射器取出来的都是同个对象,JSObjectionScopeNormal意味着注射器取出来的是不同对象。
总结
Objection 帮助你实现** 依赖注入 **,你只要要完成两件事情,配置依赖关系和获取依赖对象。配置依赖关系时,你可以使用几个常用的宏来快速的完成依赖关系的配置,另外你还可以使用模块的概念来完成更多的绑定操作,它允许你将某些类或者某些协议接口绑定到某个或者某一类的对象上,在配置完成后,你即可以使用关键的 injector 注入器获取到你所需要的对象。
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » iOS 依赖注入与Objection