ivlevastef / ditranquillity Goto Github PK
View Code? Open in Web Editor NEWDependency injection for iOS (Swift)
License: MIT License
Dependency injection for iOS (Swift)
License: MIT License
builder.register(Card).asSelf()
.initializer { (scope, type) in
if "gold" == type {
return GoldCard()
}
if "silver" == type {
return SilverCard()
}
return NormalCard()
}
.initializer { (scope) in return NormalCard() }
...
scope.resolve(Card, arg1: "gold") //GoldCard
scope.resolve(Card) //NormalCard
builder.register(Cat()) === builder.register(Cat.self).initializer { Cat() }
class MyClass {
private var dependency: IDependency { return lazyDependency.value }
private let lazyDependency: DILazy<IDependency>
init(dependency: DILazy<IDependency>) {
self.lazyDependency= dependency
}
func m() {
self.dependency.method()
}
}
class MyClass {
private var dependency: IDependency { return ownedDependency.value }
private let ownedDependency: DIOwned<IDependency>
init(dependency: DIOwned<IDependency>) {
self.ownedDependency= dependency
}
func m() {
self.dependency.method()
}
}
Instance Per Owned
For preventing multy register type.
Also replace Assembly on Module arhitecture
DIStoryboard : DIStoryboardBase
Obj-C!!
@interface DIStoryboard : UIStoryboard
@EnD
class A { init(B b) {} } // A requires B
class B { init(A a) {} } // B requires A
builder.register(A).asSelf().initializer {
(scope) in A(scope.resolve(B))
}
builder.register(B).asSelf().initializer {
(scope) in B(scope.resolve(A))
}
??? see typhoon definition.parent
resolve("name") + arg to resolve(Name: "name") + arg
for clarity
Chaos
VIPER
DelegateAndObservers
BigProject
Create DIViewController for override init methods for supported resolve object for ViewController.
Also supported warnings if parent UIViewController (not DIViewController)
also UINavigationController
builder.register(A1).asType(AProtocol)
builder.register(A2).asType(AProtocol)
builder.register(A3).asType(AProtocol)
do {
builder.build() // throw exception: A1,A2,A3 implementation one protocol AProtocol setup default
} catch {
}
Need supported multimap for checked current case
class MyClass {
private let dependency: IDependency
public init(dependency: IDependency) {
self.dependency = dependency
}
}
See Logger example.
changes:
resolveMany:
for rType in rTypes {
let pair = RTypeWithNamePair(rType, "")
if !recursive.contains(pair.uniqueKey) {
try result.append(resolveUseRType(scope, pair: pair , method: method))
}
}
class MyModule: Module {
func registate(builder: ComponentBuilder) {
}
}
builder.registrate(MyModule)
It's no initializer!
builder.register(Inject).dependency("logger", LoggerProtocol).dependency("service", ServiceProtocol)
builder.register(Inject).dependency{ (scope, inject) in
inject.logger = scope.resolve(LoggerProtocol)
inject.service = scope.resolve(ServiceProtocol)
}
Now singleton (and PerScope), not supported correct register/resolve with name
register(UIViewController).asName(pattern: "mainVC").initializer(return MainVC())
register(UIViewController).asName(pattern: "secondVC").initializer(return SecondVC())
class Inject {
setMain(mainVC: UIViewController) {..}//it's MainVC()
setSecond(secondVC: UIViewController) {...} //it's SecondVC()
}
...
inject.setMain(scope.resolve("mainVC"))
inject.setSecond(scope.resolve("secondVC"))
builder.registrate(Int).asName("*constName").initializer {_ in 20 }
builder.registrate(Array).asName("constName{Arr|Array}").initializer { _ in [1,2,3,4] }
let constName: Int
let constNameArr: [Int]
init(constName: Int, constNameArr: [Int], secondConstName: Int) {//20, [1,2,3,4], 20
self.constName = constName
self.constNameArr = constNameArr
}
let modificator = Modificator()
.asType(Logger.self)
.instanceSingle()
builder.register{ ConsoleLogger() }
.use(modificator: modificator)
builder.register{ FileLogger(file: "file.log") }
.use(modificator: modificator)
builder.register{ ServerLogger(server: "http://server.com/") }
.use(modificator: modificator)
builder.register{ MainLogger(loggers: **!$0) }
.use(modificator: modificator)
.asDefault()
this implementation incorrect
buider.registrate(Settings).asSelf().constructor(...).instancePerApp()
class Settings {
var userColor: String { didSet { save() } }
func init() {
userColor = "LoadFrom"
}
func save() {
"SaveTo" = userColor
}
}
builder.register(Cat, .Private)
need only for assembly
m() {
let thread1scope = ScopeFabric.createScope()
thread1scope.bindToThread() // scope bind to thread1
//or let thread1scope = ScopeFabric.createThreadScope() // scope bind to thread1
defer {
thread1scope.unbindFromThread() //scope unbind from thread 1
}
let threadScope = ScopeFabric.currentThreadScope() // return thread1scope
dispatch_async(dispatch_get_other, {
thread1scope.bindToThread() //scope bind to thread 2
m1()
thread1scope.unbindFromThread() //scope unbind from thread 2
})
m2()
}
m() {
let thread1scope = ScopeFabric.createScope()
thread1scope.bindToThread { (scope) in //bind to thread1
dispatch_async(dispatch_get_other, {
scope.bindToThread {
m1()
}
})
m2()
} //unbind from thread1
}
For DI Implicitly Unwrapped Optional better suited Normal Optional, because this property contains all the time value, but the value initialized after init method.
protocol logger {
register(...)
resolve(...)
warning(...)
error(...)
}
Single/ LazySingle
Для DIStoryboard можно будет указывать какие viewController (по идентификатору) нужно сохранять
replace objc_sync to spinlock
protocol DIBaseType {
func getBaseType() -> Any
}
extension AnyClass: DIBaseType {
}
extension Optional: DIBaseType {
}
extension ImplicityUnwrapperOptional: DIBaseType {
}
For example:
builder.registerAny("*Service").asOwn().asTypes("*ServiceProtocol").instancePerScope()
builder.registerAny(UIViewController)
.asName("firstVC", FirstViewController)
.asName("secondVC", SecondViewController)
.asType(ThirdViewController)
.instancePerDependency()
class MyModule: startupModule {
func registrate(builder: ComponentBuilder) {
}
}
added method performSegueWithIdentifier(identifier, sender:, arg:)
added method instantiateViewControllerWithIdentifier(identifier, arg:)
added method instantiateViewController(arg:) -> T (auto found identifier?)
supported by registration instancePerRequest(), method
perRequestParams{ (obj, arg1, arg2) in obj.arg1 = arg1 ... }
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.