In Castle, I used to do the following to register types from a different assembly:
Classes.FromAssemblyNamed("MyServer.DAL")
.Where(type => type.Name.EndsWith("Repository"))
.WithServiceAllInterfaces()
.LifestylePerWebRequest(),
In Autofac, I change the above code to this:
builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
.Where(t => t.Name.EndsWith("Repository"))
.InstancePerRequest();
Is it correct?
Register by Type var builder = new ContainerBuilder(); builder. RegisterType<ConsoleLogger>(); builder. RegisterType(typeof(ConfigReader)); When using reflection-based components, Autofac automatically uses the constructor for your class with the most parameters that are able to be obtained from the container.
Use Autofac as an IoC container API Web API project with the types you will want to inject. Autofac also has a feature to scan assemblies and register types by name conventions.
RegistrationExtensions. AsImplementedInterfaces MethodSpecifies that a type from a scanned assembly is registered as providing all of its implemented interfaces.
This is the correct way:
builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
.Where(t => t.Name.EndsWith("Repository"))
.AsImplementedInterfaces()
.InstancePerRequest();
For UWP correct way is a bit alter:
var assemblyType = typeof(MyCustomAssemblyType).GetTypeInfo();
builder.RegisterAssemblyTypes(assemblyType.Assembly)
.Where(t => t.Name.EndsWith("Repository"))
.AsImplementedInterfaces()
.InstancePerRequest();
For each assembly you have take single type that belongs assembly and retrieve assembly's link from it. Then feed builder this link. Repeat.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With