Scrutor vs Autofac in C# - Dependency Injection Examples

Introduction

Dependency Injection (DI) is a fundamental concept in modern software development, promoting modular and testable code by decoupling dependencies. In C# projects, choosing the right DI library is crucial. Scrutor and Autofac are two popular options, each offering unique features and benefits. In this article, we'll compare Scrutor and Autofac through examples to help you understand their strengths and use cases.

Scrutor 

Simplifying Service Registration with Conventions. Scrutor is a lightweight DI library focused on simplifying assembly scanning and service registration. It provides a fluent API for convention-based registration, reducing manual configuration overhead.

Example

Let's say we have an interface IRepository<T> and its implementation Repository<T>

public interface IRepository<T>
{
    void Add(T item);
}

public class Repository<T> : IRepository<T>
{
    public void Add(T item)
    {
        // Implementation here
    }
}

With Scrutor, we can register all implementations of IRepository<T> automatically

services.Scan(scan => scan
    .FromAssemblyOf<IRepository>()
    .AddClasses(classes => classes.AssignableTo(typeof(IRepository<>)))
    .AsImplementedInterfaces()
    .WithTransientLifetime());

This code scans the assembly containing IRepository and registers all classes implementing IRepository<T> with their implemented interfaces.

Advantages

  1. Concise Configuration: Scrutor's fluent API allows for concise and expressive registration code.
  2. Convention-based Registration: It simplifies registration by applying conventions, and reducing manual configuration.
  3. Lightweight: Minimal dependencies make Scrutor suitable for lightweight projects.

Autofac

Advanced Dependency Injection with Extensive Features. Autofac is a feature-rich DI container offering advanced configuration options and support for various dependency management scenarios.

Example

 Let's extend the previous example by introducing a service UserService with a dependency on IRepository<User>:

public class UserService
{
    private readonly IRepository<User> _userRepository;

    public UserService(IRepository<User> userRepository)
    {
        _userRepository = userRepository;
    }

    // Other methods
}

With Autofac, we can register services and dependencies with advanced configuration

var builder = new ContainerBuilder();
builder.RegisterType<UserService>().AsSelf();
builder.RegisterType<Repository<User>>().As<IRepository<User>>();
var container = builder. Build();

This code registers UserService and Repository<User> with Autofac, specifying their relationships explicitly.

Advantages

  1. Advanced Configuration: Autofac provides extensive options for configuring services, including constructor injection and property injection.
  2. Lifetime Management: It supports different lifetime scopes for services, offering fine-grained control over object lifetimes.
  3. Module Registration: Autofac allows organizing registration logic into modules, enhancing modularity and maintainability.

Conclusion

Scrutor and Autofac offer distinct approaches to dependency injection in C# projects. Scrutor excels in simplicity and convention-based registration, ideal for lightweight applications. In contrast, Autofac provides advanced configuration options and comprehensive features for managing dependencies in complex scenarios. By understanding their strengths and examples, you can choose the right tool for your specific project requirements, balancing simplicity and flexibility effectively.


Similar Articles