SharpServiceCollection 0.0.4
dotnet add package SharpServiceCollection --version 0.0.4
NuGet\Install-Package SharpServiceCollection -Version 0.0.4
<PackageReference Include="SharpServiceCollection" Version="0.0.4" />
<PackageVersion Include="SharpServiceCollection" Version="0.0.4" />
<PackageReference Include="SharpServiceCollection" />
paket add SharpServiceCollection --version 0.0.4
#r "nuget: SharpServiceCollection, 0.0.4"
#addin nuget:?package=SharpServiceCollection&version=0.0.4
#tool nuget:?package=SharpServiceCollection&version=0.0.4
Branch | Status |
---|---|
main |
Installation
To install, run dotnet add package SharpServiceCollection
or
from Nuget
Introduction
SharpServiceCollection
is a lightweight C# library that wraps the `IServiceCollection interface to streamline
dependency injection through attribute-based assembly scanning.
Usage
SharpServiceCollection
scans an assembly to automatically register services in theIServiceCollection
container.Use one of the extension methods of
IServiceCollection
to perform assembly scanning.AddServicesFromCurrentAssembly()
AddServicesFromAssembly(Assembly assembly)
AddServicesFromAssemblyContaining<T>()
AddServicesFromAssemblyContaining(Type type)
InstanceLifetime
is an Enum with the valuesSingleton
Scoped
Transient
IServiceCollection
comes withAdd*
andTryAdd*
methods.SharpServiceCollection
offers the same functionality.SharpServiceCollection
will perform lexicographical sort on the class name to register dependency when duplicates occur. For example,Foo
Bar
Baz
will be sorted asBar
Baz
Foo
, If three of them are resolved byIDemoInterface
, NonTry*
based attributes will register the last one, which isFoo
,Try*
based attributes will register the first one, which isBar
In general, it is better to use
Try*
based attributes.
using SharpServiceCollection.Attributes;
using SharpServiceCollection.Enums;
// The class will be resolvable by the interface specified in the generic argument
[TryResolveBy<IScopedDemoService>(InstanceLifetime.Scoped)]
[ResolveBy<IScopedDemoService>(InstanceLifetime.Scoped)]
public class ScopedDemoType : IScopedDemoService, IKeyedScopedDemoService
{
}
// The class will be resolvable by the interface specified in the generic argument and the key
[KeyedTryResolveBy<IKeyedScopedDemoService>(InstanceLifetime.Scoped, "keyed")]
[KeyedResolveBy<IKeyedScopedDemoService>(InstanceLifetime.Scoped, "keyed")]
public class ScopedDemoType : IScopedDemoService, IKeyedScopedDemoService
{
}
// The class will be resolvable by itself
[TryResolveBySelf(InstanceLifetime.Scoped)]
[ResolveBySelf(InstanceLifetime.Scoped)]
public class ScopedDependency : IScopedDependency
{
}
// This will work by convention
// The class must have to implement an interface that has the same name of the class prefixed with I
[TryResolveByMatchingInterface(InstanceLifetime.Scoped)]
[ResolveByMatchingInterface(InstanceLifetime.Scoped)]
public class ScopedDependency : IScopedDependency
{
}
// The class will be resolvable by any of the implementeed interface
[TryResolveByImplementedInterface(InstanceLifetime.Scoped)]
[ResolveByImplementedInterface(InstanceLifetime.Scoped)]
public class FooBarBaz : IFoo, IBar, IBaz
{
}
Example
public interface IDemoService
{
string Greet();
}
using SharpServiceCollection.Attributes;
using SharpServiceCollection.Enums;
[TryResolveByImplementedInterface(InstanceLifetime.Scoped)]
public class DemoService : IDemoService
{
public string Greet()
{
return "Hello World!";
}
}
using SharpServiceCollection.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Current assembly will be scanned by Assembly.GetCallingAssembly()
builder.Services.AddServicesFromCurrentAssembly();
var app = builder.Build();
app.MapGet("/", (IDemoService demoService) => demoService.Greet());
app.Run();
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.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.0.4 | 104 | 4/6/2025 |