DependencyContainer

1.0.2

insub4067/DependencyContainer

What's New

1.0.2

2023-12-03T03:48:19Z

📦 DependencyContainer

License: MIT Static Badge

What is DependencyContainer

Designed to ensure multiple objects rely on a single container to manage all dependencies.

✔️ Example

struct ContentView: View {
    
    let container: Container
    var a_service: A_Serviceable { container.resolve() }
    var b_service: B_Serviceable { container.resolve() }
    
    init(container: Container = .live) {
        self.container = container
    }
    
    var body: some View {
        Button("Fetch A Service") {
            a_service.fetch()
        }
        Button("Fetch B Service") {
            b_service.fetch()
        }
    }
}

extension Container {

    static let live = Container()
        .register { A_Service() as A_Serviceable }
        .register { B_Service() as B_Serviceable }
        
    static let fake = Container()
        .register { FakeA_Serivce() as A_Serviceable }
        .register { FakeB_Serivce() as B_Serviceable }
}
protocol A_Serviceable {
    func fetch()
}

protocol B_Serviceable {
    func fetch()
}
struct A_Service: A_Serviceable {
    func fetch() {
        print("A Fetch")
    }
}

struct B_Service: B_Serviceable {
    func fetch() {
        print("B Fetch")
    }
}
struct FakeA_Serivce: A_Serviceable {
    func fetch() -> String {
        "Fake A Fetch"
    }
}

struct FakeB_Serivce: B_Serviceable {
    func fetch() -> String {
        "Fake B Fetch"
    }
}

Description

  • Swift Tools 5.4.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sat Oct 19 2024 18:36:52 GMT-0900 (Hawaii-Aleutian Daylight Time)