Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register all interface on assembly in Scrutor similar StructureMap

How to register all interface in assembly with scan extension without write all separated in ASP.NET Core 2?

in StructureMap:

Scan(_ =>
{
    // Declare which assemblies to scan
    _.Assembly("StructureMap.Testing"); 

});

in Scrutor:

collection.Scan(scan => scan
     // We start out with all types in the assembly of ITransientService
    .FromAssemblyOf<ITransientService>()
        // AddClasses starts out with all public, non-abstract types in this
        // assembly. These types are then filtered by the delegate passed to the
        // method. In this case, we filter out only the classes that are assignable
        // to ITransientService.
        .AddClasses(classes => classes.AssignableTo<ITransientService>())
            // We then specify what type we want to register these classes as.
            // In this case, we want to register the types as all of its implemented
            // interfaces. So if a type implements 3 interfaces; A, B, C, we'd end
            // up with three separate registrations.
            .AsImplementedInterfaces()
            // And lastly, we specify the lifetime of these registrations.
            .WithTransientLifetime()
        // Here we start again, with a new full set of classes from the assembly
        // above. This time, filtering out only the classes assignable to
        // IScopedService.
        .AddClasses(classes => classes.AssignableTo<IScopedService>())
            // Now, we just want to register these types as a single interface,
            // IScopedService.
            .As<IScopedService>()
            // And again, just specify the lifetime.
            .WithScopedLifetime());
like image 826
Mohammad Javad Avatar asked Dec 01 '25 22:12

Mohammad Javad


1 Answers

This will register all classes which implemented some interfaces as StructureMap does by default:

services.Scan(scan => scan
    .FromAssemblyOf<IService>()
    .AddClasses()
    .AsImplementedInterfaces()
    .WithTransientLifetime());
like image 62
Igor Goyda Avatar answered Dec 06 '25 15:12

Igor Goyda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!