Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are .Register and .RegisterType equivalent (for classes with parameterless constructors)?

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?

like image 504
Richard Ev Avatar asked Dec 03 '12 12:12

Richard Ev


1 Answers

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.

like image 129
Jim Bolla Avatar answered Oct 02 '22 15:10

Jim Bolla