TinyNeedle 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package TinyNeedle --version 1.0.0
                    
NuGet\Install-Package TinyNeedle -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="TinyNeedle" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TinyNeedle" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="TinyNeedle" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TinyNeedle --version 1.0.0
                    
#r "nuget: TinyNeedle, 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.
#:package TinyNeedle@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TinyNeedle&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=TinyNeedle&version=1.0.0
                    
Install as a Cake Tool

💉 TinyNeedle

A super minimalistic but very capable Auto-DI framework for DotNet applications.

Usage

To create a container, create a new instance of a ServiceContainer class. This class lets you register new types and resolve type instances on the fly.

To register a new type, preferably call container.Register<MyService>() with an optional lifetime and constructor parameters.

To resolve a type manually, call container.Resolve<MyService>() or container.ResolveRequired<MyService>(). This method simply searches the container for registered types and returns a matching instance.

var root = new ServiceContainer()
    .Register<NetworkService>(Lifetime.Singleton)
    .Register<DatabaseService>(Lifetime.Scoped, new object[] { "my_db;user;password;" });

var network = root.Resolve<NetworkService>();
network.Initialize(); // => Calls Db.Connect();

To inject other services into a class, create a property (readonly or with setter) and give it a [Inject] attribute.

public class NetworkService
{
    [Inject] private DatabaseService Db { get; }
    
    public void Initialize() => Db.Connect();
}

It's also possible to automatically register types by adding the [Dependency(Lifetime = ...)] attribute to a class definition and calling container.AutoRegister()

Child containers are also supported. Create one by calling container.Scope() This returns a new ServiceContainer instance. When resolving types, the lifetime of a given type is crucial for where to search for available instances.

Lifetime

Currently, three different lifetimes are supported.

  1. Singleton: Types with this lifetime exist only once per container group.

    Container Parent registers a singleton, and scoped container Child tries to resolve this type. Child does not find a matching instance, but instead of creating a new one, the container recursively searches the type up the container tree. Parent has that type registered, but creates a new instance and adds it to its pool of instances because it does not yet have an available instance to return.

  2. Scoped: This lifetime is similar to a singleton, but is limited to scoped containers.

    Container Parent registers a scoped type and has an available instance ready. If Child resolves this type, it's the same as with singletons. However, if Parent does not have an instance of the required type, Child will instantiate that type and add the instance to its own pool of instances. This way, scoped instances will be isolated from each concurrent container.

  3. Transient: Transient types are always instantiated as soon as they're resolved (with one exception, see Circular Dependencies)

    A service container registers a transient type and resolves it. The resolution will not store the new instance in a list, but rather immediately returns it. If it's requested again, a new instance would be created.

Circular Dependencies

Different from other Auto-DI frameworks, TinyNeedle is capable enough to understand circular dependencies and handles them properly.

For example, if a container has a service registered, but that service has an injected reference to itself, TinyNeedle understands that the requested service was already instantiated before (one recursive call earlier) and uses this instance instead. This way, the resolution of the service will not crash, but rather create a successful circular reference. Behind the scenes, it would basically be a pointer that simply points to itself.

Fluent design by default

Most methods of the ServiceContainer return the container itself that allows for convenient method chaining. This makes especially registering new types manually a breeze. But keep it mind that there's also support for auto registering types!

var root = new ServiceContainer()
    .Register<NetworkService>()
    .Register<DatabaseService>()
    .Register<EmailService>()
    .Register<AuthenticationService>()
    .Register<HttpService>()...;
Product Compatible and additional computed target framework versions.
.NET 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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.1.0 220 8/11/2023
1.0.0 226 8/11/2023 1.0.0 is deprecated because it has critical bugs.