Monday, June 1, 2009

An Autofac bootstrapper for ASP.NET MVC 1.0 with property injection



   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: }

Wednesday, November 26, 2008

An ASP.NET MVC helper for calling Autofac Resolve


   1: // Typical Autofac integration with ASP.NET MVC
   2: public class MvcApplication : System.Web.HttpApplication, IContainerProviderAccessor
   3: {
   4:         public IContainerProvider ContainerProvider
   5:         {
   6:             get
   7:             {
   8:                 return _containerProvider;
   9:             }
  10:         }
  11: }
  12:  
  13: // Our Resolve Extension Method
  14: public static class AutofacHelper
  15: {
  16:     public static T Resolve <T> (this HttpContext source)
  17:     {
  18:         IContainerProviderAccessor cpa = (IContainerProviderAccessor)source.ApplicationInstance;
  19:         return cpa.ContainerProvider.RequestContainer.Resolve<T> ();
  20:     }
  21: }
  22:  
  23: // Resolving easy-mode
  24: ICustomerService customerService = HttpContext.Current.Resolve<ICustomerService> ();
  25:  
  26: // Example code-behind
  27: public partial class Site : System.Web.Mvc.ViewMasterPage
  28: {
  29:     protected string GetUserName ()
  30:     {
  31:         if (Request.IsAuthenticated)
  32:         {
  33:             ICustomerService customerService = HttpContext.Current.Resolve<ICustomerService> ();
  34:             Customer customer = customerService.GetCustomer (HttpContext.Current.User.Identity.Name);
  35:             if (customer != null && customer.IsRegistered)
  36:             {
  37:                 return customer.FirstName + " " + customer.LastName;
  38:             }
  39:         }
  40:  
  41:         return string.Empty;
  42:     }
  43: }