Skip to content

Instantly share code, notes, and snippets.

@andreadottor
Last active May 20, 2022 21:47
Show Gist options
  • Save andreadottor/20d26f267c3d795258a23345fcce76da to your computer and use it in GitHub Desktop.
Save andreadottor/20d26f267c3d795258a23345fcce76da to your computer and use it in GitHub Desktop.
Esempio di utilizzo di scrutor
services.Scan(scan => scan
.FromCallingAssembly() // 1. Find the concrete classes
.AddClasses() // to register
.UsingRegistrationStrategy(RegistrationStrategy.Skip) // 2. Define how to handle duplicates
.AsSelf() // 2. Specify which services they are registered as
.WithTransientLifetime()); // 3. Set the lifetime for the services
// Registra tutte le classi, quando richiesta la loro interfaccia
services.Scan(scan =>
scan.FromCallingAssembly()
.AddClasses()
.AsMatchingInterface());
// registra solo le classi Service1 e Service2 come Transient
services.Scan(scan => scan
.AddTypes<Service1, Service2>()
.AsSelf()
.WithTransientLifetime());
// Registra tutte le classi che implementano IService, quando viene richiesta l'interfaccia che implementano
services.Scan(scan => scan
.FromAssemblyOf<IService>()
.AddClasses(classes => classes.AssignableTo<IService>())
.AsImplementedInterfaces()
.WithTransientLifetime());
// Registra tutte le classi che hanno il nome che termnina Repository e sono all'interno dell'assembly dove è presente l'interfaccia IService
services.Scan(scan => scan
.FromAssemblyOf<IService>()
.AddClasses(classes => classes.Where(type => type.Name.EndsWith("Repository")))
.AsImplementedInterfaces()
.WithTransientLifetime());
// Registro tutte le classi che hanno il nome che termina per Service, per la loro interfaccia, e come scoped
services.Scan(scan => scan
.FromAssemblyOf<OrderService>()
.AddClasses(classes => classes.Where(type => type.Name.EndsWith("Service")))
.AsImplementedInterfaces()
.WithScopedLifetime());
// Registro tutte le classi che implementano IAsyncRepository<T>, per la loro interfaccia, e come scoped
services.Scan(scan => scan
.FromAssemblyOf<OrderRepository>()
.AddClasses(classes => classes.AssignableTo(typeof(IAsyncRepository<>)))
.AsImplementedInterfaces()
.WithScopedLifetime());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment