InjectorWinUI 1.0.0

dotnet add package InjectorWinUI --version 1.0.0
NuGet\Install-Package InjectorWinUI -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="InjectorWinUI" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add InjectorWinUI --version 1.0.0
#r "nuget: InjectorWinUI, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install InjectorWinUI as a Cake Addin
#addin nuget:?package=InjectorWinUI&version=1.0.0

// Install InjectorWinUI as a Cake Tool
#tool nuget:?package=InjectorWinUI&version=1.0.0

Simple Dependency Injection Library for WinUI 3

This guide demonstrates how to implement a simple dependency injection (DI) container in a WinUI 3 project. The DI container allows for both singleton and transient instances, enabling flexible and maintainable dependency management.

Overview

We created:

  1. A DIContainer class to manage dependencies.
  2. Custom attributes [InjectSingle] and [Inject] to mark fields for dependency injection.
  3. A DIHelper class to handle the injection of dependencies.

DIContainer Class

The DIContainer class manages both singleton and transient dependencies. It supports registering factories for creating instances and resolving these instances when needed.

DIContainer.cs

using System;
using System.Collections.Generic;

public class DIContainer
{
    private readonly Dictionary<Type, Func<object>> _transientTypes = new();
    private readonly Dictionary<Type, object> _singletonInstances = new();
    private readonly Dictionary<Type, Type> _singletonTypes = new();

    public void RegisterTransient<T>(Func<T> factory) where T : class
    {
        _transientTypes[typeof(T)] = () => factory();
    }

    public void RegisterSingleton<T>(Func<T> factory) where T : class
    {
        _singletonTypes[typeof(T)] = typeof(T);
        _singletonInstances[typeof(T)] = factory();
    }

    public T Resolve<T>() where T : class
    {
        if (_singletonInstances.ContainsKey(typeof(T)))
        {
            return (T)_singletonInstances[typeof(T)];
        }

        if (_transientTypes.ContainsKey(typeof(T)))
        {
            return (T)_transientTypes[typeof(T)]();
        }

        throw new InvalidOperationException($"Type {typeof(T)} not registered.");
    }

    public object Resolve(Type type)
    {
        if (_singletonInstances.ContainsKey(type))
        {
            return _singletonInstances[type];
        }

        if (_transientTypes.ContainsKey(type))
        {
            return _transientTypes[type]();
        }

        throw new InvalidOperationException($"Type {type} not registered.");
    }
}

Example Usage in WinUI 3 Application

Here's how you can use the DI container and inject dependencies in your WinUI 3 application.


using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;

namespace YourNamespace
{
    public sealed partial class MainPage : Page
    {
        [InjectSingle]
        private YourSingletonService singletonService;

        [Inject]
        private YourTransientService transientService;

        public MainPage()
        {
            this.InitializeComponent();

            // Setup dependency injection container
            var container = new DIContainer();
            container.RegisterSingleton(() => new YourSingletonService());
            container.RegisterTransient(() => new YourTransientService());

            // Inject dependencies
            DIHelper.InjectDependencies(this, container);

            // Use the injected dependencies
            Initialize();
        }

        private void Initialize()
        {
            // Use the singletonService and transientService
        }
    }
}

Explanation

� DIContainer: Manages both singleton and transient dependencies. Supports registering factories and resolving instances. � Inject Attributes: Custom attributes [InjectSingle] and [Inject] mark fields for dependency injection. � DIHelper: Scans fields in a target object and injects dependencies based on the custom attributes. � Example Usage: Demonstrates how to set up the DI container, register dependencies, and inject them into a WinUI 3 page.

This approach provides a basic but functional dependency injection mechanism in your WinUI 3 project, allowing for better code organization and maintainability.

Product Compatible and additional computed target framework versions.
.NET net6.0-windows10.0.17763 is compatible.  net7.0-windows was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 33 6/27/2024