BindingAttributes 0.1.1
dotnet add package BindingAttributes --version 0.1.1
NuGet\Install-Package BindingAttributes -Version 0.1.1
<PackageReference Include="BindingAttributes" Version="0.1.1" />
paket add BindingAttributes --version 0.1.1
#r "nuget: BindingAttributes, 0.1.1"
// Install BindingAttributes as a Cake Addin
#addin nuget:?package=BindingAttributes&version=0.1.1
// Install BindingAttributes as a Cake Tool
#tool nuget:?package=BindingAttributes&version=0.1.1
BindingAttributes
This library implements a number of helpful attributes for use with the Microsoft.Extensions.DependencyInjection
container.
A short guide: Bindings and Factories
Installation
There is no Nuget package available yet.
Installing with Paket
Add the following to your paket.dependencies
:
git https://github.com/dustinlacewell/BindingAttributes.git master build: "build.sh", OS: mono, Packages: /BindingAttributes/nupkg
git https://github.com/dustinlacewell/BindingAttributes.git master build: "build.bat", OS: windows, Packages: /BindingAttributes/nupkg
nuget BindingAttributes
Add the following to the paket.references
of any project that uses BindingAttributes:
BindingAttributes
Finally update your packages:
paket install
Quickstart
For any of the attributes within this package to work, you must configure your IServiceCollection
appropriately:
BindingAttribute.ConfigureBindings(services);
OptionsAttribute.ConfigureOptions(services, configuration);
Use the [Binding]
attribute on classes, to bind the class to itself.
[Binding]
public class Foo {}
Pass an interface, to bind the class an implementation of the interface:
[Binding(typeof(IFoo))]
public class Foo : IFoo {}
Pass a ServiceLifetime
:
[Binding(ServiceLifetime.Transient)]
public class Foo {}
Or use a specialized attribute:
[AsTransient]
public class Foo {}
The binding attributes work on public static methods to create factories bound to the return type. Method parameters are injected automatically (must be bound in the provider):
public class Foo {
public ILogger _logger;
public Foo(ILogger logger) { _logger = logger; }
[AsTransient]
public static Foo FooFactory(IServiceProvider sp, ILogger logger) {
return new Foo(logger);
}
}
You can bind factories to delegates as well:
public class Foo {
public ILogger _logger;
public int _bar;
public Foo(ILogger logger, int bar) { _logger = logger; _bar = bar; }
[Binding]
public static Func<int, Foo> FooFactory(IServiceProvider sp, ILogger logger) {
return bar => new Foo(logger, bar);
}
}
Use the [Options]
attribute to bind your class T
as IOptions<T>
to your IConfigurationRoot
:
[Options]
public class FooOptions {
public string Bar { get; }
}
Pass a configuration path to bind to a specific IConfiguration
sub-section:
[Options("Misc:Foo")]
public class FooOptions {
public string Bar { get; }
}
Attribute Overview
Service Lifetimes
By default the binding lifetimes are Singleton. Specify a particular scope by passing a ServiceLifetime
enumeration to [Binding(service_lifetime)]
:
- ServiceLifetime.Transient
- ServiceLifetime.Scoped
- ServiceLifetime.Singleton
Class Attributes
[Binding]
Bind the annotated type to itself with singleton lifetime.
[Binding( Type serviceType )]
Bind the annotated type to serviceType
with singleton lifetime.
[Binding( ServiceLifetime lifetime )]
Bind the annotated type to itself with lifetime
lifetime.
[Binding( Type serviceType, ServiceLifetime lifetime )]
Bind the annotated type to serviceType
with lifetime
lifetime.
[AsTransient]
Bind the annotated type to itself with transient lifetime.
[AsTransient( Type serviceType )]
Bind the annotated type to serviceType
with transient lifetime.
[AsScoped]
Bind the annotated type to itself with scoped lifetime.
[AsScoped( Type serviceType )]
Bind the annotated type to serviceType
with scoped lifetime.
[AsSingleton]
Bind the annotated type to itself with singleton lifetime.
[AsSingleton( Type serviceType )]
Bind the annotated type to serviceType
with singleton lifetime.
[Options]
Bind the annotated type as T
in IOptions<T>
against the IConfigurationRoot
.
[Options( string subSectionName )]
Bind the annotated type as T
in IOptions<T>
against the named IConfiguration
subsection.
Method Attributes
[Binding]
Bind the annotated method as a factory closure to its return type with singleton lifetime.
[Binding( Type serviceType )]
Bind the annotated method as a factory closure to serviceType
with singleton lifetime.
[Binding( ServiceLifetime lifetime )]
Bind the annotated method as a factory closure to its return type with lifetime
lifetime.
[Binding( Type serviceType, BindType lifetime )]
Bind the annotated method as a factory closure to serviceType
with lifetime
lifetime.
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 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. |
-
.NETStandard 2.0
- Microsoft.Extensions.DependencyInjection (>= 2.2.0)
- Microsoft.Extensions.Options (>= 2.2.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 2.2.0)
- Microsoft.Extensions.Options.DataAnnotations (>= 2.2.0)
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.1.1 | 640 | 9/11/2019 |