Swiftpack.co - Package - Digipolitan/dependency-injector-object-mapper

DependencyInjector+ObjectMapper

Swift Version Build Status CocoaPods Compatible Carthage Compatible Swift Package Manager Compatible Platform Twitter

Dependency injector Swift compatible with ObjectMapper

Installation

CocoaPods

To install DependencyInjector+ObjectMapper with CocoaPods, add the following lines to your Podfile.

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!

pod 'DependencyInjectorObjectMapper'

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate DependencyInjector+ObjectMapper into your Xcode project using Carthage, specify it in your Cartfile:

github 'Digipolitan/dependency-injector-object-mapper' ~> 1.0

Run carthage update to build the framework and drag the built DependencyInjectorObjectMapper.framework into your Xcode project.

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.

Once you have your Swift package set up, adding DependencyInjector+ObjectMapper as a dependency is as easy as adding it to the dependencies value of your Package.swift.

dependencies: [
    .package(url: "https://github.com/Digipolitan/dependency-injector-object-mapper.git", from: "1.0.0")
]

How to use ?

First you must create your models conforms to BaseMappable protocol

public protocol User: BaseMappable {
    var id: String { get set }
    var created: Date? { get set }
    var address: Address { get set }
}

public protocol Address: BaseMappable {
    var street: String { get set }
    var zipCode: String { get set }
    var country: String { get set }
}

After that, create the real implementation conforms to Mappable protocol foreach models

class UserModel: User, Mappable {
    public var id: String
    public var created: Date?
    public var address: Address

    public init(id: String, address: Address) {
        self.id = id
        self.address = address
    }

    public convenience required init?(map: Map) {
        guard
            let id: String = try? map.value("id"),
            let address: Address = try? map.injectedValue("address", type: Address.self) else {
                return nil
        }
        self.init(id: id, address: address)
    }

    public func mapping(map: Map) {
        self.id >>> map["id"]
        self.created <- (map["createdAt"], DateTransform())
        self.address >>> map.inject("address", type: Address.self)
    }
}

class AddressModel: Address, Mappable {
    public var street: String
    public var zipCode: String
    public var country: String

    public init(street: String, zipCode: String, country: String) {
        self.street = street
        self.zipCode = zipCode
        self.country = country
    }

    public convenience required init?(map: Map) {
        guard let street: String = try? map.value("street"), let zipCode: String = try? map.value("zipCode"), let country: String = try? map.value("country") else {
            return nil
        }
        self.init(street: street, zipCode: zipCode, country: country)
    }

    public func mapping(map: Map) {
        self.street >>> map["street"]
        self.zipCode >>> map["zipCode"]
        self.country >>> map["country"]
    }
}
  • For nonnull property set the value inside the initializer

  • To inject a custom implementation use map.injectedValue during the init process, or map.inject in the mapping function

After that, you must register implementation inside a module and push the module inside an injector

class DefaultModule: Module {

    override init() {
        super.init()
        self.bind(User.self).to(UserModel.self)
        self.bind(Address.self).to(AddressModel.self)
    }
}
Injector.default.register(module: DefaultModule(), with: "default")

Finally, inject the User model as follow

let user = try? Injector.default.inject(User.self, arguments: [
    "id": "1",
    "address": [
        "street": "abc",
        "zipCode": "75116",
        "country": "France"
    ]
])

Contributing

See CONTRIBUTING.md for more details!

This project adheres to the Contributor Covenant Code of Conduct. By participating, you are expected to uphold this code. Please report unacceptable behavior to contact@digipolitan.com.

License

DependencyInjector+ObjectMapper is licensed under the BSD 3-Clause license.

Github

link
Stars: 1
Help us keep the lights on

Used By

Total: 0

Releases

v1.0.6 - May 31, 2019

fix build error

v1.0.5 - May 30, 2019

swift 5 support

v1.0.4 - Dec 10, 2018

update swift 4.2

v1.0.3 - Nov 4, 2017

remove pod warning update readme fix spm4 update package.swift update travis remove swiftlint warning

v1.0.2 - Oct 2, 2017

swift 4 support & ObjectMappper 3.0+ & DependencyInjector 2.1