A micro-framework that leverages Swift Property Wrappers to implement the Service Locator pattern

Overview

Locatable

platforms pod Carthage compatible

Context

Locatable is a Swift micro framework that leverages Property Wrappers to implement the Service Locator pattern, through a custom attribute @Locatable.

Here's an example of how it can be used:

protocol Servicing {
    func action()
}

class Service: Servicing {
    func action() {
        print("I'm performing a service 😊")
    }
}

Locator.register(Servicing.self, { return Service() })

class MyController {
    @Locatable(.sharedInstance) var service: Servicing
    
    func work() {
        self.service.action()
    }
}

let controller = MyController()

controller.work() // I'm performing a service 😊

For convenience, some shorthand syntax are also available:

// leverages @autoclosure
Locator.register(Servicing.self, Service())

// leverages default argument values
Locator.register { return Service() as Servicing }

Service locating supports two distinct semantics:

// Will return an instance that is shared across the app
Locatable(.sharedInstance) var service: Servicing

// Will return a new instance every time
Locatable(.newInstance) var service: Servicing

Requirements

Xcode 11+ & Swift 5.1

Installation

CocoaPods

Add the following to your Podfile:

pod "Locatable"

Carthage

Add the following to your Cartfile:

github "vincent-pradeilles/locatable"

Author

You might also like...
An elegant, flexible, type-safe dependency resolution framework for Swift

Guise is an elegant, flexible, type-safe dependency resolution framework for Swift. Flexible dependency resolution, with optional caching Elegant, str

Container is a lightweight dependency injection framework for Swift.
Container is a lightweight dependency injection framework for Swift.

Container Container is a lightweight dependency injection framework for Swift. Installation is available in the Swift Package Manager. Swift Package M

Framework-level dependency graph generator for Xcode projects - with support for custom graphing
Framework-level dependency graph generator for Xcode projects - with support for custom graphing

XCGrapher xcgrapher is, by default, a framework-level dependency graph generator for Xcode projects. It works by reading local clones of the source, s

Alchemic - The most advanced iOS DI framework available

Alchemic By Derek Clarkson Alchemic is a Dependency Injection (DI) framework for iOS. It's goal is to help you manage object creation, object properti

DIContainer Swift is an ultra-light dependency injection container made to help developers to handle dependencies easily. It works with Swift 5.1 or above.

🏺 DIContainer Swift It is an ultra-light dependency injection container made to help developers to handle dependencies easily. We know that handle wi

Pilgrim - Dependency injection for Swift (iOS, OSX, Linux). Strongly typed, pure Swift successor to Typhoon.

pilgrim.ph Pilgrim is a dependency injection library for Swift with the following features: Minimal runtime-only library that works with pure Swift (s

Injector - A Swift package for simple dependency injection that also supports Swift UI previews

A Swift package for simple dependency injection that also supports Swift UI prev

Tranquillity is a lightweight but powerful dependency injection library for swift.
Tranquillity is a lightweight but powerful dependency injection library for swift.

DITranquillity Tranquillity is a lightweight but powerful dependency injection library for swift. The name "Tranquillity" laid the foundation in the b

Pure makes Pure DI easy in Swift.

Pure Pure makes Pure DI easy in Swift. This repository also introduces a way to do Pure DI in a Swift application. Table of Contents Background Pure D

Comments
  • Use auto closure and inferred type

    Use auto closure and inferred type

    On my phone so not ideal to make the PR myself but you should change to this:

    static func register<T>(_ type: T.Type = T.self, _ factory: @autoclosure @escaping () -> T) {
    

    This would allow the call site to either choose to use the autoclosure feature:

    Resolver.register( Servicing.self, Service() )
    

    Or to use the = T.self and inference and be able to write:

    Resolver.register { Service() as Servicing }
    
    opened by AliSoftware 1
  • Fixed a compilation error in 'Locatable'

    Fixed a compilation error in 'Locatable'

    Fixed a Swift 5 compilation error in 'Locatable' which was complaining of wrapper type 'Locatable' missing a non-static property named 'wrappedValue'. Renaming the Locatable.value property to Locatable.wrappedValue fixes it.

    opened by adil-hussain-84 0
Owner
Vincent Pradeilles
French iOS software engineer, working in Lyon, France 🇫🇷
Vincent Pradeilles
A library to inject your dependencies via property wrappers

?? DependencyInjection A library to inject your dependencies via property wrappers ?? Features DependencyInjection allows you to define the dependenci

Alberto Garcia 4 Dec 10, 2022
A simple way to handle dependency injection using property wrappers

Injektion Introduction A simple way to handle dependency injection using propert

Andrew McGee 2 May 31, 2022
Injection - Dependency injection using property wrappers

Dependency injection using property wrappers. Registering types: // injecting a

Alejandro Ramirez 3 Mar 14, 2022
Cleanse is a dependency injection framework for Swift.

Cleanse - Swift Dependency Injection Cleanse is a dependency injection framework for Swift. It is designed from the ground-up with developer experienc

Square 1.7k Dec 16, 2022
DIKit Dependency Injection Framework for Swift, inspired by KOIN.

DIKit Dependency Injection Framework for Swift, inspired by KOIN. Basically an implementation of service-locator pattern, living within the applicatio

null 95 Dec 22, 2022
Swinject is a lightweight dependency injection framework for Swift.

Swinject Swinject is a lightweight dependency injection framework for Swift. Dependency injection (DI) is a software design pattern that implements In

null 5.6k Dec 31, 2022
Dependency Injection framework for Swift (iOS/macOS/Linux)

Declarative, easy-to-use and safe Dependency Injection framework for Swift (iOS/macOS/Linux) Features Dependency declaration via property wrappers or

Scribd 684 Dec 12, 2022
Guise - An elegant, flexible, type-safe dependency resolution framework for Swift

Guise is an elegant, flexible, type-safe dependency resolution framework for Swift. Flexible dependency resolution, with optional caching Elegant, str

null 52 Oct 3, 2022
Needle - Compile-time safe Swift dependency injection framework

Needle is a dependency injection (DI) system for Swift. Unlike other DI frameworks, such as Cleanse, Swinject, Needle encourages hierarchical DI struc

Uber Open Source 1.4k Jan 3, 2023
CarbonGraph - A Swift dependency injection / lookup framework for iOS

CarbonGraph is a Swift dependency injection / lookup framework for iOS. You can

Baidu 244 Jan 4, 2023