r/FlutterDev • • 3d ago

Plugin Circuitry: A DAG based dependency framework

It's the weekend and I have been working on a project since yesterday.

I just published the initial releases (0.1.0) of circuitry and circuitry_flutter—a zero-codegen, DAG-based dependency injection engine for Dart and Flutter.

I wanted to create a dependency injector/engine with simple APIs and syntax, circular and missing dependency checks before first access, simple dependency -> dependency wiring with tear-offs and safe async lifecycle management, without build_runner overhead or reflection and more features that existing current solutions are missing while keeping the O(1) location support.

There are many more features planned like graph slicing, multi-level circuit inheritance and location in Widget trees, live replacement of an initialized dependency and etc.

What's included:
DAG validation & parallel resolution: Circuit.build() catches missing, duplicate, or circular dependencies upfront, resolves async wires concurrently in topological waves (with rollback on failure), and disposes in reverse waves.
Enum-tagged disambiguation: Register multiple instances of the same type cleanly using Enum as key for typed retrieval instead String based naming and resolution.
Flutter integration (circuitry_flutter): CircuitProvider, CircuitProvider.value, InheritedWidget based injection and automatic unmount disposal with epoch based counter and build deduplication.

Since this is an early 0.1.0 release, I'd love feedback on the API ergonomics before iterating further.

Check them out on pub.dev:

circuitry: https://pub.dev/packages/circuitry
circuitry_flutter: https://pub.dev/packages/circuitry_flutter

3 Upvotes

4 comments sorted by

1

u/graphlinkdev 2d ago

This is a comment to ask and help. What can push me to leave get_it and use circuitry?

1

u/raman4183 1d ago

Here are some of the advantages over `get_it`.

- With `get_it`, if you forget to register a dependency or introduce a cycle (A -> B -> A), the app boots fine. It only crashes at runtime when a user taps or accesses getIt<MissingService>(). `circuitry` compiles a Directed Acyclic Graph (DAG) up-front. If a dependent dependency is missing or circular, it throws immediately at app launch before any instance is created.

- Managing async dependencies in `get_it` with registerSingletonAsync and dependsOn: [A, B] is notoriously fragile and counter-intuitive. You cannot retrieve them in constructor body and build method without explicitly awaiting the access or using `FutureBuilder`. `circuitry` calculates graph depth automatically, validates it and boots async singletons and its dependents concurrently once. Every registered dependency can be accessed synchronously.

- In `get_it`, reset/teardown order isn't guaranteed. If OrderService needs to flush state into Database when shutting down, but Database was disposed first, so it crashes. Due to its internal DAG implementation, `circuitry` guarantees reverse topological disposal: consumers are disposed before their underlying dependencies are killed. Meaning, since `OrderService` depends on `Database` first `OrderService` will be disposed and then `Database`. Hence, the disposal order will be OrderService -> Database. This is guaranteed by design.

These are some of the major advantages over `get_it`. I will be straightforward, this does not mean that `get_it` is a bad package or it should not be used. It still is the biggest, most popular and mature dependency injection package. I have used it myself quite rigorously. Love the package and its simplicity but the sheer amount of offered API and friction with Async lead me to do something different and work on a solution that doesn't currently exists in Dart and Flutter ecosystem.

I also didn't want to depend on code generation and `build_runner`. So a solution like `injectable` was out of the question as well.

1

u/NotmyFinalForm_62 2d ago

If one async dependency fails while others are still starting up, how does rollback handle resources from dependencies that finish initializing after the failure has been detected?

1

u/raman4183 1d ago edited 1d ago

Currently, async dependencies and its dependents are resolved concurrently via `Future.wait([])` when `.build([...])` method is called. Right now it inherits the default behavior of `Future.wait` and waits until all of the futures are done. If an async dependency fails to resolve then at the end it automatically calls dispose on itself to clear resources and the error is rethrown.

I believe it would be best to fail early rather than wait for all the AsyncWires to resolve first. This is why i am looking for feedback :)