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.
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.
Install-Package Ch0pstix.InjectX
Install-Package Ch0pstix.InjectX.Mvvm
dotnet add package Ch0pstix.InjectX
dotnet add package Ch0pstix.InjectX.Mvvm
Name | Description |
---|---|
RegistrationStrategy | Specifies strategies that may be applied when adding a ServiceDescriptor to an IServiceCollection . |
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. |
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 . |
using MyConsoleApp.Services;
var services = new ServiceCollection()
.RegisterApplicationServices() // from MyConsoleApp.Services
.BuildServiceProvider();
var service = services.GetRequiredService<IExampleService>();
service.SayHello();
[Transient] // Defaults to transient anyways when not provided.
public class ExampleService : IExampleService
{
public void SayHello()
{
Console.WriteLine("Hello World");
}
}
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);
}
}
[Transient] // Override the default singleton lifetime (for views inheriting Window) since dialogs tend to be reused
public partial class MyCustomDialogWindow : Window
{
public MyCustomDialogWindow()
{
InitializeComponent();
}
}
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.