needle 0.0.1-dev.5 copy "needle: ^0.0.1-dev.5" to clipboard
needle: ^0.0.1-dev.5 copied to clipboard

Needle is a dependency injection library for Dart/Flutter inspired by Autofac ( https://autofac.org/ ).

example/lib/main.dart

import 'dart:math';

import 'package:needle/needle.dart';

import 'main.needle.dart';

@ReflectInclude(AnotherService)
@needle
class Builder extends $Builder {}

void main() {
  final builder = Builder();
  builder.setLogger((message) => print(message));

  builder.registerInstance(SomeService(5));

  builder.registerFactory((container) => AnotherService(
        someField: 5,
        anotherField: 6,
      ));

  builder.registerType<FooRepository>().singleInstance();

  builder
      .registerType<FooDataStore>()
      .withConstructor('large')
      .singleInstance();

  builder
      .registerType<BarDataStore>()
      .singleInstance()
      .withParameters({'size': 6});

  builder.registerType<BarRepository>().singleInstance();

  builder
      .registerType<RepositoryModelImpl>()
      .as<RepositoryModel>()
      .singleInstance()
      .withParameters(
          {'fooCache': const Named('Foo'), 'barCache': const Named('Bar')});

  builder.registerType<ObjectCache>().singleInstance().withName('Foo');
  builder.registerType<ObjectCache>().singleInstance().withName('Bar');

  final container = builder.build();

  final repositoryModel = container.resolve<RepositoryModel>();

  final barCache = container.resolve<ObjectCache>(name: 'Bar');
  final fooCache = container.resolve<ObjectCache>(name: 'Foo');

  final repositoryModelCopy = container.resolve<RepositoryModel>();

  print(repositoryModel);
  print(barCache);
  print(fooCache);
  print(repositoryModelCopy);
}

@reflect
class ObjectCache {
  final cache = <dynamic>[];
  final id = Random().nextInt(1000);
}

@reflect
class BarDataStore {
  BarDataStore(@Named('Bar') this.cache, this.size);

  final ObjectCache cache;
  final int size;
}

@reflect
class BarRepository {
  BarRepository(this._dataStore);

  final BarDataStore _dataStore;

  void test() {
    print(_dataStore.size);
  }
}

@reflect
class FooDataStore {
  FooDataStore(@Named('Foo') this.cache, this.size);
  FooDataStore.large(@Named('Foo') this.cache) : size = 10;
  FooDataStore.small(@Named('Foo') this.cache) : size = 2;

  final ObjectCache cache;
  final int size;
}

@reflect
class FooRepository {
  FooRepository(this._dataStore);

  final FooDataStore _dataStore;

  void test() {
    print(_dataStore.size);
  }
}

abstract class RepositoryModel {}

@reflect
class RepositoryModelImpl implements RepositoryModel {
  RepositoryModelImpl(
      {this.fooRepository, this.barRepository, this.fooCache, this.barCache});

  final FooRepository fooRepository;
  final BarRepository barRepository;
  final ObjectCache fooCache;
  final ObjectCache barCache;
}

@reflect
class SomeService {
  SomeService(this.someField);

  final int someField;
}

class AnotherService {
  AnotherService({this.someField, this.anotherField});

  final int someField;
  final int anotherField;
}

class AgentDataStore {
  AgentDataStore();
}

class AgentRepository {
  AgentRepository({
    this.dataStore,
    this.objectCache,
  });

  final AgentDataStore dataStore;
  final ObjectCache objectCache;
}

class CustomerDataStore {
  CustomerDataStore();
}

class CustomerRepository {
  CustomerRepository({
    this.dataStore,
    this.objectCache,
  });

  final CustomerDataStore dataStore;
  final ObjectCache objectCache;
}

class SalesAccountBloc {
  SalesAccountBloc({
    this.agentRepository,
    this.customerRepository,
  });

  final AgentRepository agentRepository;
  final CustomerRepository customerRepository;
}
1
likes
40
pub points
0%
popularity

Publisher

unverified uploader

Needle is a dependency injection library for Dart/Flutter inspired by Autofac ( https://autofac.org/ ).

Repository (GitHub)
View/report issues

License

MIT (LICENSE)

Dependencies

analyzer, build, dart_style, freezed_annotation, glob, source_gen

More

Packages that depend on needle