dino_generator 1.0.0-pre.2 copy "dino_generator: ^1.0.0-pre.2" to clipboard
dino_generator: ^1.0.0-pre.2 copied to clipboard

An optional static code generator for the dino library.

example/lib/main.dart

import 'package:dino/dino.dart';
import 'package:dino_extensions/dino_extensions.dart';

import 'main.dino.g.dart';

class DependencyA {}

class DependencyB {}

class DependencyC {}

abstract class TestService {
  void doSomething();
}

class TestServiceImpl implements TestService {
  final DependencyA dependencyA;
  final DependencyB dependencyB;
  final DependencyC dependencyC;

  TestServiceImpl(
    this.dependencyA,
    this.dependencyB,
    this.dependencyC,
  );

  @override
  void doSomething() {
    print('doSomething');
  }
}

void main() {
  // Create a service collection
  // The implementation of the service collection is generated
  //by the dino_generator based on subsequent method calls
  final ServiceCollection services = $ServiceCollection();

  // Create dependency A only once
  services.addInstance(DependencyA());

  // Create dependency B every time it is requested
  services.addTransientFactory((sp) => DependencyB());

  // Create TestService per a scope
  services.addScoped<TestServiceImpl>();

  // Add services via a module
  services.addModule(TestModule());

  // Create a root scope
  final rootScope = services.buildRootScope();

  // Create a nested scope
  final scope = rootScope.serviceProvider.createScope();

  // Resolve TestService from the nested scope
  final testService = scope.serviceProvider.getRequired<TestService>();

  testService.doSomething();
}

class TestModule extends Module {
  @override
  void configureServices(ServiceCollection services) {
    services.addSingleton<DependencyC>();
  }
}
0
likes
0
pub points
7%
popularity

Publisher

verified publisherexeteres.dev

An optional static code generator for the dino library.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

analyzer, build, code_builder, source_gen

More

Packages that depend on dino_generator