Skip to content

Instantly share code, notes, and snippets.

@MelbourneDeveloper
Created May 21, 2022 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MelbourneDeveloper/299ec1e4d2f989bc19cc1cbb74be39ae to your computer and use it in GitHub Desktop.
Save MelbourneDeveloper/299ec1e4d2f989bc19cc1cbb74be39ae to your computer and use it in GitHub Desktop.
Dart Ioc Container
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