Advertisement
Guest User

AutoFac Simple Resolve Demo

a guest
Oct 28th, 2013
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Autofac;
  6.  
  7. namespace TestAutoFac
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var builder = new ContainerBuilder();
  14.  
  15.             // Register our types with AutoFac
  16.  
  17.             // House is a singleton because whatever.
  18.             builder.RegisterType<House>().SingleInstance();
  19.  
  20.  
  21.             // Register implementations as the interface type.
  22.             builder.RegisterType<SmallBedroom>().As<IBedroom>();
  23.             builder.RegisterType<MasterBedroom>().As<IBedroom>();
  24.             builder.RegisterType<Kitchen>().As<IKitchen>();
  25.             builder.RegisterType<Bathroom>().As<IBathroom>();
  26.  
  27.             var container = builder.Build();
  28.  
  29.             // Resolve the house.
  30.             // Note: no parameters passed despite house constructor requiring 3 parameters.
  31.             // Parameters will be resolved via registered types.
  32.             // IEnumerable parameters will contain each unique type resolution of parameter (i.e., IEnumerable<IBathroom> will contain one SmallBedroom and one MasterBedroom)
  33.             House h = container.Resolve<House>();
  34.  
  35.             // Output:
  36.             // Kitchen
  37.             // Bathroom
  38.             // MasterBedroom
  39.             // SmallBedroom
  40.             Console.WriteLine(h.Kitchen.GetType().Name);
  41.             Console.WriteLine(h.Bathroom.GetType().Name);
  42.             foreach (IBedroom bedroom in h.Bedrooms)
  43.             {
  44.                 Console.WriteLine(bedroom.GetType().Name);
  45.             }
  46.  
  47.             Console.ReadLine();
  48.         }
  49.     }
  50.  
  51.     public class House
  52.     {
  53.         public IKitchen Kitchen { get; private set; }
  54.  
  55.         public IBathroom Bathroom { get; private set; }
  56.  
  57.         public List<IBedroom> Bedrooms { get; private set; }
  58.  
  59.         public House(IKitchen kitchen, IBathroom bathroom, IEnumerable<IBedroom> bedrooms)
  60.         {
  61.             Kitchen = kitchen;
  62.             Bathroom = bathroom;
  63.             Bedrooms = bedrooms.ToList();
  64.         }
  65.     }
  66.  
  67.     public interface IKitchen
  68.     {
  69.     }
  70.  
  71.     public interface IBedroom
  72.     {
  73.     }
  74.  
  75.     public interface IBathroom
  76.     {
  77.     }
  78.  
  79.     public class Kitchen : IKitchen
  80.     {
  81.     }
  82.  
  83.     public class Bathroom : IBathroom
  84.     {
  85.     }
  86.  
  87.     public class SmallBedroom : IBedroom
  88.     {
  89.     }
  90.  
  91.     public class MasterBedroom : IBedroom
  92.     {
  93.     }
  94.    
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement