Coder Social home page Coder Social logo

objective-c-dependency-injection's Introduction

DepenencyContext is an attempt to bring lightweight simple Dependency Injection to Objective C.

It currently only needs a dependencies file (plist), and the library including the SynthesizeSingleton.h class.

The DependencyContext class is a singleton that keeps a list of used Services, but only loads the implementation at first request.

At present only one instance per service will be instantiated. 
Support for other situations could be added later if it proves to be 
a requirement.

Usage: 

Like standard DI you will need to define a protocol (interface for the java minded) and one or more implementations.

Next you will need to define a dependencies file named dependencies.plist in your resources use the key column for your Service and the value as a String the implementation you wish to inject.

Include DependencyContext.h in any files that you wish to use access the dependencies for.

From here on in you can access any service using the [[DependencyContext sharedInstance] getDependency: __SERVICESTRING__] call.

Autowiring - can be improved: 

Do as above; then create a property in the header file you plan to use. 
IE: id<Service> servicePropertyName; 
Next define the property further as @property(getter=[get[Service], readonly) id<Service> servicePropertyName;

lastly in your implementation call the macro AUTO_WIRE([Service], @"[Service]") where service is the name of the service.

now you can use the injected service as a standard (readonly) property

objective-c-dependency-injection's People

Contributors

ranoble avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  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.