EN: Assembly Service Registrar is a .NET library that provides automatic service registration for dependency injection containers using marker interfaces. This library simplifies the process of registering services by automatically scanning assemblies and registering implementations based on interface markers.
TR: Assembly Service Registrar, marker interface'ler kullanarak dependency injection container'ları için otomatik servis kaydı sağlayan bir .NET kütüphanesidir. Bu kütüphane, assembly'leri otomatik olarak tarayarak ve interface marker'larına göre implementation'ları kaydederek servis kayıt işlemini basitleştirir.
EN:
- Automatic Assembly Scanning: Scans assemblies and automatically registers services
- Marker Interface Pattern: Uses marker interfaces to determine service lifetimes
- Lifetime Management: Supports Singleton, Scoped, and Transient lifetimes
- Simple Integration: Easy integration with Microsoft.Extensions.DependencyInjection
- Convention-based Registration: Automatically matches interfaces with implementations
TR:
- Otomatik Assembly Tarama: Assembly'leri tarar ve servisleri otomatik olarak kaydeder
- Marker Interface Pattern: Servis lifetime'larını belirlemek için marker interface'ler kullanır
- Lifetime Yönetimi: Singleton, Scoped ve Transient lifetime'larını destekler
- Basit Entegrasyon: Microsoft.Extensions.DependencyInjection ile kolay entegrasyon
- Convention Tabanlı Kayıt: Interface'leri implementation'larla otomatik eşleştirir
- .NET 8.0 or higher | .NET 8.0 veya üzeri
- Microsoft.Extensions.DependencyInjection package
EN: Add the package to your project using one of the following methods:
TR: Aşağıdaki yöntemlerden birini kullanarak paketi projenize ekleyin:
dotnet add package AssemblyServiceRegistrarInstall-Package AssemblyServiceRegistrar<PackageReference Include="AssemblyServiceRegistrar" Version="1.0.0" />git clone https://github.com/sametbrr/assembly_service_registrar.git
cd assembly_service_registrar
dotnet buildEN: First, create your service interfaces by inheriting from the appropriate marker interfaces:
TR: Öncelikle, uygun marker interface'lerden türeterek servis interface'lerinizi oluşturun:
using AssemblyServiceRegistrar;
// For Singleton services | Singleton servisler için
public interface IConfigurationService : ISingletonService
{
string GetConnectionString();
}
// For Scoped services | Scoped servisler için
public interface IUserService : IScopedService
{
Task<User> GetUserAsync(int id);
Task CreateUserAsync(User user);
}
// For Transient services | Transient servisler için
public interface IEmailService : ITransientService
{
Task SendEmailAsync(string to, string subject, string body);
}// Singleton service implementation
public class ConfigurationService : IConfigurationService
{
public string GetConnectionString()
{
return "Server=localhost;Database=MyApp;";
}
}
// Scoped service implementation
public class UserService : IUserService
{
private readonly IRepository<User> _userRepository;
public UserService(IRepository<User> userRepository)
{
_userRepository = userRepository;
}
public async Task<User> GetUserAsync(int id)
{
return await _userRepository.GetByIdAsync(id);
}
public async Task CreateUserAsync(User user)
{
await _userRepository.AddAsync(user);
}
}
// Transient service implementation
public class EmailService : IEmailService
{
public async Task SendEmailAsync(string to, string subject, string body)
{
// Email sending logic
await Task.CompletedTask;
}
}using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using AssemblyServiceRegistrar;
using System.Reflection;
// Program.cs
var builder = Host.CreateDefaultBuilder(args);
builder.services.AddServicesFromAssembly(Assembly.GetExecutingAssembly());
var host = builder.Build();EN:
- The library scans all types in the specified assembly
- Finds all classes that implement interfaces derived from
IService - Determines the service lifetime based on marker interfaces:
ISingletonService→ServiceLifetime.SingletonIScopedService→ServiceLifetime.ScopedITransientService→ServiceLifetime.Transient- If no marker interface is found, uses the provided default lifetime
- Registers the service with the DI container
TR:
- Kütüphane belirtilen assembly'deki tüm tipleri tarar
IService'den türeyen interface'leri implement eden tüm class'ları bulur- Marker interface'lere göre servis lifetime'ını belirler:
ISingletonService→ServiceLifetime.SingletonIScopedService→ServiceLifetime.ScopedITransientService→ServiceLifetime.Transient- Eğer marker interface bulunamazsa, sağlanan varsayılan lifetime'ı kullanır
- Servisi DI container'a kaydeder
namespace AssemblyServiceRegistrar
{
// Base marker interface | Temel marker interface
public interface IService { }
// Scoped lifetime marker | Scoped lifetime marker'ı
public interface IScopedService : IService { }
// Singleton lifetime marker | Singleton lifetime marker'ı
public interface ISingletonService : IService { }
// Transient lifetime marker | Transient lifetime marker'ı
public interface ITransientService : IService { }
}EN:
- Use
ISingletonServicefor stateless services and configurations - Use
IScopedServicefor services that should be shared within a request scope - Use
ITransientServicefor lightweight, stateless services - Keep your service interfaces focused and follow the Single Responsibility Principle
TR:
- Stateless servisler ve konfigürasyonlar için
ISingletonServicekullanın - Request scope içinde paylaşılması gereken servisler için
IScopedServicekullanın - Hafif, stateless servisler için
ITransientServicekullanın - Servis interface'lerinizi odaklı tutun ve Single Responsibility Principle'ı takip edin
EN:
- Only works with interfaces that inherit from
IService - Does not support generic service registration
- Requires marker interfaces for lifetime determination
- One interface per implementation (no multiple interface implementations)
TR:
- Sadece
IService'den türeyen interface'lerle çalışır - Generic servis kayıtlarını desteklemez
- Lifetime belirleme için marker interface'ler gerektirir
- Her implementation için bir interface (çoklu interface implementation'ları desteklemez)
This project is licensed under the MIT License
Bu proje MIT Lisansı altında lisanslanmıştır