Skip to content

Commit

Permalink
Merge pull request #5 from Tim-Maes/feature/try-add-implementation
Browse files Browse the repository at this point in the history
Add support for TryAddScoped, TryAddTransient and TryAddSingleton
  • Loading branch information
Tim-Maes committed Oct 10, 2023
2 parents a0e0cd4 + 0c45886 commit b8679f5
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 15 deletions.
30 changes: 27 additions & 3 deletions README.md
Expand Up @@ -5,10 +5,23 @@ Easily register your services in .NET's built-in IoC container with attribute di
## Features 🌟

- Automatic registration of services using custom attributes.
- Supports Transient, Scoped, and Singleton service lifetimes.
- No need for explicit interface specification for class-only registrations.
- Provides clear visibility and reduces boilerplate code.
- Simple integration with the built-in .NET IoC container..
- Simple integration with the built-in .NET IoC container.

### Supported types
<center>

| Type | Supported|
|----------------|----------|
|AddTransient |✔️ |
|TryAddTransient |✔️ |
|AddScoped |✔️ |
|TryAddScoped |✔️ |
|AddSingleton |✔️ |
|TryAddSingleton |✔️ |
|TryAddEnumerable||
</center>

## Installation 📦

Expand All @@ -24,7 +37,9 @@ dotnet add package Bindicate
## Usage

### Add Bindicate
Update startup or service configuration and call the `AddBindicate()` method

Register Bindicate inside your startup class, or inside your project's `ServiceCollectionExtension`

```csharp
services.AddBindicate(Assembly.GetExecutingAssembly());
```
Expand All @@ -41,6 +56,15 @@ public class SimpleTaskRunner
// ...
}
}

[TryAddSingleton]
public class SimpleService
{
public void DoThing()
{
// ...
}
}
```

**When using interfaces:**
Expand Down
5 changes: 4 additions & 1 deletion src/Bindicate/Attributes/Lifetime.cs
Expand Up @@ -3,6 +3,9 @@
public enum Lifetime
{
Scoped,
TryAddScoped,
Transient,
TryAddTransient,
Singleton,
Transient
TryAddSingleton
}
10 changes: 10 additions & 0 deletions src/Bindicate/Attributes/Scoped/TryAddScopedAttribute.cs
@@ -0,0 +1,10 @@
namespace Bindicate.Attributes;

public class TryAddScopedAttribute : BaseServiceAttribute
{
public override Lifetime Lifetime => Lifetime.TryAddScoped;

public TryAddScopedAttribute() : base() { }

public TryAddScopedAttribute(Type serviceType) : base(serviceType) { }
}
10 changes: 10 additions & 0 deletions src/Bindicate/Attributes/Singleton/TryAddSingletonAttribute.cs
@@ -0,0 +1,10 @@
namespace Bindicate.Attributes;

public class TryAddSingletonAttribute : BaseServiceAttribute
{
public override Lifetime Lifetime => Lifetime.TryAddSingleton;

public TryAddSingletonAttribute() : base() { }

public TryAddSingletonAttribute(Type serviceType) : base(serviceType) { }
}
10 changes: 10 additions & 0 deletions src/Bindicate/Attributes/Transient/TryAddTransientAttribute.cs
@@ -0,0 +1,10 @@
namespace Bindicate.Attributes;

public class TryAddTransientAttribute : BaseServiceAttribute
{
public override Lifetime Lifetime => Lifetime.TryAddTransient;

public TryAddTransientAttribute() : base() { }

public TryAddTransientAttribute(Type serviceType) : base(serviceType) { }
}
6 changes: 3 additions & 3 deletions src/Bindicate/Bindicate.csproj
Expand Up @@ -6,14 +6,14 @@
<Nullable>enable</Nullable>
<Title>Bindicate</Title>
<Authors>Tim Maes</Authors>
<Description>Easily add services to DI container by adding a attribute</Description>
<Description>Easily add services to DI container by decorating your service with an attribute</Description>
<PackageProjectUrl>https://www.github.com/Tim-Maes/Bindicate</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://www.github.com/Tim-Maes/Bindicate</RepositoryUrl>
<PackageTags>di, ioc, service, collection, extensions, attribute</PackageTags>
<PackageReleaseNotes>Basic implemenation for Transient, Scoped and Singleton</PackageReleaseNotes>
<PackageReleaseNotes>Add TryAddScoped, TryAddTransient and TryAddSingleton</PackageReleaseNotes>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<Version>1.1.2</Version>
<Version>1.1.5</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
22 changes: 14 additions & 8 deletions src/Bindicate/Configuration/ServiceCollectionExtensions.cs
@@ -1,5 +1,6 @@
using Bindicate.Attributes;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System.Reflection;

namespace Bindicate.Configuration;
Expand All @@ -22,13 +23,22 @@ public static IServiceCollection AddBindicate(this IServiceCollection services,
switch (attr.Lifetime)
{
case Lifetime.Scoped:
RegisterService(services, serviceType, type, (s, t) => services.AddScoped(s, t));
RegisterService(serviceType, type, (s, t) => services.AddScoped(s, t));

Check warning on line 26 in src/Bindicate/Configuration/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Possible null reference argument for parameter 'implementationType' in 'IServiceCollection ServiceCollectionServiceExtensions.AddScoped(IServiceCollection services, Type serviceType, Type implementationType)'.

Check warning on line 26 in src/Bindicate/Configuration/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / run_test

Possible null reference argument for parameter 'implementationType' in 'IServiceCollection ServiceCollectionServiceExtensions.AddScoped(IServiceCollection services, Type serviceType, Type implementationType)'.
break;
case Lifetime.TryAddScoped:
RegisterService(serviceType, type, (s, t) => services.TryAddScoped(s, t));

Check warning on line 29 in src/Bindicate/Configuration/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Possible null reference argument for parameter 'implementationType' in 'void ServiceCollectionDescriptorExtensions.TryAddScoped(IServiceCollection collection, Type service, Type implementationType)'.

Check warning on line 29 in src/Bindicate/Configuration/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / run_test

Possible null reference argument for parameter 'implementationType' in 'void ServiceCollectionDescriptorExtensions.TryAddScoped(IServiceCollection collection, Type service, Type implementationType)'.
break;
case Lifetime.Singleton:
RegisterService(services, serviceType, type, (s, t) => services.AddSingleton(s, t));
RegisterService(serviceType, type, (s, t) => services.AddSingleton(s, t));

Check warning on line 32 in src/Bindicate/Configuration/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Possible null reference argument for parameter 'implementationType' in 'IServiceCollection ServiceCollectionServiceExtensions.AddSingleton(IServiceCollection services, Type serviceType, Type implementationType)'.

Check warning on line 32 in src/Bindicate/Configuration/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / run_test

Possible null reference argument for parameter 'implementationType' in 'IServiceCollection ServiceCollectionServiceExtensions.AddSingleton(IServiceCollection services, Type serviceType, Type implementationType)'.
break;
case Lifetime.TryAddSingleton:
RegisterService(serviceType, type, (s, t) => services.TryAddSingleton(s, t));

Check warning on line 35 in src/Bindicate/Configuration/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Possible null reference argument for parameter 'implementationType' in 'void ServiceCollectionDescriptorExtensions.TryAddSingleton(IServiceCollection collection, Type service, Type implementationType)'.

Check warning on line 35 in src/Bindicate/Configuration/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / run_test

Possible null reference argument for parameter 'implementationType' in 'void ServiceCollectionDescriptorExtensions.TryAddSingleton(IServiceCollection collection, Type service, Type implementationType)'.
break;
case Lifetime.Transient:
RegisterService(services, serviceType, type, (s, t) => services.AddTransient(s, t));
RegisterService(serviceType, type, (s, t) => services.AddTransient(s, t));

Check warning on line 38 in src/Bindicate/Configuration/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Possible null reference argument for parameter 'implementationType' in 'IServiceCollection ServiceCollectionServiceExtensions.AddTransient(IServiceCollection services, Type serviceType, Type implementationType)'.

Check warning on line 38 in src/Bindicate/Configuration/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / run_test

Possible null reference argument for parameter 'implementationType' in 'IServiceCollection ServiceCollectionServiceExtensions.AddTransient(IServiceCollection services, Type serviceType, Type implementationType)'.
break;
case Lifetime.TryAddTransient:
RegisterService(serviceType, type, (s, t) => services.TryAddTransient(s, t));

Check warning on line 41 in src/Bindicate/Configuration/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / create_nuget

Possible null reference argument for parameter 'implementationType' in 'void ServiceCollectionDescriptorExtensions.TryAddTransient(IServiceCollection collection, Type service, Type implementationType)'.

Check warning on line 41 in src/Bindicate/Configuration/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / run_test

Possible null reference argument for parameter 'implementationType' in 'void ServiceCollectionDescriptorExtensions.TryAddTransient(IServiceCollection collection, Type service, Type implementationType)'.
break;
default:
throw new ArgumentException($"Unsupported lifetime: {attr.Lifetime}");
Expand All @@ -44,15 +54,11 @@ public static IServiceCollection AddBindicate(this IServiceCollection services,
return services;
}

private static void RegisterService(IServiceCollection services, Type serviceType, Type implementationType, Action<Type, Type?> registrationMethod)
private static void RegisterService(Type serviceType, Type implementationType, Action<Type, Type?> registrationMethod)
{
if (serviceType == implementationType)
{
registrationMethod(implementationType, implementationType);
}
else
{
registrationMethod(serviceType, implementationType);
}
}
}

0 comments on commit b8679f5

Please sign in to comment.