FlexInject 1.0.37

There is a newer version of this package available.
See the version list below for details.
dotnet add package FlexInject --version 1.0.37                
NuGet\Install-Package FlexInject -Version 1.0.37                
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="FlexInject" Version="1.0.37" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FlexInject --version 1.0.37                
#r "nuget: FlexInject, 1.0.37"                
#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 FlexInject as a Cake Addin
#addin nuget:?package=FlexInject&version=1.0.37

// Install FlexInject as a Cake Tool
#tool nuget:?package=FlexInject&version=1.0.37                

FlexInject: Lightweight DI Container for .NET

Overview:

FlexInject is a lightweight, efficient, and flexible Dependency Injection container for .NET applications. It enables developers to manage the lifecycle of their objects, register dependencies, and resolve them at runtime while providing scope management.

Features:

  • Type Registration: Register types with optional names and tags.
  • Lifecycle Management: Support for Transient, Scoped, and Singleton lifecycles.
  • Attribute Injection: Inject dependencies using the InjectAttribute on fields and properties.
  • Custom Resolve Policies: Extend resolving capabilities using custom policies.
  • Scope Management: Create and manage scopes for resolving scoped instances.
  • Cyclic Dependency Detection: Detects cyclic dependencies and throws informative exceptions.
  • Initialization Interface: Optionally initialize resolved objects that implement the IInitialize interface.

Quick Start:

1. Create a FlexInject Container:
var container = new FlexInjectContainer();
2. Register Types:
container.Register<IService, ServiceImplementation>();
container.RegisterSingleton<ISingletonService, SingletonServiceImplementation>();
container.RegisterTransient<ITransientService, TransientServiceImplementation>();
container.RegisterScoped<IScopedService, ScopedServiceImplementation>();
3. Resolve Types:
var service = container.Resolve<IService>();
4. Create a Scope:
using (var scope = container.CreateScope())
{
    var scopedService = container.Resolve<IScopedService>();
}

Attribute Injection:

FlexInject supports attribute injection using the InjectAttribute, which can be applied to properties and fields. You can optionally specify name and tag via attributes.

public class MyClass
{
    [Inject(Name = "specialService")]
    private readonly IService _service;
    
    [Inject(Tag = "taggedService")]
    public IAnotherService AnotherService { get; set; }
}

Adding Resolve Policies:

You can extend the resolving capabilities of the container by implementing and adding custom IResolvePolicy.

public class MyResolvePolicy : IResolvePolicy
{
    public object Resolve(FlexInjectContainer container, Type type, string name, string tag)
    {
        // Custom resolve logic here.
    }
}

container.AddPolicy(new MyResolvePolicy());

Initialization:

Objects that implement the IInitialize interface will have their Initialize method called upon creation.

public class InitializableObject : IInitialize
{
    public void Initialize()
    {
        // Initialization logic here.
    }
}

Disposal:

Dispose of the container to release resources of IDisposable objects.

container.Dispose();

Documentation:

For more detailed information and advanced usage, please refer to the full documentation (https://github.com/vitkuz573/FlexInject/wiki).

Download:

FlexInject is open-source and available for download here.

Licensing:

FlexInject is licensed under the MIT License - see the LICENSE file for details.

Contributing:

We welcome contributions! Please see our contributing guidelines (https://github.com/vitkuz573/FlexInject/blob/main/CONTRIBUTING.md) for more details.

Issues:

If you encounter any issues or have feature requests, please open an issue here.

Conclusion:

FlexInject provides a lightweight and flexible solution for managing dependencies in .NET applications, allowing developers to focus on writing clean and maintainable code.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos 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.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.

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.39 150 10/1/2023
1.0.37 153 10/1/2023
1.0.35 144 10/1/2023
1.0.34 140 10/1/2023