Search code examples
c#autofac

Registering types in a chain of classes


Let's say we have the following inheritance tree:

Base<Generic> : Base : IBase

And Have two kinds of implementations:

A) Class1Operation : Base<Generic>
   Class2Operation : Base<Generic>

B) Class3Operation : Base
   Class4Operation : Base

For point A I have something like this:

builder.RegisterAssemblyTypes(assembly)
        .Where(t => t.Name.EndsWith("Operation"))
        .AsClosedTypesOf(typeof(Base<>))
        .InstancePerLifetimeScope();

How Can i register types for point B so all of them (A and B) I could resolve by interface: IBase?

EDIT

I managed to do that by adding next registerAssemblyTypes method:

builder.RegisterAssemblyTypes(persistenceAssembly)
        .Where(t => t.Name.EndsWith("Operation"))
        .AssignableTo<Base>()
        .AsImplementedInterfaces()
        .InstancePerLifetimeScope();

and ... seems to resolve references in both examples.


Solution

  • That solved my problem:

    builder.RegisterAssemblyTypes(persistenceAssembly)
            .Where(t => t.Name.EndsWith("Operation"))
            .AssignableTo<Base>()
            .AsImplementedInterfaces()
            .InstancePerLifetimeScope();