Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/vincent-pradeilles/locatable

A micro-framework that leverages Swift Property Wrappers to implement the Service Locator pattern
https://github.com/vincent-pradeilles/locatable

Last synced: about 1 month ago
JSON representation

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

Lists

README

        

# Locatable

![platforms](https://img.shields.io/badge/platforms-iOS-333333.svg)
![pod](https://img.shields.io/cocoapods/v/Locatable.svg)
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)

## 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:

```swift
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:

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

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

Service locating supports two distinct semantics:

```swift
// 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

* Twitter: [@v_pradeilles](https://twitter.com/v_pradeilles)