Reputation: 8597
My project (which happens built on top of Orchard, though I don't think that's relevant) uses Autofac. I am writing unit tests in which I want to stub out any dependencies using Moq, and I'm using the Autofac/Moq integration to achieve this.
This is fine for any simple dependencies that are being passed in as constructor arguments. (Autofac documentation provides details of how to achieve this here).
But because I don't ever have a containerBuilder, I don't see how to use a lot of Autofac's power - lamda registration, registering generics, marking properties to be auto-wired. etc.
Upvotes: 6
Views: 3114
Reputation: 4428
Some of the features, you've mentioned, can be used with AutoMock class and some won't make sense any more. With AutoMock we do not deal with ContainerBuilder class - this is for simplicity, we don't want to have to code all those registering instructions, but to only register the ones interesting for us. So from creation of AutoMock object with GetLoose or GetStrict method we deal only with built IContainer object ready to resolve things. Fortunately for us IContainer still allows us to extend its registration set, however Autofac won't support that with its convenient extension methods as they are built to work with ContainerBuilder object. This is perfectly reasonable because for Autofac perspective extending definitions on IContainer object is something uncommon. ContainerBuilder is supposed to handle registration and IContainer is supposed to handle resolving.
Here I will, as an example, present how with AutoMock we may use auto-wired properties functionality:
using (var mock = AutoMock.GetLoose())
{
mock.Container.ComponentRegistry.Register(
RegistrationBuilder
.ForType<MyClass>()
.PropertiesAutowired()
.CreateRegistration<MyClass, ConcreteReflectionActivatorData, SingleRegistrationStyle>()
);
}
As you see now we have to deal with all the ugliness of autofac internal code which normally is hidden by extension methods. PropertiesAutowired functionality may be easily used with AutoMock, however lamda registration will not make much sense any more. What lambda registration gives us is that object being registered in ContainerBuilder can depend on other registered object which will be resolved during building ContainerBuilder to IContainer so we do not need to care for the order of the registering instructions. Here we already have ready made IContainer and it won't be built again so such deffering of registration is pointless. If we want to pass to some registered object another already registered object then we may simply resolve it first and then use it.
So as summary:
Upvotes: 2