我对DI和台风特别陌生。我想知道是否可以用init方法和属性以外的方法初始化对象。我有一个名为ObjectMapper的类,ObjectMapper可以有N个ObjectMaps数。在使用台风之前,我会制作这样的地图:
ObjectMap *map1 = [ObjectMap new]; [map1 mapProperty:@"prop1" toName:@"name1"]; [map1 mapProperty:@"prop2" toName:@"name2"]; ObjectMap *map2 = [ObjectMap new]; [map2 mapProperty:@"prop3" toName:@"name3"]; mapper.maps = @[map1, map2];
在应用程序的整个生命周期中,映射和映射对象都不会改变。我想在台风中创建ObjectMapper和ObjectMaps。更新:看起来TyphoonFactoryProvider可能会有所帮助,但我不知道如何将工厂创建的对象放到“map”数组中。
发布于 2014-03-07 21:13:48
TyphoonFactoryProvider不会在这里帮助您--这个(高级)类只是提供了一种干净的方法来获得一个实例,其中一些初始化器-参数或属性直到运行时才知道。。通常在这里你可以:
TyphoonFactoryProvider只为您编写自定义工厂代码,并处理一些内存管理细节。(懒惰的依赖性)。它对例如:从一个视图控制器到另一个视图控制器的转换很有用。
如果我明白你的意思,你想做的事在台风中是不可能的。但是,您总是可以插入一个对象实例(配置信息)和一个afterPropertyInjection回调以结束。示例:
-(id) mappedComponent
{
return [TyphoonDefinition withClass:[MyType class] properties:^(TyphoonDefinition* definition)
{
// Any object. This time an NSDictionary using Objc literals shorthand
[definition injectProperty:@selector(mappings) withObjectInstance:@{
@"prop1" : @"name1",
@"prop2" : @"name2",
@"prop3" : @"name3"
}];
//This can be a category method if you don't "own" the class in question. The method puts the object in the required state, using the config data provided.
definition.afterPropertyInject = @selector(myConfigMethod)];
}];
}发布于 2014-03-20 10:49:11
如果你已经做好了风险准备,你可以尝试开发版本的台风支持方法注射。(至今仍无证件,但似乎有效)
-(id) mappedComponent
{
return [TyphoonDefinition withClass:[ObjectMap class] injections:^(TyphoonDefinition *definition) {
[definition injectMethod:@selector(mapProperty:toName:) withParameters:^(TyphoonMethod *method) {
[method injectParameterWith:@"property"];
[method injectParameterWith:@"name"];
}];
}];
}https://stackoverflow.com/questions/22255868
复制相似问题