Skip to content

Instantly share code, notes, and snippets.

@bakertim
Created February 5, 2014 20:10
Show Gist options
  • Save bakertim/8832024 to your computer and use it in GitHub Desktop.
Save bakertim/8832024 to your computer and use it in GitHub Desktop.
Objective-c: Dependecy Injection Class
//
// SRSDependencyBuilder.m
//
// Created by Tim Westbaker on 2/5/14.
// Copyright (c) 2014 Double Blue Sports. All rights reserved.
//
// Objective C version of
// Original PHP code: http://net.tutsplus.com/tutorials/php/dependency-injection-huh/
#import <Foundation/Foundation.h>
#import "SRSDependencyBuilder.h"
@interface SRSDependencyBuilder : NSObject
@end
@implementation SRSDependencyBuilder
/** Stores creation rules */
static NSMutableDictionary *registry;
/**
* Add a new resolver to the registry array.
* @param string $name The id
* @param object $resolve Closure that creates instance
* @return void
*/
+(void) assign:(NSString *)key createBlock:( id (^)())block
{
registry[key] = block;
}
/**
* Create the instance
* @param string $name The id
* @return mixed
*/
+(id) resolve:(NSString *)key
{
if ( registry[key] )
{
id (^ createBlock)();
createBlock = registry[key];
return createBlock();
}
[NSException raise:@"Block doesn't exist" format:@"Can't resolve for key %@", key];
return nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment