Code Monkey home page Code Monkey logo

appleguice's Introduction

AppleGuice

Effortless dependency injection framework for Objective-C

Build Status Coverage Status

Who is using it?

Flashcards+ FlashCards+ CheggApp Chegg โ€“ Textbooks, eTextbooks & Study Tools

Are you using AppleGuice and your app is not on the list? Drop me a line.

What AppleGuice does for you?

AppleGuice helps you write clean, reuseable and testable code by allowing you to easily inject your services to any class. Other dependency injection frameworks require binding, xml editing or initializing your classes with a special method. With AppleGuice all you have to do is declare the injected type and thats it. As a bonus, you will still be able to initialize classes with [[MyClass alloc] init] so it is even easier to integrate it with your existing code base.

Show Me

Inject your first injectable instance with 3 simple steps:

Start AppleGuice

//AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Some code

[AppleGuice startService];

//More Code
}

Create your injectable service

Mark your injectable service with the protocol AppleGuiceInjectable so AppleGuice will find it.

@protocol MyServiceProtocol <AppleGuiceInjectable>

-(void) doStuff;

@end

@interface MyService : NSObject<MyServiceProtocol>
@end

@implementation MyService
...
@end

Enjoy automatic injection while coding

Create an ivar prefixed with the ioc prefix (the default is _ioc_). AppleGuice will automatically inject the proper implementation when calling the init method.

//MyClass.h
@interface MyClass : NSObject

@property (nonatomic, strong) id<MyServiceProtocol> ioc_myService;

@end

//MyClass.m
@implementation MyClass

//Now, you can use _ioc_myService anywhere. Even in the init function!

-(id) init {
  self = [super init];
  [self.ioc_myService doStuff];
  return self;
}
@end

AppleGuice initialized _ioc_myService without any manual binding!

Stub with ease while testing

#import <AppleGuice/AppleGuice.h>

@implementation MyClassTests {
    MyClass* classUnderTest;
}

-(void)setUp
{
    [super setUp];
    [AppleGuice startService];
    [AppleGuice setInstanceCreationPolicy:AppleGuiceInstanceCreationPolicyCreateMocks];
    classUnderTest = [[MyClass alloc] init];
}

-(void) test_foo_returnsValue {
  //the injectable ivar is initialized with a mock. You can stub methods on it as you normally do with OCMock.
  [[[classUnderTest.ioc_myService expect] andReturn:@"someString"] doStuff:OCMOCK_ANY];
  
  [classUnderTest foo];
  
  [classUnderTest.ioc_myService verify];
}

*When testing, AppleGuice works best with OCMock.

Inject In every flavour

Injecting a service is done by declering an ivar in a class. You can add it in the interface, implementation, as a property or even inside a private category. AppleGuice will find it. Injection comes in three flavours:

@interface MyClass () {
    MyService* _ioc_MyService; //will create an instance of MyService.
    id<MyServiceProtocol> _ioc_MyService //will create an instance of the first class conforming to MyServiceProtocol.
    NSArray* _ioc_MyProtocol //will return an array containing instances of all classes conforming to MyProtocol
}

More features

Singletons in a snap

Instead of messing your code with shared instance declerations or macros, you can just add AppleGuiceSingleton to the implemented protocols list and AppleGuice will always return the same instance.

@protocol MyServiceProtocol <AppleGuiceInjectable, AppleGuiceSingleton>
@end

Circular dependency support

AppleGuice can handle circular depenency between injected classes as long as the dependent classes conforms to AppleGuiceSingleton.

LazyLoad objects

You can configure AppleGuice to inject a proxy object instead of the real service. Once the service is needed (A method in the service is called) the proxy will be replaced with the real object.

//add in your AppDelegate
[AppleGuice setInstanceCreationPolicy:AppleGuiceInstanceCreationPolicyLazyLoad];

Ready to start?

Check out the quick installation guide.

Documentation can be found here. githalytics.com alpha

appleguice's People

Contributors

tomersh avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.