DependencyInjectorObjectMapper

1.0.6

Dependency injector Swift support ObjectMapper
Digipolitan/dependency-injector-object-mapper

What's New

1.0.6

2019-05-31T11:52:22Z

fix build error

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.

Description

  • Swift Tools 4.0.0
View More Packages from this Author

Dependencies

Last updated: Tue Mar 19 2024 03:54:13 GMT-0900 (Hawaii-Aleutian Daylight Time)