简体   繁体   中英

Typhoon inject a reference and initialise it

Is there any possibility in Typhoon to return a an instance in the assembly file ?

I would like to inject a reference of AFHTTPRequestOperationManager and then setting both the response and request serializer.

I imagine I can do something like this :

- (AFHTTPRequestOperationManager*) httpRequestManager{

    return [TyphoonDefinition withClass:[AFHTTPRequestOperationManager class] configuration:^(TyphoonDefinition * definition){

        [definition useInitializer:@selector(initWithBaseURL:) parameters:^(TyphoonMethod * initializer){

            NSURL * baseURL = [NSURL URLWithString:kPBAuthenticatorBaseURL];
            [initializer injectParameterWith:baseURL];
        }];

        [definition performAfterInjections:@selector(setResponseSerializer:) parameters:^(TyphoonMethod * initializer){}];
        [definition performAfterInjections:@selector(setRequestSerializer:) parameters:^(TyphoonMethod * initializer){}];
    }];

}

Is there no simple option to return an instance like this :

- (AFHTTPRequestOperationManager*) httpRequestManager{


    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];

    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];

   //Do some stuff to register it in the graph object --> HELP?

}

Is this approach wrong ?

It's currently possible to inject inline a simple object to an initializer, property or method.

It's not currently possible to declare a method that returns a simple object, and have that participate in the assembly. The reasons are:

  • Typhoon instruments the assembly at startup and all methods are expected to return a TyphoonDefinition. Typhoon allocates a key based on the method name and registers the definition in the factory.
  • After activation the TyphoonComponentFactory returns built instances based on the definitions. Your assembly interfaces can pose in front of the TyphoonComponent factory. Note that the actual assembly implementation (contents of .m file) has been discarded by this time, and we're just reusing the interfaces as a proxy to the TyphoonComponentFactory
  • We wanted to encourage keeping assemblies simple by returning only definitions.

It would be simple to relax the rule requiring all assembly methods to return a TyphoonDefinition. This would allow injecting the object returned by such a method as a dependency in other definitions, bearing in mind that it would not participate in Typhoon's scope pool.

Having such an instance automatically registered in the container would also be possible, though more involved. It would be necessary to agree on which implicit scope such an object has.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM