Spencer.NET 1.0.0

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

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

Spencer.NET

Open Source project of smart IoC Container made by programmer for programmer.

Is made with a view to creating class libraries.

Author

Kacper Faber, Poland

Requires

  • NET STANDARD 2.0 or newest

  • .NET CORE 2.0 or newest

  • .NET FRAMEWORK 4.61 or newest

Versions

  • 1.0 (29.04.2020)

Usage

Installing package

Spencer.NET is avaible on nuget.org.

You can use one of follow commands.

dotnet add package Spencer.NET

Install-Package Spencer.NET

Adding using

Spencer.NET using one statement for every feature.

It is it.

using Spencer.NET;

Create Container

Constructors of both of containers are too long to

writing it by contributor programmer.

You can use tested and safely ContainerFactory class

IContainer container = ContainerFactory.Container();

Create ReadOnlyContainer

Constructors of both of containers are too long to

writing it by contributor programmer.

You can use tested and safely ContainerFactory class

This container cannot be updated in him lifetime

You should provide instance of Storage class

Prefered way is using StorageBuilder

IReadOnlyContainer container = ContainerFactory.ReadOnlyContainer(storage);

Create Storage

ReadOnlyContainer cannot be updated in him lifetime.

To provides static registrations, you have to use instance of Storage class

Prefered way is using StorageBuilder class.

StorageBuilder.Build():IStorage

Returns Storage instance

StorageBuilder.Register<T>():StorageBuilder

Registering generic T as class or interface

StorageBuilder.Register(Type):StorageBuilder

Registering class or interface

StorageBuilder.Register<T>(params object[]):StorageBuilder

Registering generic T as class or interface with

given parameters to constructor

with compatible parameters

StorageBuilder.Register(Type, params object[]):StorageBuilder

Registering class or interface using constructor

with compatible parameters

StorageBuilder.RegisterObject(object):StorageBuilder

Registering class with instance gived in parameter.

Registration type will be taken from unboxed instance.

StorageBuilder.RegisterObject<T>(T):StorageBuilder

Registering class with instance of gived in parameter.

Registration type will be taken from T.

StorageBuilder.RegisterAssembly(Assembly):StorageBuilder

Registering assembly types.

StorageBuilder.RegisterAssembly(AssemblyName):StorageBuilder

Registering assembly types

StorageBuilder.RegisterAssemblies(params Assembly[]):StorageBuilder

Registering assembly types

StorageBuilder.RegisterAssemblies(params AssemblyName[]):StorageBuilder

Registering assembly types

Attributes
  • SingleInstance service will using single instance of class

  • MultiInstance service will create a new instance for each trying to get

  • AutoValue service will be instantiate on registering process

  • Exclude (Type) excluding type from registration.

  • ServiceConstructor pointing to default constructor for class, if any constructor will be find automatically

  • Factory pointing to static method, which will be used to instantiate new instance of class

  • FactoryResult (Type) expanding factory method for good type which actually is producing in this method

Troubleshooting
[SingleInstance]
[MultiInstance]
class Test
{
}

// Test will be registered as SingleInstance.
class Test
{
}

// Test will be registered as SingleInstance
// SingleInstance is default
class Test
{
}

// Test will be registered as not AutoValue,
// Class will be instantiate, on first trying to get
[MultiInstance]
class Test
{
}

Test instance = new Test();
IContainer container = ContainerFactory.Container();
container.RegisterObject(instance);

// Test will be registered as MultiInstace
// Given instance will be ever returns using Resolve methods.
class Test
{
    [Factory]
    public static Test FactoryMethod() => new Test();

    [ServiceConstructor]
    public Test()
    {
    }
}

// Factory always be checked first.
// In this situation Test will be instantiate using factory
Parametrized Constructors

Mechanism for finding constructor always will be picking accessible and

with the smallest count of parameters he have, or he could have.

You would use ServiceConstructor attribute?

If you want dynamically choose constructor you would to use,

i'm providing functionality to find constructor by given parameters.

Lets see the sample... :>

class Test
{
    // #1
    public Test(int x) {}
    
    // #2
    public Test(int x, int y) {}

    // #3 
    public Test(string str, bool b) {}
}

// Container.Register<Test>(0);
// Will invoke #1 constructor
// params
// x = 0

// Container.Register<Test>(1, 2);
// Will use #2 constructor
// params
// x = 1,
// y = 2

// Container.Register<Test>(true, "Hello World!");
// Will use #3 constructor
// params 
// str = "Hello World!"
// b = true
Factories

Factories is a static methods generating instance of a class.

They can returns interface, if they will have a FactoryResult(Type) attribute,

which will be pointing to the valid returns type.

They can take dependencies as their parameters

class Test 
{
    Test() {}
        
    [Factory]
    public static Test FactoryMethod() => new Test();
}
interface ITest 
{
}

class Test : ITest
{
    Test() {}
        
    [Factory]
    [FactoryResult(typeof(Test))
    public static ITest FactoryMethod() => new Test();
}
Injections

Spencer.NET want to help you. If you want, you cannot inject everything in constructor.

I am providing attributes Inject, TryInject and Auto.

Inject way is not recommended for good Dependency Injection

  • Injections

    • Inject trying to resolve instance from self Container,

    if could not found any matching will throw ResolveException

    • TryInject trying to resolve instance from self Container,

    if could not found any matching set variable to null

  • Auto initializing simple types

class OldWay
{
    public IEnumerable<int> Ints { get; set; }
    public IDep Dep { get; set; }
    
    public OldWay(IDep dep)
    {
        Dep = dep;
        Ints = new List<int>();
    }
}

class NewWay
{
    [Auto]
    public IEnumerable<int> Ints { get; set; }

    [Inject]
    public IDep Dep { get; set; } 
}
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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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.

This package has 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.3.0 164 7/25/2023
1.2.0 132 7/25/2023
1.1.29500 1,063 7/18/2020
1.1.3000 447 7/23/2020
1.1.2955 421 7/18/2020
1.1.2900 413 7/18/2020
1.1.2876 398 7/18/2020
1.1.2453 421 7/14/2020
1.0.75 455 6/16/2020
1.0.6 415 6/13/2020
1.0.5 471 6/12/2020
1.0.2.2 443 5/27/2020
1.0.2.1 408 5/26/2020
1.0.2 421 5/26/2020
1.0.1 445 4/30/2020
1.0.0 431 4/29/2020