DEV Community

Janki Mehta
Janki Mehta

Posted on

Using TinyIoC in ASP.NET Core

Dependency injection (DI) is a key technique used in ASP.NET Core to promote loose coupling between classes and increase flexibility. Instead of having classes create their own dependencies, ASP.NET Core allows dependencies to be "injected" into classes from the outside through constructor parameters.

The built-in ASP.NET Core dependency injection container works great for most scenarios. However, you may want a lightweight container for simple apps or where you need more control over container behavior. That's where TinyIoC comes in handy - it's an easy-to-use third-party IOC container perfect for ASP.NET Core.

This post will cover how to use TinyIoC as your dependency injection container in ASP.NET Core web applications.

Installing TinyIoC

First, install the TinyIoC NuGet package into your ASP.NET Core project:

dotnet add package TinyIoC
Enter fullscreen mode Exit fullscreen mode

Or via Package Manager:

Install-Package TinyIoC
Enter fullscreen mode Exit fullscreen mode

It will add a reference to TinyIoC and make it available for resolving dependencies.

Registering Services

Next, we need to register our services with TinyIoC so it knows what to inject where. This is done in the ConfigureServices method of ASP.NET Core's Startup class:

public void ConfigureServices(IServiceCollection services)
{
  // Create TinyIoC container
  var container = new TinyIoCContainer();

  // Register services
  container.Register<IExampleService, ExampleService>();

  // Build service provider
  services.AddSingleton<IServiceProvider>(container);

  // Other service registrations    
}
Enter fullscreen mode Exit fullscreen mode

Here we:

  • Create an instance of the TinyIoCContainer class
  • Register our services by specifying the interface and corresponding implementation
  • Set the container as the application's service provider

It makes our TinyIoC container fulfill the role of an ASP.NET Core service provider.

Injecting Dependencies

We can now have dependencies automatically injected using our TinyIoC container. For example:

public class MyController : Controller
{
   private readonly IExampleService _service;

   public MyController(IExampleService service)
   {
      _service = service; 
   } 
}
Enter fullscreen mode Exit fullscreen mode

TinyIoC will resolve the IExampleService interface to the registered ExampleService implementation and inject it.

Additional Registration Options

TinyIoC provides several useful methods for registering services:

Registering Multiple Implementations

container.Register<ILogger, ConsoleLogger>()
         .Register<ILogger, FileLogger>();
Enter fullscreen mode Exit fullscreen mode

This registers ConsoleLogger and FileLogger as ILogger implementations.

Registering as Singleton

container.Register<IExampleService, ExampleService>()
     .AsSingleton();
Enter fullscreen mode Exit fullscreen mode

This ensures only a single instance of ExampleService is used across the application.

Registering with Callback

container.Register<IExampleService, ExampleService>(
   x => x.WithParameter("debugMode", true));
Enter fullscreen mode Exit fullscreen mode

This allows passing parameters to the service constructor.

Resolving Services

In addition to constructor injection, services can be resolved directly from the container:

var service = container.Resolve<IExampleService>();
Enter fullscreen mode Exit fullscreen mode

It is useful for manually resolving dependencies in application code as needed.

That covers the key steps in using TinyIoC as a simple, lightweight dependency injection container in ASP.NET Core applications. The major points:

  • Install TinyIoC package
  • Register services in ConfigureServices
  • Set TinyIoC as a service provider
  • Leverage constructor injection
  • Directly resolve services from the container when needed

TinyIoC lets you enjoy the testability and flexibility of dependency injection without the complexity of full-featured containers. It's great for small to mid-sized applications where you want more control over the container behavior.

The full documentation can be found on GitHub at TinyIoC, which covers advanced usage and configuration options.

Let me know if you have any other questions about using TinyIoC or dependency injection in ASP.NET Core!

Top comments (0)