在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):Swinject/Swinject开源软件地址(OpenSource Url):https://github.com/Swinject/Swinject开源编程语言(OpenSource Language):Swift 93.9%开源软件介绍(OpenSource Introduction):SwinjectSwinject is a lightweight dependency injection framework for Swift. Dependency injection (DI) is a software design pattern that implements Inversion of Control (IoC) for resolving dependencies. In the pattern, Swinject helps your app split into loosely-coupled components, which can be developed, tested and maintained more easily. Swinject is powered by the Swift generic type system and first class functions to define dependencies of your app simply and fluently. Features
Extensions
Requirements
InstallationSwinject is available through Carthage, CocoaPods, or Swift Package Manager. CarthageTo install Swinject with Carthage, add the following line to your
Then run CocoaPodsTo install Swinject with CocoaPods, add the following lines to your source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0' # or platform :osx, '10.10' if your target is OS X.
use_frameworks!
pod 'Swinject'
# Uncomment if you use SwinjectStoryboard
# pod 'SwinjectStoryboard' Then run Swift Package Managerin dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/Swinject/Swinject.git", from: "2.8.0")
],
targets: [
.target(
name: "MyProject",
dependencies: [..., "Swinject"]
)
...
] Documentation
Basic UsageFirst, register a service and component pair to a let container = Container()
container.register(Animal.self) { _ in Cat(name: "Mimi") }
container.register(Person.self) { r in
PetOwner(pet: r.resolve(Animal.self)!)
} Then get an instance of a service from the container. The person is resolved to a pet owner, and playing with the cat named Mimi! let person = container.resolve(Person.self)!
person.play() // prints "I'm playing with Mimi." Where definitions of the protocols and classes are protocol Animal {
var name: String? { get }
}
class Cat: Animal {
let name: String?
init(name: String?) {
self.name = name
}
} and protocol Person {
func play()
}
class PetOwner: Person {
let pet: Animal
init(pet: Animal) {
self.pet = pet
}
func play() {
let name = pet.name ?? "someone"
print("I'm playing with \(name).")
}
} Notice that the Where to Register ServicesServices must be registered to a container before they are used. The typical registration approach will differ depending upon whether you are using The following view controller class is used in addition to the protocols and classes above in the examples below. class PersonViewController: UIViewController {
var person: Person?
} With SwinjectStoryboardImport SwinjectStoryboard at the top of your swift source file. import SwinjectStoryboard Services should be registered in an extension of extension SwinjectStoryboard {
@objc class func setup() {
defaultContainer.register(Animal.self) { _ in Cat(name: "Mimi") }
defaultContainer.register(Person.self) { r in
PetOwner(pet: r.resolve(Animal.self)!)
}
defaultContainer.register(PersonViewController.self) { r in
let controller = PersonViewController()
controller.person = r.resolve(Person.self)
return controller
}
}
} Without SwinjectStoryboardIf you do not use class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let container: Container = {
let container = Container()
container.register(Animal.self) { _ in Cat(name: "Mimi") }
container.register(Person.self) { r in
PetOwner(pet: r.resolve(Animal.self)!)
}
container.register(PersonViewController.self) { r in
let controller = PersonViewController()
controller.person = r.resolve(Person.self)
return controller
}
return container
}()
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
// Instantiate a window.
let window = UIWindow(frame: UIScreen.main.bounds)
window.makeKeyAndVisible()
self.window = window
// Instantiate the root view controller with dependencies injected by the container.
window.rootViewController = container.resolve(PersonViewController.self)
return true
}
} Notice that the example uses a convenience initializer taking a closure to register services to the new instance of Play in Playground!The project contains To run the playground in the project, first build the project, then select Example AppsSome example apps using Swinject can be found on GitHub. Blog PostsThe following blog posts introduce the concept of dependency injection and Swinject.
Thanks the authors! Contribution GuideA guide to submit issues, to ask general questions, or to open pull requests is here. Question?
CreditsThe DI container features of Swinject are inspired by: and highly inspired by:
LicenseMIT license. See the LICENSE file for details. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论