CodeIX.ServiceInjection 0.2.4-alpha

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

// Install CodeIX.ServiceInjection as a Cake Tool
#tool nuget:?package=CodeIX.ServiceInjection&version=0.2.4-alpha&prerelease                

Master Release NuGet

Service Injection Package

The CodeIX.ServiceInjection is a Roslyn-based source generator for .NET that facilitates automatic dependency injection into classes. It allows services and dependencies to be inserted into classes without manually initializing them in the constructor.

Features

  • Automatic Injection: Injects services and dependencies directly into your classes.
  • Configurable: Supports custom settings to control the injection process.
  • Easy to Use: Seamlessly integrates into the .NET build process.

Installation

Add the CodeIX.ServiceInjection NuGet package to your project:

dotnet add package CodeIX.ServiceInjection

Usage

To use the source generator, simply mark a class with the [ServiceInjection] attribute and mark the properties or fields you want to be injected with the [Injected] attribute:

using CodeIX.ServiceInjection;

[ServiceInjection]
public partial class MyClass
{
    [Injected(false)]
    public MyService MyServiceObject { get; set; }
}

The source generator automatically generates a partial class with the necessary constructor to inject MyService into MyClass, which will look like this:

partial class MyClass
{
    public MyClass(MyService MyServiceObject)
    {
        this.MyServiceObject = MyServiceObject ?? throw new ArgumentNullException(nameof(MyServiceObject));
    }
}

Examples

Basic Usage

ServiceInjection is possible for both properties and fields:

[ServiceInjection]
public partial class ExampleClass
{
    [Injected]
    private readonly Service1 _service1Field;

    [Injected]
    public Service2 Service2Property { get; set; }
}

Optional Dependencies

Dependencies can be marked as optional by setting the isOptional parameter of the [Injected] attribute to true:

[ServiceInjection]
public partial class ExampleClass
{
    [Injected]
    public Service1 Service1Property { get; set; }

    [Injected(isOptional: true)]
    public Service2 Service2Property { get; set; }
}

If Service1 is not registered, the ServiceProvider will throw an exception. If the optional Service2 is not registered in the service collection, the ServiceProvider will inject null into the property.

Setting the injected type

By default, the type of the injected property or field is used to determine the type of the injected service. If you want to inject a different type, you can set the injectedType parameter of the [Injected] attribute:

[ServiceInjection]
public partial class ExampleClass
{
	[Injected(injectedType: typeof(Service1))]
	public IService Service1Property { get; set; } // will inject Service1

	[Injected(injectedType: typeof(Service2))]
	public IService Service2Property { get; set; } // will inject Service2
}

Keep in mind that the injected type must be registered in the service collection by its inherited type.

Custom Constructor

You can still create a constructor for your class. The source generator will call it and inject the services afterwards:

[ServiceInjection]
public partial class ExampleClass
{
	public ExampleClass()
	{
        // do something
	}

	[Injected]
	public Service1 Service1Property { get; set; }
}

You can also create a constructor with parameters. The source generator will call it and inject the services afterwards:

[ServiceInjection]
public partial class ExampleClass
{
	public ExampleClass(Service1 service1Property)
	{
		// do something with service1Property before it is injected, like configuring it
	}

	[Injected]
	public Service1 Service1Property { get; set; }
}

Keep in mind that the injected services are injected after the constructor is called. This means that the injected services are not available in the constructor.

Also it's constrained by the ServiceProviders ability to resolve the constructor parameters. If the ServiceProvider can't resolve the constructor parameters, it will throw an exception.

List of Services

Of course you can also inject a list of services, which will give you all Services of the specified type:

[ServiceInjection]
public partial class ExampleClass
{
    [Injected]
    public IEnumerable<IService> ServiceProperty { get; set; }
}

License

This project is licensed under the MIT License.

Contributing

Contributions are welcome! Please read our contribution guidelines for more information.

Support

For questions or issues, please open an issue on GitHub.

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.
  • .NETStandard 2.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
0.2.5-alpha 180 11/25/2023
0.2.4-alpha 71 11/24/2023
0.2.3-alpha 78 11/24/2023
0.2.2-alpha 66 11/24/2023
0.2.1-alpha 66 11/24/2023
0.2.0-alpha 69 11/24/2023
0.1.6-alpha 72 11/24/2023
0.1.5-alpha 66 11/24/2023
0.1.4-alpha 63 11/23/2023
0.1.3-alpha 69 11/23/2023
0.1.2-alpha 69 11/23/2023
0.1.1-alpha 64 11/23/2023