Skip to content

Commit

Permalink
Add SelfRegister option
Browse files Browse the repository at this point in the history
  • Loading branch information
enisn committed May 3, 2023
1 parent 157f9bc commit 7827d59
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
11 changes: 5 additions & 6 deletions src/DotNurse.Injector/DotNurseInjectorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ public class DotNurseInjectorOptions
/// </summary>
public Assembly Assembly { get; set; }

/// <summary>
/// Filter objects by name with given algorithm.
/// </summary>
[Obsolete("This property has a typo. Please use SelectImplementation property instead of this.")]
public Func<Type, bool> SelectImplementtion { get => SelectImplementation; set => SelectImplementation = value; }

/// <summary>
/// Filter objects by name with given algorithm.
/// </summary>
Expand All @@ -31,4 +25,9 @@ public class DotNurseInjectorOptions
/// Filters only objects which inherits directly from this type. For ex.: typeof(BaseRepository<>)
/// </summary>
public Type ImplementationBase { get; set; }

/// <summary>
/// Register the type as self too. True by default.
/// </summary>
public bool SelfRegister { get; set; } = true;
}
7 changes: 5 additions & 2 deletions src/DotNurse.Injector/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static IServiceCollection RegisterTypes(

foreach (var type in types)
{
if (type.GetCustomAttribute<IgnoreInjectionAttribute>() != null || type.GetCustomAttribute<DontRegisterAttribute>() != null)
if (type.GetCustomAttribute<DontRegisterAttribute>() != null)
continue;

if (!options.SelectImplementation(type))
Expand All @@ -94,7 +94,10 @@ public static IServiceCollection RegisterTypes(

var interfaces = type.GetInterfaces();

services.Add(new ServiceDescriptor(type, type, lifetime));
if (options.SelfRegister)
{
services.Add(new ServiceDescriptor(type, type, lifetime));
}

if (interfaces.Length == 1)
{
Expand Down

0 comments on commit 7827d59

Please sign in to comment.