Dependency Injection and Reflection for Frameworks in NET Core

Dependency Injection and Reflection for Frameworks in NET Core

This article is based on learnings from the StarCraft II AI Framework, Abathur. It introduces a method for hiding dependency injection from the user of your framework and so might not be desirable in your case.

Please note before reading: This article is based on learnings from the StarCraft II AI Framework, Abathur. It introduces a method for hiding dependency injection from the user of your framework and so might not be desirable in your case.

One of the features that the framework _‘Abathur’ _includes is the ability to add and remove components without recompiling the code. This feature is implemented through dependency injection and utilizes reflection.

The framework is responsible for the overall flow of control and the user interact with it by adding ‘components’ through a simple _IModule _interface;

public interface IModule {
  void Initialize();
  void OnStart();
  void OnStep();
  void OnGameEnded();
  void OnRestart();
}

This approach is not unique to this AI framework — it can also be seen in developing web services in NET Core where the user defines a service and the framework handles routing and the overall flow of control.

Normal Dependency Injection

The user creates their own implementation of IModule so they can interact with the framework. In our case, we will use this minimalist — and somewhat irrelevant — implementation of IModule.

public class UserCreatedModule : IModule {
  void IModule.Initialize()
    => Console.WriteLine("Initalize | UserCreatedModule");
  void IModule.OnStart()
    => Console.WriteLine("Start | UserCreatedModule");
  void IModule.OnStep()
    => Console.WriteLine("Step | UserCreatedModule");
  void IModule.OnGameEnded()
    => Console.WriteLine("Game Ended | UserCreatedModule");
  void IModule.OnRestart()
    => Console.WriteLine("Restart | UserCreatedModule");
}

If this was a regular application, and all code was written by the user, the Dependency Injection would look something like this:

public Abathur(IModule module) {
  ...
}

IServiceProvider ConfigureServices() {
  ServiceCollection services = new ServiceCollection();
  services.AddScoped<IModule, UserCreatedModule>();
  services.AddSingleton<IAbathur, Abathur>();
  return services.BuildServiceProvider();
}

dependency-injection artificial-intelligence coding programming-languages net-core

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

jQuery Ajax CRUD in ASP.NET Core MVC with Modal Popup

In this article, we’ll discuss how to use jQuery Ajax for ASP.NET Core MVC CRUD Operations using Bootstrap Modal. With jQuery Ajax, we can make HTTP request to controller action methods without reloading the entire page, like a single page application.

Getting Started with ASP.NET Core Dependency Injection

I've lately been feeling my way around getting an actual, production-ready ASP.NET Core app developed, and one of the features I've really been digging (one of many) is native support for Dependency Injection (DI). DI is huge in modern web apps, so making it the default for ASP.NET

Dependency Injection in ASP.NET Core 3.1

ASP.NET Core is designed to support the dependency injection. Dependency injection in ASP.NET Core provides helps to create loosely coupled application. ASP.NET Core is designed from the ground up to support the dependency injection design pattern.

Getting Started with ASP.NET Core Dependency Injection

I've lately been feeling my way around getting an actual, production-ready ASP.NET Core app developed, and one of the features I've really been digging (one of many) is native support for Dependency Injection (DI). DI is huge in modern web apps, so making it the default for ASP.NET

MVC User Registration & Login with ASP.NET Core Identity

In this article, we will discuss the quickest way to use ASP.NET Core Identity for User Login and Registration in a new or existing MVC application.