{"id":176007,"date":"2013-04-09T22:58:06","date_gmt":"2013-04-09T18:58:06","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=176007"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=176007","title":{"rendered":"<span class=\"post_title\">ASP.NET MVC \u0423\u0440\u043e\u043a 2. Dependency Injection<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t<b>\u0426\u0435\u043b\u044c \u0443\u0440\u043e\u043a\u0430<\/b>: \u0418\u0437\u0443\u0447\u0435\u043d\u0438\u0435 DI (Dependency Injection). \u041f\u0440\u0438\u043c\u0435\u0440 \u043d\u0430 Ninject \u0438 Unity (Autofac, Winsor). <\/p>\n<p>  \u0412\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u0432\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u043c\u043e\u0434\u0443\u043b\u044f\u0445. \u041f\u0440\u043e\u0441\u0442\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0448\u0430\u0431\u043b\u043e\u043d\u0430 \u041e\u0434\u0438\u043d\u043e\u0447\u043a\u0430 (Singleton).<\/p>\n<p>  \u041d\u043e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u044d\u0442\u0443 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e \u0441 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b. \u0422\u0430\u043a \u043a\u0430\u043a \u0434\u0430\u043d\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0438 \u043a \u043d\u0435\u043c\u0443, \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432\u0440\u0435\u043c\u044f \u0436\u0438\u0437\u043d\u0438. \u041f\u0440\u0438 \u043c\u043e\u0434\u0443\u043b\u044c\u043d\u043e\u043c \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 (unit-test) \u043d\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 (\u0438\u043b\u0438 \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e). \u0427\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u044d\u0442\u043e\u0433\u043e, \u043c\u044b \u043d\u0435 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442, \u0430 \u0447\u0435\u0440\u0435\u0437 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441. \u0418 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430, \u0438 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440-\u0437\u0430\u0433\u043b\u0443\u0448\u043a\u0430 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0431\u0443\u0434\u0443\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441. \u0410 \u043b\u043e\u0433\u0438\u043a\u0443 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043c\u044b \u043f\u043e\u0440\u0443\u0447\u0430\u0435\u043c DI-\u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0443.<\/p>\n<p>  <a name=\"habracut\"><\/a><br \/>  \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0435\u0440\u0432\u0438\u0441\u0430. \u041e\u043f\u0438\u0448\u0435\u043c \u043f\u0430\u0440\u0443 \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 IWeapon \u0441 \u043c\u0435\u0442\u043e\u0434\u043e\u043c Kill, \u0434\u0432\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 Bazuka \u0438 Sword, \u0438 \u043a\u043b\u0430\u0441\u0441 Warrior, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043e\u0440\u0443\u0436\u0438\u0435\u043c:  <\/p>\n<pre><code class=\"cs\">  public interface IWeapon     {         void Kill();     }     public class Bazuka : IWeapon     {         public void Kill()         {             Console.WriteLine(&quot;BIG BADABUM!&quot;);         }     }     public class Sword : IWeapon     {         public void Kill()         {             Console.WriteLine(&quot;Chuk-chuck&quot;);         }     }     public class Warrior     {         readonly IWeapon Weapon;          public Warrior(IWeapon weapon)         {             this.Weapon = weapon;         }          public void Kill()         {             Weapon.Kill();         }     } <\/code><\/pre>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u044d\u0442\u043e:  <\/p>\n<pre><code class=\"cs\">    class Program     {         static void Main(string[] args)         {             Warrior warrior = new Warrior(new Bazuka());             warrior.Kill();             Console.ReadLine();         }     } <\/code><\/pre>\n<p>  \u0427\u0438\u0442\u0430\u0435\u043c \u043c\u0435\u0436\u0434\u0443 \u0441\u0442\u0440\u043e\u043a. \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0432\u043e\u0438\u043d\u0430 \u0438 \u0434\u0430\u0435\u043c \u0435\u043c\u0443 \u0431\u0430\u0437\u0443\u043a\u0443, \u043e\u043d \u0438\u0434\u0435\u0442 \u0438 \u0443\u0431\u0438\u0432\u0430\u0435\u0442. \u0412 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c:  <\/p>\n<pre><code class=\"bash\">BIG BADABUM! <\/code><\/pre>\n<p>  \u0417\u0430\u043c\u0435\u0442\u0438\u043c, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u043d\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043d\u0430 null \u0432 \u0441\u0442\u0440\u043e\u043a\u0435   <\/p>\n<pre><code class=\"cs\">Weapon.Kill(); <\/code><\/pre>\n<p>  \u0427\u0442\u043e \u0437\u0434\u0435\u0441\u044c \u043d\u0435\u043a\u043e\u0440\u0435\u043a\u0442\u043d\u043e? \u0412\u043e\u0438\u043d \u043d\u0435 \u0437\u043d\u0430\u0435\u0442, \u0435\u0441\u0442\u044c \u043b\u0438 \u0443 \u043d\u0435\u0433\u043e \u043e\u0440\u0443\u0436\u0438\u0435, \u0438 \u0432\u044b\u0434\u0430\u0447\u0435\u0439 \u043e\u0440\u0443\u0436\u0438\u044f \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u043d\u0435 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u043e\u0434\u0443\u043b\u044c, \u0430 \u0433\u043b\u0430\u0432\u043d\u0430\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430. <br \/>  \u0421\u0443\u0442\u044c DI \u2013 \u043f\u043e\u0440\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0434\u0430\u0447\u0443 \u043e\u0440\u0443\u0436\u0438\u044f \u0434\u0440\u0443\u0433\u043e\u043c\u0443 \u043c\u043e\u0434\u0443\u043b\u044e.<\/p>\n<p>  \u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c Ninject:<\/p>\n<pre><code class=\"bash\">Install-Package Ninject <\/code><\/pre>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043c\u043e\u0434\u0443\u043b\u044c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u0432\u044b\u0434\u0430\u0447\u0435\u0439 \u043e\u0440\u0443\u0436\u0438\u044f:<\/p>\n<pre><code class=\"cs\">    public class WeaponNinjectModule : NinjectModule     {         public override void Load()         {             this.Bind&lt;IWeapon&gt;().To&lt;Sword&gt;();         }     } <\/code><\/pre>\n<p>  \u0427\u0442\u043e \u0431\u0443\u043a\u0432\u0430\u043b\u044c\u043d\u043e \u0437\u043d\u0430\u0447\u0438\u0442: \u00ab\u0435\u0441\u043b\u0438 \u043f\u043e\u043f\u0440\u043e\u0441\u044f\u0442 \u043e\u0440\u0443\u0436\u0438\u0435 \u2013 \u0442\u043e \u0432\u044b\u0434\u0430\u0439\u0442\u0435 \u043c\u0435\u0447\u0438\u00bb.<br \/>  \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u00ab\u0441\u0435\u0440\u0432\u0438\u0441-\u043b\u043e\u043a\u0430\u0442\u043e\u0440\u00bb \u0438 \u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u043e\u0440\u0443\u0436\u0438\u0435\u043c:  <\/p>\n<pre><code class=\"cs\">    class Program     {         public static IKernel AppKernel;          static void Main(string[] args)         {             AppKernel = new StandardKernel(new WeaponNinjectModule());                          var warrior = AppKernel.Get&lt;Warrior&gt;();              warrior.Kill();                          Console.ReadLine();         }     } <\/code><\/pre>\n<p>  \u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e, \u043e\u0431\u044a\u0435\u043a\u0442 warrior \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 new, \u0430 \u0447\u0435\u0440\u0435\u0437 <code>AppKernel.Get&lt;&gt;()<\/code>. \u041f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 AppKernel, \u043c\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 \u043c\u043e\u0434\u0443\u043b\u044c, \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0449\u0438\u0439 \u0437\u0430 \u0432\u044b\u0434\u0430\u0447\u0443 \u043e\u0440\u0443\u0436\u0438\u044f (\u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044d\u0442\u043e \u043c\u0435\u0447). \u041b\u044e\u0431\u043e\u0439 \u043e\u0431\u044a\u0435\u043a\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0435\u0440\u0435\u0437 <code>AppKernel.Get<\/code>, \u0431\u0443\u0434\u0435\u0442 (\u043f\u043e \u043c\u0435\u0440\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438) \u043f\u0440\u043e\u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d, \u0435\u0441\u043b\u0438 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u043c\u043e\u0434\u0443\u043b\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u043d\u0430\u044e\u0442, \u043a\u0430\u043a \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c.<\/p>\n<p>  \u0414\u0440\u0443\u0433\u043e\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f, \u043a\u043e\u0433\u0434\u0430 \u043e\u0431\u044a\u0435\u043a\u0442 <code>Warrior<\/code> \u043d\u0435 \u0431\u0435\u0440\u0435\u0442 \u0441 \u0441\u043e\u0431\u043e\u0439 \u043e\u0440\u0443\u0436\u0438\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u0430 \u043f\u0440\u0438 \u043d\u0435 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u0438\u0438 \u043e\u043d\u043e\u0433\u043e \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043a \u0441\u0435\u0440\u0432\u0438\u0441\u0443 \u043b\u043e\u043a\u0430\u0442\u043e\u0440\u0443 \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0435\u0433\u043e:<\/p>\n<pre><code class=\"cs\">  public class OtherWarrior     {         private IWeapon _weapon;           public IWeapon Weapon         {             get             {                 if (_weapon == null)                 {                     _weapon = Program.AppKernel.Get&lt;IWeapon&gt;();                 }                 return _weapon;             }         }          public void Kill()         {             Weapon.Kill();         }     } <\/code><\/pre>\n<p>  \u0418\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c:  <\/p>\n<pre><code class=\"cs\">      var otherWarrior = new OtherWarrior();       otherWarrior.Kill(); <\/code><\/pre>\n<p>  \u041d\u0430\u0448 \u0432\u043e\u0438\u043d \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043e\u0440\u0443\u0436\u0438\u0435 \u043f\u043e \u043f\u0440\u044f\u043c\u044b\u043c \u043f\u043e\u0441\u0442\u0430\u0432\u043a\u0430\u043c \u2013 \u0441\u0443\u043f\u0435\u0440!<\/p>\n<p>  \u0412 Ninject \u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u043d\u0430 \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u0430\u044f \u0434\u0435\u0442\u0430\u043b\u044c. \u0415\u0441\u043b\u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e (<code>public property<\/code>) \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u043e <code>[Inject]<\/code>, \u0442\u043e \u043f\u0440\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u043a\u043b\u0430\u0441\u0441\u0430 \u0447\u0435\u0440\u0435\u0437 <code>AppKernel.Get&lt;&gt;()<\/code> \u2013 \u043f\u043e\u043b\u0435 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u043c-\u043b\u043e\u043a\u0430\u0442\u043e\u0440\u043e\u043c:   <\/p>\n<pre><code class=\"cs\">    public class AnotherWarrior     {         [Inject]         public IWeapon Weapon { get; set; }          public void Kill()         {             Weapon.Kill();         }     }      var anotherWarrior = AppKernel.Get&lt;AnotherWarrior&gt;();     anotherWarrior.Kill(); <\/code><\/pre>\n<h5>Unity <\/h5>\n<p>  \u0410\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e \u0432\u0441\u0451 \u0442\u043e \u0436\u0435:  <\/p>\n<ul>\n<li>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 <br \/> \n<pre><code class=\"bash\">Install-Package Unity <\/code><\/pre>\n<p>  <\/li>\n<li>\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u043b\u043e\u043a\u0430\u0442\u043e\u0440\u0430 (Container)<br \/> \n<pre><code class=\"cs\">Container = new UnityContainer(); <\/code><\/pre>\n<p>  <\/li>\n<li>\u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u0442\u0438\u043f\u0430 <br \/> \n<pre><code class=\"cs\">Container.RegisterType(typeof(IWeapon), typeof(Bazuka)); <\/code><\/pre>\n<p>  <\/li>\n<li>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435:<br \/> \n<pre><code class=\"cs\">var warrior = Container.Resolve&lt;Warrior&gt;(); warrior.Kill(); <\/code><\/pre>\n<\/li>\n<li>\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0443 Unity \u0435\u0441\u0442\u044c \u043a\u043b\u0430\u0441\u0441-\u043e\u0434\u0438\u043d\u043e\u0447\u043a\u0430 <code>(Singleton) ServiceLocator<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0441\u0435\u0440\u0432\u0438\u0441\u0430\u043c \u0438\u0437 \u043b\u044e\u0431\u043e\u0433\u043e \u043c\u0435\u0441\u0442\u0430.<br \/> \n<pre><code class=\"cs\">var serviceProvider = new UnityServiceLocator(Container); ServiceLocator.SetLocatorProvider(() =&gt; serviceProvider); <\/code><\/pre>\n<p>  <\/li>\n<li>\u0425\u0438\u0442\u0440\u044b\u0439 <code>OtherWarrior<\/code> \u0442\u0435\u043f\u0435\u0440\u044c \u0442\u0430\u043a \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043e\u0440\u0443\u0436\u0438\u0435:<br \/> \n<pre><code class=\"cs\"> public class OtherWarrior     {         private IWeapon _weapon;           public IWeapon Weapon         {             get             {                 if (_weapon == null)                 {                     _weapon = ServiceLocator.Current.GetInstance&lt;IWeapon&gt;();                 }                 return _weapon;             }         }          public void Kill()         {             Weapon.Kill();         } } <\/code><\/pre>\n<\/li>\n<\/ul>\n<h5>Autofac<\/h5>\n<p>  \u0422\u0430\u043a \u0436\u0435, \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0432\u0441\u0451 \u0438 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442:  <\/p>\n<ul>\n<li>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 <br \/> \n<pre><code class=\"bash\">Install-Package Autofac <\/code><\/pre>\n<\/li>\n<li>\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044f \u0441\u0435\u0440\u0432\u0438\u0441\u0430-\u043b\u043e\u043a\u0430\u0442\u043e\u0440\u0430 (<code>ContainerBuilder<\/code>) \u2013 \u043d\u0435\u0442-\u043d\u0435\u0442, \u044d\u0442\u043e \u0435\u0449\u0435 \u043d\u0435 \u0441\u0430\u043c \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440, \u044d\u0442\u043e \u2014 \u043a\u0430\u043a \u043c\u043e\u0434\u0443\u043b\u0438<br \/> \n<pre><code class=\"cs\">var builder = new ContainerBuilder(); <\/code><\/pre>\n<\/li>\n<li>  \u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u0442\u0438\u043f\u043e\u0432. \u041d\u0430\u0434\u043e \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u043e\u0432 \u043d\u0435\u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043a\u043b\u0430\u0441\u0441\u043e\u0432 \u0442\u0443\u0442 \u043d\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d.<br \/> \n<pre><code class=\"cs\">builder.RegisterType&lt;Bazuka&gt;(); builder.RegisterType&lt;Warrior&gt;(); builder.Register&lt;IWeapon&gt;(x =&gt; x.Resolve&lt;Bazuka&gt;()); <\/code><\/pre>\n<p>  <\/li>\n<li>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u043b\u043e\u043a\u0430\u0442\u043e\u0440\u0430 (Container)<br \/> \n<pre><code class=\"cs\">var container = builder.Build(); <\/code><\/pre>\n<p>  <\/li>\n<li>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435:<br \/> \n<pre><code class=\"cs\">var warrior = container.Resolve&lt;Warrior&gt;(); warrior.Kill(); <\/code><\/pre>\n<p>  <\/li>\n<\/ul>\n<p>  <\/p>\n<h5>Castle Windsor<\/h5>\n<p>  <\/p>\n<ul>\n<li>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430<br \/> \n<pre><code class=\"bash\">Install-Package Castle.Windsor <\/code><\/pre>\n<p>  <\/li>\n<li>\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0435\u0440\u0432\u0438\u0441\u0430-\u043b\u043e\u043a\u0430\u0442\u043e\u0440\u0430<br \/> \n<pre><code class=\"cs\">var container = new WindsorContainer(); <\/code><\/pre>\n<p>  <\/li>\n<li>\u0420\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u0442\u0438\u043f\u043e\u0432. \u0410\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u0430 \u043a\u0430\u043a \u0438 \u0432 Autofac. <br \/> \n<pre><code class=\"cs\">container.Register(Component.For&lt;IWeapon&gt;().ImplementedBy&lt;Bazuka&gt;(), Component.For&lt;Warrior&gt;().ImplementedBy&lt;Warrior&gt;()); <\/code><\/pre>\n<p>  <\/li>\n<li>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435:<br \/> \n<pre><code class=\"cs\">var warrior = container.Resolve&lt;Warrior&gt;(); warrior.Kill(); <\/code><\/pre>\n<p>  <\/li>\n<\/ul>\n<p>  <\/p>\n<h5>\u041c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0439 \u043f\u043e\u0434\u0438\u0442\u043e\u0433<\/h5>\n<p>  \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435, \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 Dependency Injection \u043d\u0435 \u0441\u0438\u043b\u044c\u043d\u043e, \u043d\u043e \u0432\u0441\u0451 \u0436\u0435 \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f. \u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0442 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0432 <code>Web.config (App.config)<\/code> \u0444\u0430\u0439\u043b\u0430\u0445. \u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435, \u0437\u0430\u0434\u0430\u044e\u0442 \u043f\u0440\u0430\u0432\u0438\u043b\u0430 \u0434\u043b\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u043a\u0430\u043a \u043c\u044b \u0441\u0435\u0439\u0447\u0430\u0441 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0438 Ninject \u0434\u043b\u044f asp.net mvc \u2013 \u044d\u0442\u043e \u043a\u0430\u0441\u0430\u0435\u0442\u0441\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u0430-\u043b\u043e\u043a\u0430\u0442\u043e\u0440\u0430 \u043a\u0430\u043a \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u043e\u0431\u0449\u0438\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u0442\u0430\u043a \u0438 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043a\u0430 \u0438\u043b\u0438 web-\u0437\u0430\u043f\u0440\u043e\u0441\u0435.<\/p>\n<h5>\u041e\u0431\u044a\u0435\u043a\u0442\u044b \u043e\u0431\u043b\u0430\u0441\u0442\u0435\u0439 (Ninject)<\/h5>\n<p>  \u0412 Ninject \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0434\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438\u0437 \u043a\u043b\u0430\u0441\u0441\u0430. \u0415\u0441\u043b\u0438 \u043c\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0432 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430\u0445 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u0430\u0445 (Thread)), \u0442\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u044b \u0440\u0430\u0437\u043d\u044b\u0435. \u0422\u0435\u043c \u0441\u0430\u043c\u044b\u043c, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u043c\u043e\u0441\u0442\u044c \u0438 \u0433\u0438\u0431\u043a\u043e\u0441\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f.   <\/p>\n<table>\n<tr>\n<td><b>\u041e\u0431\u043b\u0430\u0441\u0442\u044c<\/b><\/td>\n<td><b>\u041c\u0435\u0442\u043e\u0434 \u0441\u0432\u044f\u0437\u044b\u0432\u0430\u043d\u0438\u044f<\/b><\/td>\n<td><b>\u041e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u0435<\/b><\/td>\n<\/tr>\n<tr>\n<td>\u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439<\/td>\n<td><code>.InTransientScope()<\/code><\/td>\n<td>\u041e\u0431\u044a\u0435\u043a\u0442 \u043a\u043b\u0430\u0441\u0441\u0430 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044e (\u043c\u0435\u0442\u043e\u0434 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e).<\/td>\n<\/tr>\n<tr>\n<td>\u041e\u0434\u0438\u043d\u043e\u0447\u043a\u0430<\/td>\n<td><code>.InSingletonScope()<\/code><\/td>\n<td>\u041e\u0431\u044a\u0435\u043a\u0442 \u043a\u043b\u0430\u0441\u0441\u0430 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u0438 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e. <\/td>\n<\/tr>\n<tr>\n<td>\u041f\u043e\u0442\u043e\u043a<\/td>\n<td><code>.InThreadScope()<\/code><\/td>\n<td>\u041e\u0434\u0438\u043d \u043e\u0431\u044a\u0435\u043a\u0442 \u043d\u0430 \u043f\u043e\u0442\u043e\u043a.<\/td>\n<\/tr>\n<tr>\n<td>\u0417\u0430\u043f\u0440\u043e\u0441<\/td>\n<td><code>.InRequestScope()<\/code><\/td>\n<td>\u041e\u0434\u0438\u043d \u043e\u0431\u044a\u0435\u043a\u0442 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 web-\u0437\u0430\u043f\u0440\u043e\u0441<\/td>\n<\/tr>\n<\/table>\n<h5>Lifetime Manager \u0432 Unity <\/h5>\n<p>  \u0412 Unity \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u0440\u0430\u0432\u0438\u043b \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 LifetimeManager. <br \/>  \u041f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u044d\u0442\u043e \u0442\u0430\u043a:   <\/p>\n<pre><code class=\"cs\"> _container.RegisterType&lt;DbContext, SavecashTravelContext&gt;(new PerRequestLifetimeManager()); <\/code><\/pre>\n<p>  \u0413\u0434\u0435 PerRequestLifetimeManager \u2013 \u044d\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f LifetimeManager:  <\/p>\n<pre><code class=\"cs\">public class PerRequestLifetimeManager : LifetimeManager     {         \/\/\/ &lt;summary&gt;         \/\/\/ Key to store data         \/\/\/ &lt;\/summary&gt;         private readonly string _key = String.Format(&quot;SingletonPerRequest{0}&quot;, Guid.NewGuid());          \/\/\/ &lt;summary&gt;         \/\/\/ Retrieve a value from the backing store associated with this Lifetime policy.         \/\/\/ &lt;\/summary&gt;         \/\/\/ &lt;returns&gt;         \/\/\/ the object desired, or null if no such object is currently stored.         \/\/\/ &lt;\/returns&gt;         public override object GetValue()         {             if (HttpContext.Current != null && HttpContext.Current.Items.Contains(_key))                 return HttpContext.Current.Items[_key];             return null;         }          \/\/\/ &lt;summary&gt;         \/\/\/ Stores the given value into backing store for retrieval later.         \/\/\/ &lt;\/summary&gt;         \/\/\/ &lt;param name=&quot;newValue&quot;&gt;The object being stored.&lt;\/param&gt;         public override void SetValue(object newValue)         {             if (HttpContext.Current != null)                 HttpContext.Current.Items[_key] = newValue;         }          \/\/\/ &lt;summary&gt;         \/\/\/ Remove the given object from backing store.         \/\/\/ &lt;\/summary&gt;         public override void RemoveValue()         {             if (HttpContext.Current != null && HttpContext.Current.Items.Contains(_key))                 HttpContext.Current.Items.Remove(_key);         }     } <\/code><\/pre>\n<p>  \u0421\u0443\u0442\u044c. \u0412\u0441\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 <code>HttpContext.Current.Items[_key]<\/code> \u0438 \u0432\u044b\u0434\u0430\u044e\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e, \u0435\u0441\u043b\u0438 \u0443\u0436\u0435 \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u0442\u043e\u043c \u0436\u0435 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 (<code>HttpContext.Current<\/code>). \u0412 \u0438\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043d\u043e\u0432\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442. \u0415\u0441\u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 (<code>HttpContext.Current<\/code>) \u0432 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u043a\u043e\u0434\u0430 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 (\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0442\u0430\u043a\u043e\u0439 <code>LifetimeManager <\/code>\u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c\u043d\u043e\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0438\u043b\u0438 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435) \u2013 \u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c.<\/p>\n<h5>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 Ninject \u0432 asp.net mvc<\/h5>\n<p>  \u0423\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c Ninject \u0432 \u0441\u0440\u0435\u0434\u0443 asp.net mvc. \u041e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u0432\u043e\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 LessonProject, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0430\u043c HomeController \u0441 \u043c\u0435\u0442\u043e\u0434\u043e\u043c \u0438 view Index. (\/Contollers\/HomeController.cs):  <\/p>\n<pre><code class=\"cs\">public class HomeController : Controller     {         public ActionResult Index()         {             return View();         }     }  <\/code><\/pre>\n<p>  \u0418 (\/Views\/Home\/Index.cshtml):<\/p>\n<pre><code class=\"html\">@{     ViewBag.Title = &quot;LessonProject&quot;;     Layout = &quot;~\/Views\/Shared\/_Layout.cshtml&quot;; }  &lt;h2&gt;LessonProject&lt;\/h2&gt; <\/code><\/pre>\n<p>  \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u2013 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.<\/p>\n<p>  <i>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435: \u0412 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u043f\u0440\u043e\u0435\u043a\u0442 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0443\u0440\u043e\u043a\u0438. <\/i><\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c \u043c\u043e\u0434\u0443\u043b\u044c Ninject \u0438 Ninject.MVC3 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430.  <\/p>\n<pre><code class=\"bash\">Install-Package Ninject.MVC3 <\/code><\/pre>\n<p>  \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043a\u043b\u0430\u0441\u0441 \u0432 \u043f\u0430\u043f\u043a\u0443 App_Start (\/App_Start\/NinjectWebCommon.cs):  <\/p>\n<pre><code class=\"cs\"> [assembly: WebActivator.PreApplicationStartMethod(typeof(LessonProject.App_Start.NinjectWebCommon), &quot;Start&quot;)] [assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(LessonProject.App_Start.NinjectWebCommon), &quot;Stop&quot;)] namespace LessonProject.App_Start {     using System;     using System.Web;      using Microsoft.Web.Infrastructure.DynamicModuleHelper;      using Ninject;     using Ninject.Web.Common;      public static class NinjectWebCommon      {         private static readonly Bootstrapper bootstrapper = new Bootstrapper();          \/\/\/ &lt;summary&gt;         \/\/\/ Starts the application         \/\/\/ &lt;\/summary&gt;         public static void Start()          {             DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));             DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));             bootstrapper.Initialize(CreateKernel);         }                  \/\/\/ &lt;summary&gt;         \/\/\/ Stops the application.         \/\/\/ &lt;\/summary&gt;         public static void Stop()         {             bootstrapper.ShutDown();         }                  \/\/\/ &lt;summary&gt;         \/\/\/ Creates the kernel that will manage your application.         \/\/\/ &lt;\/summary&gt;         \/\/\/ &lt;returns&gt;The created kernel.&lt;\/returns&gt;         private static IKernel CreateKernel()         {             var kernel = new StandardKernel();             kernel.Bind&lt;Func&lt;IKernel&gt;&gt;().ToMethod(ctx =&gt; () =&gt; new Bootstrapper().Kernel);             kernel.Bind&lt;IHttpModule&gt;().To&lt;HttpApplicationInitializationHttpModule&gt;();                          RegisterServices(kernel);             return kernel;         }          \/\/\/ &lt;summary&gt;         \/\/\/ Load your modules or register your services here!         \/\/\/ &lt;\/summary&gt;         \/\/\/ &lt;param name=&quot;kernel&quot;&gt;The kernel.&lt;\/param&gt;         private static void RegisterServices(IKernel kernel)         {         }             } } <\/code><\/pre>\n<p>  \u0412 RegisterServices \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0441\u0432\u043e\u0438\u0445 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432. \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0448\u0443\u0442\u043b\u0438\u0432\u044b\u0439 IWeapon, \u0430 \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u0435\u0449\u0435 \u0431\u0443\u0434\u0435\u043c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u044d\u0442\u043e\u043c\u0443 \u043c\u0435\u0442\u043e\u0434\u0443 \u0434\u043b\u044f \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432:   <\/p>\n<pre><code class=\"cs\">  public interface IWeapon     {         string Kill();     } \u2026     public class Bazuka : IWeapon     {         public string Kill()         {             return &quot;BIG BADABUM!&quot;;         }     } \u2026     private static void RegisterServices(IKernel kernel)     { kernel.Bind&lt;IWeapon&gt;().To&lt;Bazuka&gt;();     } <\/code><\/pre>\n<p>  \u0412 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0430\u0442\u0440\u0438\u0431\u0443\u0442 <code>[Inject]<\/code>:   <\/p>\n<pre><code class=\"cs\">  public class HomeController : Controller     {         [Inject]         public IWeapon weapon { get; set; }          public ActionResult Index()         {             return View(weapon);         }     } <\/code><\/pre>\n<p>  \u0418\u0437\u043c\u0435\u043d\u044f\u0435\u043c View:  <\/p>\n<pre><code class=\"html\">@model LessonProject.Models.IWeapon @{     ViewBag.Title = &quot;LessonProject&quot;;     Layout = &quot;~\/Views\/Shared\/_Layout.cshtml&quot;; }  &lt;h2&gt;LessonProject&lt;\/h2&gt;  &lt;p&gt; @Model.Kill() &lt;\/p&gt;  <\/code><\/pre>\n<p>  \u041d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c:<br \/>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/8fb\/496\/ff0\/8fb496ff05351221ac16f967e86cbcc6.jpg\"\/><\/p>\n<p>  Ninject \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 WebActivator:  <\/p>\n<ul>\n<li>\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u0442 \u0441\u0432\u043e\u0438 \u043c\u043e\u0434\u0443\u043b\u0438 OnePerRequestHttpModule \u0438 NinjectHttpModule<\/li>\n<li>\u0441\u043e\u0437\u0434\u0430\u0435\u0442 StandartKernel<\/li>\n<li>\u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442 \u043d\u0430\u0448\u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u044b.<\/li>\n<\/ul>\n<h5>DependencyResolver<\/h5>\n<p>  \u0412 asp.net mvc3 \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f \u043a\u043b\u0430\u0441\u0441 DependencyResolver. \u042d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u0441\u0435\u0440\u0432\u0438\u0441\u0430. \u041d\u0430\u0448\u0438 \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u044b (\u0438 \u0434\u0430\u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0439 DI-\u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440) \u043c\u044b \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u043e\u043c \u044d\u0442\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430.   <\/p>\n<pre><code class=\"cs\">    public class HomeController : Controller     {         private IWeapon weapon { get; set; }          public HomeController()         {             weapon = DependencyResolver.Current.GetService&lt;IWeapon&gt;();         }          public ActionResult Index()         {             return View(weapon);         }     } <\/code><\/pre>\n<p>  <\/p>\n<h5>\u0418\u0442\u043e\u0433<\/h5>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 DI-\u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u043e\u0432 \u0432 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u0441\u0438\u043b\u044c\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u043d\u043e\u0441\u0442\u0438 \u043a\u043e\u0434\u0430, \u0438 \u0434\u043b\u044f \u043b\u0435\u0433\u043a\u043e\u0433\u043e \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u0438\u0437 \u043b\u044e\u0431\u043e\u0439 \u0435\u0433\u043e \u0447\u0430\u0441\u0442\u0438 \u043a \u0441\u0435\u0440\u0432\u0438\u0441\u0430\u043c. \u0422\u0430\u043a\u0436\u0435, \u044d\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043b\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f Unit-\u0442\u0435\u0441\u0442\u043e\u0432.<\/p>\n<p>  \u0412\u0441\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 <a href=\"https:\/\/bitbucket.org\/chernikov\/lessons\">https:\/\/bitbucket.org\/chernikov\/lessons<\/a>    \t \t\t   \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/176007\/\"> http:\/\/habrahabr.ru\/post\/176007\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t<b>\u0426\u0435\u043b\u044c \u0443\u0440\u043e\u043a\u0430<\/b>: \u0418\u0437\u0443\u0447\u0435\u043d\u0438\u0435 DI (Dependency Injection). \u041f\u0440\u0438\u043c\u0435\u0440 \u043d\u0430 Ninject \u0438 Unity (Autofac, Winsor). <\/p>\n<p>  \u0412\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u0432\u0430\u0448\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438 \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u043c\u043e\u0434\u0443\u043b\u044f\u0445. \u041f\u0440\u043e\u0441\u0442\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0448\u0430\u0431\u043b\u043e\u043d\u0430 \u041e\u0434\u0438\u043d\u043e\u0447\u043a\u0430 (Singleton).<\/p>\n<p>  \u041d\u043e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u044d\u0442\u0443 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e \u0441 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b. \u0422\u0430\u043a \u043a\u0430\u043a \u0434\u0430\u043d\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0438 \u043a \u043d\u0435\u043c\u0443, \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432\u0440\u0435\u043c\u044f \u0436\u0438\u0437\u043d\u0438. \u041f\u0440\u0438 \u043c\u043e\u0434\u0443\u043b\u044c\u043d\u043e\u043c \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 (unit-test) \u043d\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 (\u0438\u043b\u0438 \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e). \u0427\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u044d\u0442\u043e\u0433\u043e, \u043c\u044b \u043d\u0435 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u043e\u0431\u044a\u0435\u043a\u0442, \u0430 \u0447\u0435\u0440\u0435\u0437 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441. \u0418 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430, \u0438 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440-\u0437\u0430\u0433\u043b\u0443\u0448\u043a\u0430 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0431\u0443\u0434\u0443\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441. \u0410 \u043b\u043e\u0433\u0438\u043a\u0443 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043c\u044b \u043f\u043e\u0440\u0443\u0447\u0430\u0435\u043c DI-\u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0443.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-176007","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/176007","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=176007"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/176007\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=176007"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=176007"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=176007"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}