Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Ninject.Modules;
- using Ninject;
- using Microsoft.Practices.Unity;
- using System.ComponentModel;
- namespace ConsoleApplication1
- {
- class Program
- {
- public static IKernel AppKernel;
- static void Main(string[] args)
- {
- Warrior warrior = new Warrior(new Bazuka());
- warrior.Kill();
- AppKernel = new StandardKernel(new WeaponNinjectModule());
- var Warrior = AppKernel.Get<Warrior>();
- warrior.Kill();
- var otherWarrior = new ConsoleApplication1.Program.Warrior.OtherWarrior();
- otherWarrior.Kill();
- var anotherWarrior = AppKernel.Get<AnotherWarrior>();
- anotherWarrior.Kill();
- /* Unity */
- var Container = new UnityContainer();
- Container.RegisterType(typeof(IWeapon), typeof(Bazuka));
- var warrior = Container.Resolve<Warrior>();
- warrior.Kill();
- var serviceProvider = new UnityServiceLocator(Container);
- ServiceLocator.SetLocatorProvider(() => serviceProvider);
- Console.ReadLine();
- }
- public class AnotherWarrior
- {
- [Inject]
- public IWeapon Weapon { get; set; }
- public void Kill()
- {
- Weapon.Kill();
- }
- }
- /* Создаем модуль, который занимается выдачей оружия: */
- public class WeaponNinjectModule : NinjectModule
- {
- public override void Load()
- {
- this.Bind<IWeapon>().To<Sword>();
- }
- }
- public interface IWeapon
- {
- void Kill();
- }
- public class Bazuka : IWeapon
- {
- public void Kill()
- {
- Console.WriteLine("BIG BADABUM!");
- }
- }
- public class Sword : IWeapon
- {
- public void Kill()
- {
- Console.WriteLine("Chuk-chuck");
- }
- }
- public class Warrior
- {
- readonly IWeapon Weapon;
- public Warrior(IWeapon weapon)
- {
- this.Weapon = weapon;
- }
- public void Kill()
- {
- Weapon.Kill();
- }
- public class OtherWarrior
- {
- private IWeapon _weapon;
- public IWeapon Weapon
- {
- get
- {
- if (_weapon == null)
- {
- _weapon = Program.AppKernel.Get<IWeapon>();
- }
- return _weapon;
- }
- }
- public void Kill()
- {
- Weapon.Kill();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement