Skip to content

Does transient instances support disposing? #14

Description

@zupat

Correct me if I'm wrong... I don't think this is supported but this snippet in your docs implies that it is.

final builder = IocContainerBuilder()
    ..add((container) => DatabaseConnection('my-connection-string'))
    ..add<UserRepository>(
      (container) => UserRepository(container<DatabaseConnection>()),
      dispose: (userRepository) => userRepository.dispose(),
    )

Based on the dispose extension, only scoped singletons are disposed. Therefore the dispose function in the above example will never be called.

Future<void> dispose() async {
assert(isScoped, 'Only dispose scoped containers');
for (final type in singletons.keys) {
//Note: we don't need to check if the service is a singleton because
//singleton service definitions never have dispose
final serviceDefinition = serviceDefinitionsByType[type]!;
//We can't do a null check here because if a Dart issue
serviceDefinition._dispose.call(singletons[type]);
await serviceDefinition._disposeAsync(singletons[type]);
}
singletons.clear();
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions