FrenziedMarmot.DependencyInjection
1.0.0
See the version list below for details.
dotnet add package FrenziedMarmot.DependencyInjection --version 1.0.0
NuGet\Install-Package FrenziedMarmot.DependencyInjection -Version 1.0.0
<PackageReference Include="FrenziedMarmot.DependencyInjection" Version="1.0.0" />
paket add FrenziedMarmot.DependencyInjection --version 1.0.0
#r "nuget: FrenziedMarmot.DependencyInjection, 1.0.0"
// Install FrenziedMarmot.DependencyInjection as a Cake Addin
#addin nuget:?package=FrenziedMarmot.DependencyInjection&version=1.0.0
// Install FrenziedMarmot.DependencyInjection as a Cake Tool
#tool nuget:?package=FrenziedMarmot.DependencyInjection&version=1.0.0
FrenziedMarmot.DependencyInjection
The library, FrenziedMarmot.DependencyInjection
provides the ability to map dependency injection via attributes.
Purpose
By default, using dependency injection in C# requires that every time you have a new class to inject, you end up having to create it AND register it in the dependency injection system - usually in Startup.cs
. Using this library, you modify your Startup once and then each time you create a class you decorate it with the [Injectable]
attribute. This way, you define how something is injected with the class you're injecting. As long as the assembly that class is contained in is scanned, then it gets picked up and adding a class is one less step.
Installation
Install via nuget package manager or use dotnet.
dotnet add package FrenzedMarmot.DependencyInjection
Description
The library works by specifying injection via attributes AND finding those mappings by scanning for them.
Step 1: Specifying Injection
Using the constructor on the InjectableAttribute
you will be able to specify:
- What class/interface is being injected
- The service lifetime the injection lives for
and either:
- The implementation being injected
- The type acting as a factory for the injection
⚠️ If you provide both the implementation and a factory, the Factory will take precedence.
Step 2: Scanning for Injection Specified by Attributes
In Startup.cs when you're configuring dependency injection, utilize the extension method ScanForAttributeInjection
supplying a list of assemblies for it to scan. The method utilizes the params
keyword so multiple assemblies can be provided.
services.ScanForAttributeInjection(GetType().Assembly);
Example Attribute Usage
Specifying a concrete implementation as an interface, the following is the equivalent of coding services.AddScoped<IGreetingService, GreetingService>()
public interface IGreetingService
{
public string Greet();
}
[Injectable(typeof(IGreetingService), typeof(GreetingService), ServiceLifetime.Scoped)]
public class GreetingService : IGreetingService
{
public string Greet()
{
return $"{GetType().Name} was injected!";
}
}
If any parameter is left off, it uses the class it's attached to as an argument and ServiceLifetime.Scoped
is the default service lifetime.
For example, the attribute usage above for GreetingService
could be simplified to:
[Injectable(typeof(IGreetingService))]
public class GreetingService : IGreetingService
For example, the following is the same as services.AddScoped<MyClass>()
:
[Injectable]
public class MyClass
Property initializers can be used to set only a specific property. For example, the following is the same as services.AddSingleton<MyClass>()
:
[Injectable(Lifetime = ServiceLifetime.Singleton)]
public class MyClass
The Factory
property can be used to specify a factory class that will be called for the implementation. The factory class provided must implement IInjectableFactory
. For example, a factory attached to an injectable would look like:
[Injectable(Factory = typeof(MyClassFactory))]
public class MyClass
{
public MyClass(string someString)
{
//...
}
}
public class MyClassFactory : IInjectableFactory
{
public object Create(IServiceProvider serviceProvider)
{
return new MyClass("I was injected via Factory!");
}
}
Note: If you specify the same factory type twice, it will NOT inject the same instance to both. It will create 2 instances.
If you want to fuel our continued work: "Buy Me A Coffee"
Product | Versions 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 is compatible. |
.NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 is compatible. 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. |
-
.NETFramework 4.6.1
-
.NETFramework 4.7
-
.NETStandard 2.0
-
.NETStandard 2.1
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial Release
- Map dependency injection via attributes
- Define factories