OneShot 3.0.1

dotnet add package OneShot --version 3.0.1
                    
NuGet\Install-Package OneShot -Version 3.0.1
                    
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="OneShot" Version="3.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OneShot" Version="3.0.1" />
                    
Directory.Packages.props
<PackageReference Include="OneShot" />
                    
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 OneShot --version 3.0.1
                    
#r "nuget: OneShot, 3.0.1"
                    
#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 OneShot@3.0.1
                    
#: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=OneShot&version=3.0.1
                    
Install as a Cake Addin
#tool nuget:?package=OneShot&version=3.0.1
                    
Install as a Cake Tool

NuGet Badge openupm

One Shot Dependency Injection

A single file DI container

Basic Concept of DI

Installation

  • Copy and paste OneShot.cs into your project.
  • [Unity only] or follow instructions on OpenUPM to install it as a package of Unity.
  • [.NET only] or follow instruction on NuGet to install it for .NET project.

Usage

Test Cases

Container

A scope mark for registered types.

// create new container
var container = new Container();

// create child container/scope
var child = container.CreateChildContainer();

// create a scope container (exactly same as child container)
using (var scope = container.BeginScope())

// container options
// enable/disable circular check, enabled by default
container.EnableCircularCheck = false;

// pre-allocate argument array of registered type, disabled by default
container.PreAllocateArgumentArrayOnRegister = true;

// throw on register disposable transient, disabled by default
container.PreventDisposableTransient = true;

Register Types

container.RegisterInstance<int>(10).AsSelf(); // register instance of int
container.Register<Foo>().Singleton().AsSelf(); // register a singleton of `Foo`
container.Register<Bar>().AsSelf(); // register transient of `Bar`
container.Register<Func<int>>((resolveContainer, contractType) => container.Resolve<Foo>().GetIntValue).AsSelf(); // register `Func<int>`
conatiner.Register<Foo>().As<IFoo>(); // register interface of `IFoo`
container.Register<Foo>().With(123, new Bar()).AsSelf(); // register with certain instances
container.Register(typeof(Generic<>), (_, type) => Activator.CreateInstance(type)).As(typeof(Generic<>)); // register generic type

Resolve

container.Resolve<int>();
container.Resolve<IFoo>();
container.Resolve<Generic<int>>();

InjectAttribute

class Foo
{
    public Foo() {}
    // mark a constructor to use on instantiate
    [Inject] public Foo(int value) {}

    [Inject] int IntValue; // field able to inject
    [Inject] float FloatValue { get; set; } // property albe to inject
    [Inject] void Init(int value) {} // method albe to inject
}

container.Register<Foo>().Singleton().AsSelf();
var foo = container.Resolve<Foo>(); // instantial `Foo` by `Foo(int value)`
container.InjectAll(foo); // inject its fields, properteis and methods

Label

class Foo {}
interface TypedLabelFoo : ILabel<Foo> {} // declare type-specific label, will throw on labeling other type
interface AnyLabel<T> : ILabel<T> {} // declare generic-typed label, works on any types

class Bar
{
    public Bar(
        Foo foo, // un-labeled type
        [Inject(typeof(TypedLabelFoo))] Foo labeledFoo, // labeled type with type-specific label
        [Inject(typeof(AnyLabel<>))] Foo anyLabeledFoo, // labeled type
        [Inject(typeof(AnyLabel<>))] int anyLabeledInt  // labeled type
    ) {}
    
    [Inject(typeof(AnyLabel<>))] Foo LabeledProperty { get; private set; }
    [Inject(typeof(FooLabel))] Foo LabeledField;
}

container.Register<Foo>().AsSelf(typeof(TypedLabel)); // register typed label foo
container.Register<Bar>().With((123, typeof(AnyLabel<>)), (new Foo(), typeof(AnyLabel<>))).AsSelf(); // register additional-labeled instances
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
3.0.1 208 8/22/2023
3.0.0 200 8/19/2023
2.8.2 181 8/18/2023
2.7.2 156 8/18/2023
2.6.2 428 10/4/2022
2.6.1 428 10/2/2022
2.6.0 425 10/1/2022
2.5.0 425 9/29/2022
2.4.0 409 9/29/2022
2.3.1 413 9/28/2022
2.3.0 433 9/27/2022
2.2.1 437 9/27/2022
2.2.0 799 8/25/2022
2.1.2 469 7/5/2022
2.1.1 485 7/4/2022
2.1.0 465 7/2/2022
2.0.4 449 6/28/2022
2.0.3 481 3/9/2022
2.0.2 481 3/4/2022
2.0.1 496 3/4/2022
2.0.0 504 2/24/2022