Advertisement
almir_vuk

AutoFac Dependency Injection

Jul 1st, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.01 KB | None | 0 0
  1. using Autofac;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace DependencyInjection {
  9.  
  10.     class Program {
  11.  
  12.         static void Main(string[] args) {
  13.  
  14.             List<int> speakerList = new List<int>();
  15.             List<float> presentationList = new List<float>();
  16.  
  17.             using (var container = ContainerListi.SpeakerListContainer()) {
  18.  
  19.                 var speakerController = container.Resolve<IGetSpeakerList>();
  20.                 speakerList = speakerController.GetListu();
  21.             }
  22.  
  23.             using (var container = ContainerListi.PresentationListContainer()) {
  24.  
  25.                 var presentationController = container.Resolve<IGetPresentationList>();
  26.                 presentationList = presentationController.GetListu();
  27.             }
  28.  
  29.  
  30.  
  31.             for (int i = 0; i < speakerList.Count; i++)
  32.                 Console.WriteLine(speakerList[i]);
  33.  
  34.             for (int i = 0; i < presentationList.Count; i++)
  35.                 Console.WriteLine(presentationList[i]);
  36.  
  37.             Console.WriteLine("Negativni brojevi [ -1 -2 -3 ] su dummy PREDAVACI\n" +
  38.                               "Negativni brojevi [ -10 -20 -30 ] su dummy PREDAVANJA\n" +
  39.                               "Pozitivni brojeve [ 1 2 3 ] su real PREDAVACI\n" +
  40.                               "Pozitivni brojevi [ 10 20 30 ] su real PREDAVANJA");
  41.  
  42.         }
  43.     }
  44.  
  45.  
  46.     // Predavač dummy
  47.     class DummySpeakerController : IGetSpeakerList {
  48.  
  49.         public List<int> GetListu() {
  50.             return new List<int>() { -1, -2, -3 };
  51.         }
  52.     }
  53.  
  54.     // Predavač real
  55.     class CloudSpeakerController : IGetSpeakerList {
  56.  
  57.         public List<int> GetListu() {
  58.             return new List<int>() { 1, 2, 3 };
  59.         }
  60.     }
  61.  
  62.  
  63.     // Predavanja dummy
  64.     class DummyPresentationController : IGetPresentationList {
  65.  
  66.         public List<float> GetListu() {
  67.             return new List<float>() { -10, -20, -30 };
  68.         }
  69.     }
  70.  
  71.     // Predavanja real
  72.     class CloudPresentationController : IGetPresentationList {
  73.  
  74.         public List<float> GetListu() {
  75.             return new List<float>() { 10, 20, 30 };
  76.         }
  77.     }
  78.  
  79.  
  80.     class ContainerListi {
  81.  
  82.         public static IContainer SpeakerListContainer() {
  83.  
  84.             var builder = new ContainerBuilder();
  85.             builder.RegisterType<DummySpeakerController>().As<IGetSpeakerList>();
  86.  
  87.             return builder.Build();
  88.         }
  89.  
  90.  
  91.         public static IContainer PresentationListContainer() {
  92.  
  93.             var builder = new ContainerBuilder();
  94.             builder.RegisterType<CloudPresentationController>().As<IGetPresentationList>();
  95.  
  96.             return builder.Build();
  97.         }
  98.  
  99.     }
  100.  
  101.     // Interface za predavace - int = Speaker
  102.     public interface IGetSpeakerList {
  103.  
  104.         List<int> GetListu();
  105.     }
  106.  
  107.     // Interface za predavanja - float = Presentation
  108.     public interface IGetPresentationList {
  109.  
  110.         List<float> GetListu();
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement