1: public class FakeOrderRepository : IOrderRepository
2: {
3: private readonly List<Order> _orders;
4:
5: public FakeOrderRepository (List<Order> orders)
6: {
7: _orders = orders;
8: }
9:
10: public IQueryable<Order> GetOrders ()
11: {
12: return _orders.AsQueryable ();
13: }
14: }
1: public static class Bootstrapper
2: {
3: public static IContainerProvider ConfigureAutofac ()
4: {
5: var builder = new ContainerBuilder ();
6:
7: // Register a logger service which will get injected as a property after object construction (ex: public ILogger Log { get; set; })
8: builder.Register<Log4NetLogger> ().As<ILogger> ().OnActivating (ActivatingHandler.InjectProperties).SingletonScoped ();
9:
10: builder.Register<MailerService> ().As<IMailerService> ().FactoryScoped ();
11:
12: // Example of passing in named constructor parameters
13: // I registered a fake repository here to get a quick prototype up and running of of the checkout process
14: List<Order> orders = FakeRepositoryData.CreateOrders ();
15: builder.Register<FakeOrderRepository> ().WithArguments (new NamedParameter ("orders", orders)).As<IOrderRepository> ().FactoryScoped ();
16:
17: // Attach the ActivatingHandler to our controller module so that ILogger will get injected on our asp.net mvc controllers as well
18: AutofacControllerModule autofacControllerModule = new AutofacControllerModule (Assembly.GetExecutingAssembly ());
19: autofacControllerModule.ActivatingHandler += ActivatingHandler.InjectProperties;
20: builder.RegisterModule (autofacControllerModule);
21:
22: IContainerProvider containerProvider = new ContainerProvider (builder.Build ());
23: ControllerBuilder.Current.SetControllerFactory (new AutofacControllerFactory (containerProvider));
24:
25: return containerProvider;
26: }
27: }