When using AutoFac, you can use .RegisterType
to associate a class with an interface, but you can also use .Register
(which allows you to specify construtor arguments via a lambda).
For classes that have a parameterless constructor, are these two methods equivalent?
var builder = new Autofac.ContainerBuilder();
builder.RegisterType<MyClass>().As<IMyInterface>();
builder.Register(x => new MyClass()).As<IMyInterface>();
Is there any scenario where using .RegisterType
is perferable?
Assuming your class only has one defined constructor, they should be functionally equivalent.
One internal difference would be that RegisterType
will use reflection to determine that constructor to use while Register
with a lambda has provided all the information needed.
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