Conclusion - Building a Simple Social Feed using DDD Clean Architecture

In previous articles, we have been building a simple social feed using DDD Clean Architecture. In this article, we will conclude the series by implementing Dependency Injection and wrapping up the project.

Source code on Github

The source code for Circle is available on Github. You can find it here

Dependency Injection

Dependency injection is a programming technique where the dependencies required by a component are provided by an external source. In other words, instead of creating dependencies within a component, the component is given access to external dependencies.

Dependency Injection

In the above image, the injection.dart where get_it is initialized is the external source. The get_it package is used to implement dependency injection in this project. It is a simple service locator for Dart and Flutter projects. It allows you to access your classes from anywhere in your project.

import 'package:get_it/get_it.dart';
import 'package:injectable/injectable.dart';
import 'injection.config.dart';

final GetIt getIt = GetIt.instance;


void configureDependencies() {
  getIt.init();
}

We also have the injection.config.dart file where we register our dependencies. The @injectableInit annotation is used to generate the injection.config.dart file. The @module annotation is used to register a class as a dependency. The @lazySingleton annotation is used to register a class as a singleton. The @preResolve annotation is used to register a class as a singleton and initialize it immediately.

The injection.config.dart file is generated by running the following command:

flutter pub run build_runner build --delete-conflicting-outputs

We then call the configureDependencies function in the main.dart file.

Future<void> main() async {
  //Preserve the native splash screen
  WidgetsBinding widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
  FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);

  //Set the orientation of the application to portrait
  await SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);

  //Configure injectable dependencies necessary for the application
  configureDependencies();

  //Initialize Hive (for local storage)
  await Hive.initFlutter();
  registerAdapters();

  runApp(const App());
}

Now, we can access our dependencies anywhere in the project. Here is an example of how we can access the PostsBloc in the App widget.

  class App extends StatelessWidget {
  const App({super.key});

  
  Widget build(BuildContext context) {
    FlutterNativeSplash.remove();

    return BlocProvider(
      create: (context) => getIt<PostsBloc>()..add(GetPosts()),
      child: MaterialApp(
        title: 'Circle',
        debugShowCheckedModeBanner: false,
        theme: themeData,
        home: const Scaffold(
          appBar: AppAppBar(),
          body: HomeScreen(),
        ),
      ),
    );
  }
}

The above code snippet shows how we can access the PostsBloc in the App widget. We use the getIt instance to access the PostsBloc and add the GetPosts event to the PostsBloc.

Wrapping up

In this article, we have concluded the series by implementing Dependency Injection and wrapping up the project. We have also seen how we can access our dependencies anywhere in the project.

Conclusion

In this series, we have built a simple social feed using DDD Clean Architecture. We have seen how we can implement the different layers of DDD Clean Architecture and shown how to apply Separation of Concerns and Single Responsibility Principle. We have also seen how we can implement Dependency Injection in our project.

References

Source code on Github

The source code for Circle is available on Github. You can find it here

Mastodon