{"id":17,"date":"2010-04-27T13:07:29","date_gmt":"2010-04-27T11:07:29","guid":{"rendered":"http:\/\/blog.fh-kaernten.at\/wehr\/?p=17"},"modified":"2011-02-15T11:37:14","modified_gmt":"2011-02-15T10:37:14","slug":"ninject-the-first-contact","status":"publish","type":"post","link":"https:\/\/blog.fh-kaernten.at\/wehr\/2010\/04\/27\/ninject-the-first-contact\/","title":{"rendered":"Ninject, the first contact."},"content":{"rendered":"<p id=\"top\" \/>\n<p><img decoding=\"async\" class=\"size-medium wp-image-20 alignnone\" src=\"http:\/\/blog.fh-kaernten.at\/wehr\/files\/2010\/04\/NinjectLogo-300x91.png\" alt=\"\" width=\"300\" height=\"91\" srcset=\"https:\/\/blog.fh-kaernten.at\/wehr\/files\/2010\/04\/NinjectLogo-300x91.png 300w, https:\/\/blog.fh-kaernten.at\/wehr\/files\/2010\/04\/NinjectLogo.png 450w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>So what&#8217;s <a title=\"Ninject\" href=\"http:\/\/ninject.org\/\" target=\"_blank\">Ninject<\/a>.\u00a0 Ninject is a lightweight dependency injection framework for .NET applications. &#8230; and what is it good for (absolutely nothing?)? Ehm, not at all!<\/p>\n<p>The simplest use case would be following scenario:<\/p>\n<p>For example your design declares a singleton class. Normally you would code something like that:<\/p>\n<pre class=\"brush: csharp\"> public sealed class TheManager\r\n{\r\n    static TheManager instance=null;\r\n    static readonly object padlock = new object();\r\n\r\n    TheManager()\r\n    {\r\n    }\r\n\r\n    public static TheManager Instance\r\n    {\r\n        get\r\n        {\r\n            lock (padlock)\r\n            {\r\n                if (instance==null)\r\n                {\r\n                    instance = new TheManager();\r\n                }\r\n                return instance;\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>Then you would get an instance of TheManager like that:<\/p>\n<pre class=\"brush: csharp\"> TheManager myManager = TheManager.Instance()\r\n<\/pre>\n<p>Okay, now we will use Ninject for our singleton pattern.<br \/>\n If you use Ninject you need not to add code for locking down your class to achieve a singleton behavior. You only have to tell Ninject that the class type should be tied to some implementation, in our example to itself. Furthermore, you have to setup\u00a0 the &#8211; what they call &#8211; <em>Activation Behavior<\/em>. In our Example it is,&#8230;.\u00a0 yes the <em>InSingletonScope.<\/em><\/p>\n<p><strong>Ninject <\/strong>distinguishes between 4 (build-in) scopes:<\/p>\n<table>\n<tbody>\n<tr>\n<td>Transient<\/td>\n<td><code>.InTransientScope()<\/code><\/td>\n<td>A new instance of the type will be created each time one is requested. (This is the default scope)<\/td>\n<\/tr>\n<tr>\n<td>Singleton<\/td>\n<td><code>.InSingletonScope()<\/code><\/td>\n<td>Only a single instance of the type will be created, and the same instance will be returned for each subsequent request.<\/td>\n<\/tr>\n<tr>\n<td>Thread<\/td>\n<td><code>.InThreadScope()<\/code><\/td>\n<td>One instance of the type will be created per thread.<\/td>\n<\/tr>\n<tr>\n<td>Request<\/td>\n<td><code>.InRequestScope()<\/code><\/td>\n<td>One instance of the type will be created per web request, and will be destroyed when the request ends.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The first step would be to create a new NinjectModule which will hold the bindings.<\/p>\n<pre class=\"brush: csharp\">public class MainModule : NinjectModule\r\n{\r\n public override void Load()\r\n {\r\n   Bind&lt;TheManager&gt;().ToSelf().InSingletonScope();\r\n }\r\n}\r\n<\/pre>\n<p>Our <strong>TheManager <\/strong>class<strong> <\/strong>looks like that:<\/p>\n<pre class=\"brush: csharp\"> public class TheManager\r\n{\r\n    public TheManager()\r\n    {\r\n\r\n    }\r\n    public  DoManage()\r\n    {\r\n        System.Console.WriteLine(\"Resistance is futile\");\r\n    }\r\n}<\/pre>\n<p>Now we can use our <strong>TheManager <\/strong>class as a singleton.<\/p>\n<p>The simplest use would be if you ask the Ninject kernel for the <strong>TheManager <\/strong>class<\/p>\n<pre class=\"brush: csharp\"> class Program\r\n  {\r\n    static void Main(string[] args)\r\n    {\r\n      IKernel kernel = new StandardKernel(new MainModule());\r\n      TheManager myManager = kernel.Get&lt;TheManager&gt;();\r\n    }\r\n}\r\n<\/pre>\n<p>Whenever you call the Ninject kernel for the <strong>TheManager<\/strong> type implementation you will<strong> <\/strong>get a reference to the same instance.<\/p>\n<p>But Ninjects magic can do a lot more cooler stuff for you.<\/p>\n<p>Let&#8217;s suppose you have another class <strong>TheBoss<\/strong> which should use <strong>TheManager<\/strong>. And you want that if you instantiate the <strong><\/strong><strong>TheBoss <\/strong>it should automatically have a <strong>TheManager.<\/strong><\/p>\n<p>Then you could use the <strong>[Inject]<\/strong> Attribut. Whenever you use this Attribut Ninject will search his binding &#8220;tables&#8221; for an implementation which suits to the request context respectively to the bindings.<\/p>\n<p>For example you can use the\u00a0 <em>Property Injection<\/em> feature. Here we annotate the <strong>myManager <\/strong>property with <strong>[Inject]<\/strong> . So if we create an instance of\u00a0 <strong>TheBoss <\/strong>we also get <strong>myManager<\/strong> automatically resolved.<\/p>\n<pre class=\"brush: csharp\"> public class TheBoss\r\n{\r\n    [Inject]\r\n    TheManager myManager { get; set;}\r\n\r\n    public TheBoss()\r\n    {\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>So what&#8217;s Ninject.\u00a0 Ninject is a lightweight dependency injection framework for .NET applications. &#8230; and what is it good for (absolutely nothing?)? Ehm, not at all! The simplest use case would be following scenario: For example your design declares a singleton class. Normally you would code something like that: public sealed class TheManager { static &hellip; <a href=\"https:\/\/blog.fh-kaernten.at\/wehr\/2010\/04\/27\/ninject-the-first-contact\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Ninject, the first contact.<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"ngg_post_thumbnail":0,"footnotes":""},"categories":[4,3],"tags":[],"class_list":["post-17","post","type-post","status-publish","format-standard","hentry","category-net-stuff","category-programming"],"_links":{"self":[{"href":"https:\/\/blog.fh-kaernten.at\/wehr\/wp-json\/wp\/v2\/posts\/17","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.fh-kaernten.at\/wehr\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.fh-kaernten.at\/wehr\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.fh-kaernten.at\/wehr\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.fh-kaernten.at\/wehr\/wp-json\/wp\/v2\/comments?post=17"}],"version-history":[{"count":0,"href":"https:\/\/blog.fh-kaernten.at\/wehr\/wp-json\/wp\/v2\/posts\/17\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.fh-kaernten.at\/wehr\/wp-json\/wp\/v2\/media?parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.fh-kaernten.at\/wehr\/wp-json\/wp\/v2\/categories?post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.fh-kaernten.at\/wehr\/wp-json\/wp\/v2\/tags?post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}