Skip to content

A high-performance, zero-configuration dependency injection library for .NET 9+ that automatically discovers and registers services using attributes, with advanced features like conditional registration, fluent configuration, caching, and plugin extensibility.

License

Notifications You must be signed in to change notification settings

furkansarikaya/FS.AutoServiceDiscovery

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

FS.AutoServiceDiscovery.Extensions

NuGet Version NuGet Downloads GitHub License GitHub Stars

A powerful, convention-based automatic service discovery and registration library for .NET 9.0 applications. This library transforms manual dependency injection configuration into an intelligent, attribute-driven system that can discover, validate, and register services automatically while maintaining full control and flexibility.

πŸš€ Quick Start

Transform your service registration from manual configuration to intelligent automation:

// 1. Mark your services with attributes
[ServiceRegistration(ServiceLifetime.Scoped)]
public class UserService : IUserService
{
    public async Task<User> GetUserAsync(int id)
    {
        return new User { Id = id, Name = "Sample User" };
    }
}

// 2. Enable automatic discovery in Program.cs
var builder = WebApplication.CreateBuilder(args);

// Single line to automatically discover and register all services
builder.Services.AddAutoServices();

var app = builder.Build();

That's it! Your IUserService is now automatically discovered, registered, and ready for dependency injection.

🎯 Core Features

Convention-Based Discovery

Automatically discover services using intelligent naming conventions and attributes:

[ServiceRegistration(ServiceLifetime.Scoped)]
public class ProductService : IProductService { }

[ServiceRegistration(ServiceLifetime.Singleton)]
public class CacheService : ICacheService { }

Environment-Aware Registration

Register different implementations based on environment:

[ServiceRegistration(ServiceLifetime.Scoped, Profile = "Development")]
public class MockEmailService : IEmailService { }

[ServiceRegistration(ServiceLifetime.Scoped, Profile = "Production")]
public class SmtpEmailService : IEmailService { }

Expression-Based Conditional Registration

Use powerful, type-safe conditional logic:

[ConditionalService(ctx => 
    ctx.Environment.IsProduction() && 
    ctx.FeatureEnabled("AdvancedLogging") &&
    !ctx.Configuration.GetValue<bool>("MaintenanceMode"))]
[ServiceRegistration(ServiceLifetime.Scoped)]
public class AdvancedLoggingService : ILoggingService { }

Fluent Configuration API

Build complex configurations with readable, chainable syntax:

builder.Services.ConfigureAutoServices()
    .FromCurrentDomain(assembly => !assembly.FullName.StartsWith("System"))
    .WithProfile(ctx => ctx.Environment.IsDevelopment() ? "Dev" : "Prod")
    .When(ctx => ctx.FeatureEnabled("AutoDiscovery"))
    .ExcludeNamespaces("MyApp.Internal.*", "MyApp.Testing.*")
    .WithPerformanceOptimizations()
    .Apply();

High-Performance Discovery

Optimized for production with advanced caching and parallel processing:

builder.Services.AddAutoServicesWithPerformanceOptimizations(options =>
{
    options.EnableParallelProcessing = true;
    options.EnablePerformanceMetrics = true;
    options.MaxDegreeOfParallelism = 4;
});

πŸ—οΈ Architecture Overview

graph TB
    A[Service Classes] --> B[Assembly Scanner]
    B --> C[Attribute Processor]
    C --> D[Naming Convention Resolver]
    D --> E[Conditional Evaluator]
    E --> F[Plugin Coordinator]
    F --> G[Performance Cache]
    G --> H[Service Registration]
    
    I[Configuration] --> E
    J[Environment Context] --> E
    K[Feature Flags] --> E
    L[Custom Plugins] --> F
    
    style A fill:#e1f5fe
    style H fill:#c8e6c9
    style G fill:#fff3e0
    style F fill:#f3e5f5
Loading

The library follows a sophisticated pipeline architecture where each component has a specific responsibility:

  • Assembly Scanner: Efficiently discovers service candidates using reflection
  • Attribute Processor: Interprets registration attributes and metadata
  • Naming Convention Resolver: Applies intelligent interface-to-implementation mapping
  • Conditional Evaluator: Processes environment and configuration-based conditions
  • Plugin Coordinator: Manages extensible discovery strategies
  • Performance Cache: Optimizes repeated discovery operations
  • Service Registration: Final registration with dependency injection container

πŸ“š Documentation

Core Concepts

Advanced Features

Architecture & Extensibility

πŸ› οΈ Installation

dotnet add package FS.AutoServiceDiscovery.Extensions

Requirements:

  • .NET 9.0 or later
  • Microsoft.Extensions.DependencyInjection 9.0.0+

🎨 Usage Patterns

Basic Service Registration

[ServiceRegistration(ServiceLifetime.Scoped)]
public class UserService : IUserService
{
    // Automatically registered as IUserService -> UserService
}

Profile-Based Environment Configuration

[ServiceRegistration(ServiceLifetime.Scoped, Profile = "Development")]
public class DevUserService : IUserService { }

[ServiceRegistration(ServiceLifetime.Scoped, Profile = "Production")]
public class ProdUserService : IUserService { }

Complex Conditional Logic

[ConditionalService(ctx => 
    ctx.Environment.IsProduction() && 
    ctx.Configuration.GetValue<bool>("Features:EnableCaching") &&
    ctx.EvaluateCustomCondition("DatabaseHealthy"))]
[ServiceRegistration(ServiceLifetime.Singleton)]
public class RedisCacheService : ICacheService { }

High-Performance Discovery

// Optimized for large applications
builder.Services.AddAutoServicesWithPerformanceOptimizations(options =>
{
    options.EnableParallelProcessing = true;
    options.EnablePerformanceMetrics = true;
    options.EnableLogging = false; // Reduce overhead in production
});

// Get performance statistics
var stats = PerformanceServiceCollectionExtensions.GetCacheStatistics();
Console.WriteLine($"Cache Hit Ratio: {stats.HitRatio:F1}%");

πŸ”§ Configuration Options

Basic Configuration

builder.Services.AddAutoServices(options =>
{
    options.Profile = builder.Environment.EnvironmentName;
    options.Configuration = builder.Configuration;
    options.EnableLogging = true;
    options.IsTestEnvironment = false;
});

Advanced Performance Configuration

builder.Services.AddAutoServicesWithPerformanceOptimizations(options =>
{
    options.EnableParallelProcessing = true;
    options.MaxDegreeOfParallelism = Environment.ProcessorCount;
    options.EnablePerformanceMetrics = true;
    options.EnableLogging = false;
});

Fluent Configuration

builder.Services.ConfigureAutoServices()
    .FromAssemblies(Assembly.GetExecutingAssembly())
    .WithProfile("Production")
    .When(ctx => ctx.FeatureEnabled("AutoDiscovery"))
    .ExcludeTypes(type => type.Name.EndsWith("Test"))
    .WithDefaultLifetime(ServiceLifetime.Scoped)
    .WithPerformanceOptimizations()
    .Apply();

πŸ“Š Performance Characteristics

Feature Impact Best Use Case
Basic Discovery ~10-50ms Small to medium applications
Cached Discovery ~1-5ms Repeated discovery operations
Parallel Processing 2-4x faster Large applications (100+ services)
Plugin System Variable Complex discovery requirements

🀝 Contributing

We welcome contributions! This project is open source and benefits from community involvement:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Development Guidelines:

  • Follow existing code patterns and conventions
  • Add comprehensive tests for new features
  • Update documentation for any public API changes
  • Ensure backward compatibility when possible

πŸ“„ License

This project is licensed under the MIT License. See the LICENSE file for details.

Made with ❀️ by Furkan Sarıkaya

For detailed documentation, advanced usage patterns, and architectural insights, explore the comprehensive guides in our documentation directory

About

A high-performance, zero-configuration dependency injection library for .NET 9+ that automatically discovers and registers services using attributes, with advanced features like conditional registration, fluent configuration, caching, and plugin extensibility.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages