Skip to content
This repository has been archived by the owner on May 19, 2024. It is now read-only.

Ch0pstix/InjectX

Repository files navigation

InjectX Banner Build Workflow Status - Master CodeQL Workflow Status - Master

What is InjectX? 🔎

The InjectX and InjectX.Mvvm libraries were developed with the aim of simplifying the implementation of dependency injection in your .NET applications. They achieve this by eliminating the need to register services, and in the case of InjectX.Mvvm, views and viewmodels, individually with the service container.

This is made possible by adding a few helpful extensions to the IServiceCollection class from Microsoft's dependency injection library, Microsoft.Extensions.DependencyInjection.

Getting started 🚀

Note: The dev prefix Ch0pstix.* is added to the package IDs as a means of avoiding naming conflicts with some unlisted packages in the NuGet catalog. This prefix is not present in the namespaces of the actual libraries.

The packages may be installed via nuget, package manager console, or dotnet cli.

NuGet

Package Manager Console

Install-Package Ch0pstix.InjectX
Install-Package Ch0pstix.InjectX.Mvvm

DotNet CLI

dotnet add package Ch0pstix.InjectX
dotnet add package Ch0pstix.InjectX.Mvvm

What's included 📂

Types

Name Description
RegistrationStrategy Specifies strategies that may be applied when adding a ServiceDescriptor to an IServiceCollection.

Extensions

Method Description
RegisterApplicationServices Registers service objects that have been defined within the application's assembly.
RegisterAssemblyServices Registers service objects that have been defined within the specified assembly.
RegisterViewsAndViewModels Registers view and viewmodel objects that have been defined within the application's assembly.

Attributes

Name Description
SingletonAttribute Specifies that a view or service should be registered with a ServiceLifetime of ServiceLifetime.Singleton.
TransientAttribute Specifies that a view or service should be registered with a ServiceLifetime of ServiceLifetime.Transient.
ScopedAttribute Specifies that a service should be registered with a ServiceLifetime of ServiceLifetime.Scoped.

Usage examples 🪄

Console App (InjectX)

Setting up the application service provider:

using MyConsoleApp.Services;

var services = new ServiceCollection()
  .RegisterApplicationServices() // from MyConsoleApp.Services
  .BuildServiceProvider();

var service = services.GetRequiredService<IExampleService>();

service.SayHello();

Setting a service's lifetime via lifetime annotation:

[Transient] // Defaults to transient anyways when not provided.
public class ExampleService : IExampleService
{
  public void SayHello()
  {
    Console.WriteLine("Hello World");
  }
}

Wpf Mvvm App (InjectX + InjectX.Mvvm)

Setting up the application service provider:

public partial class App : Application
{
  protected override void OnStartup(StartupEventArgs e)
  {
    var services = new ServiceCollection()
      .RegisterApplicationServices()  // from MyWpfApp.Services
      .RegisterViewsAndViewModels() // from MyWpfApp.Views && MyWpfApp.ViewModels
      .BuildServiceProvider();
      
    var mainWindow = services.GetRequiredService<MainWindow>();
    
    mainWindow.Show();
    
    base.OnStartup(e);
  }
}

Overriding a view's lifetime via lifetime annotation:

[Transient] // Override the default singleton lifetime (for views inheriting Window) since dialogs tend to be reused
public partial class MyCustomDialogWindow : Window
{
  public MyCustomDialogWindow()
  {
    InitializeComponent();
  }
}

License 📄

InjectX and related libraries are free and open source software under the MIT License. This license permits the modification, distribution, and private and commercial use of this software. Keep in mind that you must include a copy of this license in your projects.

View LICENSE.txt