Created
May 21, 2022 21:52
-
-
Save MelbourneDeveloper/299ec1e4d2f989bc19cc1cbb74be39ae to your computer and use it in GitHub Desktop.
Dart Ioc Container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class IocContainer { | |
final Map<Type, Object Function(IocContainer container)> _factories = {}; | |
IocContainer add<T>(T Function(IocContainer container) get) { | |
_factories.putIfAbsent( | |
typeOf<T>(), () => get as Object Function(IocContainer container)); | |
return this; | |
} | |
T get<T>() => _factories[typeOf<T>()]!(this) as T; | |
} | |
Type typeOf<T>() => T; | |
extension Extensions on IocContainer { | |
IocContainer addSingleton<T>(T service) => add((i) => service); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment