Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Ninject.Modules;
  7. using Ninject;
  8. using Microsoft.Practices.Unity;
  9. using System.ComponentModel;
  10.  
  11. namespace ConsoleApplication1
  12. {
  13.      class Program
  14.     {
  15.  
  16.          public static IKernel AppKernel;
  17.  
  18.         static void Main(string[] args)
  19.         {
  20.             Warrior warrior = new Warrior(new Bazuka());
  21.             warrior.Kill();
  22.  
  23.             AppKernel = new StandardKernel(new WeaponNinjectModule());
  24.  
  25.            var Warrior = AppKernel.Get<Warrior>();
  26.  
  27.             warrior.Kill();
  28.  
  29.             var otherWarrior = new ConsoleApplication1.Program.Warrior.OtherWarrior();
  30.             otherWarrior.Kill();
  31.  
  32.             var anotherWarrior = AppKernel.Get<AnotherWarrior>();
  33.             anotherWarrior.Kill();
  34.  
  35.             /* Unity */
  36.  
  37.             var Container = new UnityContainer();
  38.             Container.RegisterType(typeof(IWeapon), typeof(Bazuka));
  39.  
  40.             var warrior = Container.Resolve<Warrior>();
  41.             warrior.Kill();
  42.  
  43.             var serviceProvider = new UnityServiceLocator(Container);
  44.             ServiceLocator.SetLocatorProvider(() => serviceProvider);
  45.  
  46.             Console.ReadLine();
  47.         }
  48.  
  49.  
  50.         public class AnotherWarrior
  51.         {
  52.             [Inject]
  53.             public IWeapon Weapon { get; set; }
  54.  
  55.             public void Kill()
  56.             {
  57.                 Weapon.Kill();
  58.             }
  59.         }
  60.  
  61.         /* Создаем модуль, который занимается выдачей оружия: */
  62.         public class WeaponNinjectModule : NinjectModule
  63.         {
  64.             public override void Load()
  65.             {
  66.                 this.Bind<IWeapon>().To<Sword>();
  67.             }
  68.         }
  69.  
  70.          public interface IWeapon
  71.         {
  72.             void Kill();
  73.         }
  74.        
  75.         public class Bazuka : IWeapon
  76.         {
  77.             public void Kill()
  78.             {
  79.                 Console.WriteLine("BIG BADABUM!");
  80.             }
  81.         }
  82.        
  83.         public class Sword : IWeapon
  84.         {
  85.             public void Kill()
  86.             {
  87.                 Console.WriteLine("Chuk-chuck");
  88.             }
  89.         }
  90.        
  91.         public class Warrior
  92.         {
  93.             readonly IWeapon Weapon;
  94.  
  95.             public Warrior(IWeapon weapon)
  96.             {
  97.                 this.Weapon = weapon;
  98.             }
  99.  
  100.             public void Kill()
  101.             {
  102.                 Weapon.Kill();
  103.             }
  104.  
  105.  
  106.            
  107.             public class OtherWarrior
  108.             {
  109.                 private IWeapon _weapon;
  110.  
  111.                 public IWeapon Weapon
  112.                 {
  113.                     get
  114.                     {
  115.                         if (_weapon == null)
  116.                         {
  117.                             _weapon = Program.AppKernel.Get<IWeapon>();
  118.                         }
  119.                         return _weapon;
  120.                     }
  121.                 }
  122.  
  123.                 public void Kill()
  124.                 {
  125.                     Weapon.Kill();
  126.                 }
  127.  
  128.             }
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement