Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/BlazorServerExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
builder.Services.AddSingleton<WeatherForecastService>();

// Add EasyScrutor - Automatically scans and registers services
builder.Services.AddAdvancedDependencyInjection();
builder.Services.AddEasyScrutor();

var app = builder.Build();

Expand Down
2 changes: 1 addition & 1 deletion examples/ConsoleHostExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// Add EasyScrutor - Automatically scans and registers all services
// This will find and register IDataProcessor and IMetricsCollector without manual configuration
builder.Services.AddAdvancedDependencyInjection();
builder.Services.AddEasyScrutor();

builder.Services.AddHostedService<Worker>();

Expand Down
2 changes: 1 addition & 1 deletion examples/MvcExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
builder.Services.AddControllersWithViews();

// Add EasyScrutor - Automatically scans and registers services
builder.Services.AddAdvancedDependencyInjection();
builder.Services.AddEasyScrutor();

var app = builder.Build();

Expand Down
2 changes: 1 addition & 1 deletion examples/WebApiExample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
builder.Services.AddOpenApi();

// Add EasyScrutor - Automatically scans and registers services based on lifetime interfaces
builder.Services.AddAdvancedDependencyInjection();
builder.Services.AddEasyScrutor();

var app = builder.Build();

Expand Down
36 changes: 34 additions & 2 deletions src/EasyScrutor/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class ServiceCollectionExtensions {
/// </summary>
/// <param name="services">The service collection to add services to.</param>
/// <returns>The service collection for chaining.</returns>
public static IServiceCollection AddAdvancedDependencyInjection(this IServiceCollection services) {
public static IServiceCollection AddEasyScrutor(this IServiceCollection services) {
services.Scan(scan => scan
.FromDependencyContext(DependencyModel.DependencyContext.Default)
.AddClassesFromInterfaces());
Expand All @@ -30,14 +30,46 @@ public static IServiceCollection AddAdvancedDependencyInjection(this IServiceCol
/// <param name="services">The service collection to add services to.</param>
/// <param name="predicate">A predicate to filter which assemblies to scan.</param>
/// <returns>The service collection for chaining.</returns>
public static IServiceCollection AddAdvancedDependencyInjection(this IServiceCollection services, Func<Assembly, bool> predicate) {
public static IServiceCollection AddEasyScrutor(this IServiceCollection services, Func<Assembly, bool> predicate) {
services.Scan(scan => scan
.FromDependencyContext(DependencyModel.DependencyContext.Default, predicate)
.AddClassesFromInterfaces());

return services;
}

/// <summary>
/// Scans and registers services from assemblies whose names start with the specified prefix.
/// Services are registered based on their implemented lifetime interfaces (ISingletonLifetime, ITransientLifetime, IScopedLifetime).
/// </summary>
/// <param name="services">The service collection to add services to.</param>
/// <param name="prefix">The prefix that assembly names must start with.</param>
/// <returns>The service collection for chaining.</returns>
public static IServiceCollection AddEasyScrutorForAssembliesStartingWith(this IServiceCollection services, string prefix) {
services.Scan(scan => scan
.FromDependencyContext(DependencyModel.DependencyContext.Default, assembly =>
assembly.FullName != null && assembly.FullName.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
.AddClassesFromInterfaces());

return services;
}

/// <summary>
/// Scans and registers services from assemblies whose names contain the specified text.
/// Services are registered based on their implemented lifetime interfaces (ISingletonLifetime, ITransientLifetime, IScopedLifetime).
/// </summary>
/// <param name="services">The service collection to add services to.</param>
/// <param name="text">The text that assembly names must contain.</param>
/// <returns>The service collection for chaining.</returns>
public static IServiceCollection AddEasyScrutorForAssembliesContaining(this IServiceCollection services, string text) {
services.Scan(scan => scan
.FromDependencyContext(DependencyModel.DependencyContext.Default, assembly =>
assembly.FullName != null && assembly.FullName.Contains(text, StringComparison.OrdinalIgnoreCase))
.AddClassesFromInterfaces());

return services;
}

/// <summary>
/// Configures service registration based on lifetime marker interfaces.
/// Scans for classes implementing ISingletonLifetime, ITransientLifetime, IScopedLifetime, and their self-registration variants.
Expand Down
32 changes: 16 additions & 16 deletions tests/EasyScrutor.Tests/AdvancedDependencyInjectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ namespace EasyScrutor.Tests;
[TestFixture]
public class AdvancedDependencyInjectionTests {
[Test]
public void AddAdvancedDependencyInjection_WithoutPredicate_ShouldRegisterAllServices() {
public void AddEasyScrutor_WithoutPredicate_ShouldRegisterAllServices() {
// Arrange
var services = new ServiceCollection();

// Act
services.AddAdvancedDependencyInjection();
services.AddEasyScrutor();
var serviceProvider = services.BuildServiceProvider();

// Assert
Expand All @@ -24,13 +24,13 @@ public void AddAdvancedDependencyInjection_WithoutPredicate_ShouldRegisterAllSer
}

[Test]
public void AddAdvancedDependencyInjection_WithPredicate_ShouldOnlyRegisterMatchingAssemblies() {
public void AddEasyScrutor_WithPredicate_ShouldOnlyRegisterMatchingAssemblies() {
// Arrange
var services = new ServiceCollection();
var testAssemblyName = Assembly.GetExecutingAssembly().GetName().Name;

// Act
services.AddAdvancedDependencyInjection(assembly =>
services.AddEasyScrutor(assembly =>
assembly.FullName != null && assembly.FullName.Contains(testAssemblyName!));
var serviceProvider = services.BuildServiceProvider();

Expand All @@ -41,12 +41,12 @@ public void AddAdvancedDependencyInjection_WithPredicate_ShouldOnlyRegisterMatch
}

[Test]
public void AddAdvancedDependencyInjection_WithExcludingPredicate_ShouldNotRegisterFilteredAssemblies() {
public void AddEasyScrutor_WithExcludingPredicate_ShouldNotRegisterFilteredAssemblies() {
// Arrange
var services = new ServiceCollection();

// Act - Exclude assemblies containing "NonExistent"
services.AddAdvancedDependencyInjection(assembly =>
services.AddEasyScrutor(assembly =>
assembly.FullName != null && !assembly.FullName.Contains("NonExistent"));
var serviceProvider = services.BuildServiceProvider();

Expand All @@ -55,13 +55,13 @@ public void AddAdvancedDependencyInjection_WithExcludingPredicate_ShouldNotRegis
}

[Test]
public void AddAdvancedDependencyInjection_CalledMultipleTimes_ShouldSkipDuplicates() {
public void AddEasyScrutor_CalledMultipleTimes_ShouldSkipDuplicates() {
// Arrange
var services = new ServiceCollection();

// Act
services.AddAdvancedDependencyInjection();
services.AddAdvancedDependencyInjection();
services.AddEasyScrutor();
services.AddEasyScrutor();

// Assert - Should only have one registration due to Skip strategy
var singletonServices = services.Where(s => s.ServiceType == typeof(ISingletonService)).ToList();
Expand All @@ -74,7 +74,7 @@ public void ServiceRegistration_ShouldRespectRegistrationStrategy() {
var services = new ServiceCollection();

// Act
services.AddAdvancedDependencyInjection();
services.AddEasyScrutor();

// Try to add a manual registration - it should be skipped due to RegistrationStrategy.Skip
services.AddSingleton<ISingletonService, SingletonService>();
Expand All @@ -89,7 +89,7 @@ public void ServiceRegistration_ShouldRespectRegistrationStrategy() {
public void ComplexService_WithDependencies_ShouldResolveCorrectly() {
// Arrange
var services = new ServiceCollection();
services.AddAdvancedDependencyInjection();
services.AddEasyScrutor();
var serviceProvider = services.BuildServiceProvider();

// Act
Expand All @@ -110,7 +110,7 @@ public void ServiceCollection_ShouldReturnItself_ForChaining() {
var services = new ServiceCollection();

// Act
var result = services.AddAdvancedDependencyInjection();
var result = services.AddEasyScrutor();

// Assert
Assert.That(result, Is.SameAs(services), "Should return the same ServiceCollection for method chaining");
Expand All @@ -120,7 +120,7 @@ public void ServiceCollection_ShouldReturnItself_ForChaining() {
public void AllLifetimeInterfaces_ShouldBeRegistered() {
// Arrange
var services = new ServiceCollection();
services.AddAdvancedDependencyInjection();
services.AddEasyScrutor();
var serviceProvider = services.BuildServiceProvider();

// Act & Assert - Verify all 6 lifetime interfaces are working
Expand All @@ -138,7 +138,7 @@ public void AllLifetimeInterfaces_ShouldBeRegistered() {
public void MultipleImplementations_ShouldRegisterFirst() {
// Arrange
var services = new ServiceCollection();
services.AddAdvancedDependencyInjection();
services.AddEasyScrutor();
var serviceProvider = services.BuildServiceProvider();

// Act
Expand All @@ -154,7 +154,7 @@ public void MultipleImplementations_ShouldRegisterFirst() {
public void AbstractBaseClass_ShouldNotBeRegistered() {
// Arrange
var services = new ServiceCollection();
services.AddAdvancedDependencyInjection();
services.AddEasyScrutor();

// Act
var abstractRegistrations = services.Where(s => s.ImplementationType == typeof(BaseService)).ToList();
Expand All @@ -167,7 +167,7 @@ public void AbstractBaseClass_ShouldNotBeRegistered() {
public void ConcreteClassFromAbstract_ShouldBeRegistered() {
// Arrange
var services = new ServiceCollection();
services.AddAdvancedDependencyInjection();
services.AddEasyScrutor();
var serviceProvider = services.BuildServiceProvider();

// Act
Expand Down
Loading
Loading