{"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 loading=\"lazy\" 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=\"auto, (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"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Ninject, the first contact. - Mario&#039;s TinyLab<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/blog.fh-kaernten.at\/wehr\/2010\/04\/27\/ninject-the-first-contact\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Ninject, the first contact. - Mario&#039;s TinyLab\" \/>\n<meta property=\"og:description\" content=\"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; Continue reading Ninject, the first contact. &rarr;\" \/>\n<meta property=\"og:url\" content=\"http:\/\/blog.fh-kaernten.at\/wehr\/2010\/04\/27\/ninject-the-first-contact\/\" \/>\n<meta property=\"og:site_name\" content=\"Mario&#039;s TinyLab\" \/>\n<meta property=\"article:published_time\" content=\"2010-04-27T11:07:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2011-02-15T10:37:14+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blog.fh-kaernten.at\/wehr\/files\/2010\/04\/NinjectLogo.png\" \/>\n\t<meta property=\"og:image:width\" content=\"450\" \/>\n\t<meta property=\"og:image:height\" content=\"137\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Mario Wehr\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mario Wehr\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/2010\\\/04\\\/27\\\/ninject-the-first-contact\\\/#article\",\"isPartOf\":{\"@id\":\"http:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/2010\\\/04\\\/27\\\/ninject-the-first-contact\\\/\"},\"author\":{\"name\":\"Mario Wehr\",\"@id\":\"https:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/#\\\/schema\\\/person\\\/8761b4d788b13b547de9ccd6504ce62d\"},\"headline\":\"Ninject, the first contact.\",\"datePublished\":\"2010-04-27T11:07:29+00:00\",\"dateModified\":\"2011-02-15T10:37:14+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/2010\\\/04\\\/27\\\/ninject-the-first-contact\\\/\"},\"wordCount\":380,\"commentCount\":0,\"image\":{\"@id\":\"http:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/2010\\\/04\\\/27\\\/ninject-the-first-contact\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/files\\\/2010\\\/04\\\/NinjectLogo-300x91.png\",\"articleSection\":[\".net Stuff\",\"Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/2010\\\/04\\\/27\\\/ninject-the-first-contact\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"http:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/2010\\\/04\\\/27\\\/ninject-the-first-contact\\\/\",\"url\":\"http:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/2010\\\/04\\\/27\\\/ninject-the-first-contact\\\/\",\"name\":\"Ninject, the first contact. - Mario&#039;s TinyLab\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/2010\\\/04\\\/27\\\/ninject-the-first-contact\\\/#primaryimage\"},\"image\":{\"@id\":\"http:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/2010\\\/04\\\/27\\\/ninject-the-first-contact\\\/#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/files\\\/2010\\\/04\\\/NinjectLogo-300x91.png\",\"datePublished\":\"2010-04-27T11:07:29+00:00\",\"dateModified\":\"2011-02-15T10:37:14+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/#\\\/schema\\\/person\\\/8761b4d788b13b547de9ccd6504ce62d\"},\"breadcrumb\":{\"@id\":\"http:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/2010\\\/04\\\/27\\\/ninject-the-first-contact\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/2010\\\/04\\\/27\\\/ninject-the-first-contact\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/2010\\\/04\\\/27\\\/ninject-the-first-contact\\\/#primaryimage\",\"url\":\"https:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/files\\\/2010\\\/04\\\/NinjectLogo.png\",\"contentUrl\":\"https:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/files\\\/2010\\\/04\\\/NinjectLogo.png\",\"width\":\"450\",\"height\":\"137\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/2010\\\/04\\\/27\\\/ninject-the-first-contact\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Ninject, the first contact.\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/#website\",\"url\":\"https:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/\",\"name\":\"Mario&#039;s TinyLab\",\"description\":\"About cool\\\/interesting Projects and Technologies\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/#\\\/schema\\\/person\\\/8761b4d788b13b547de9ccd6504ce62d\",\"name\":\"Mario Wehr\",\"url\":\"https:\\\/\\\/blog.fh-kaernten.at\\\/wehr\\\/author\\\/wehr\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Ninject, the first contact. - Mario&#039;s TinyLab","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/blog.fh-kaernten.at\/wehr\/2010\/04\/27\/ninject-the-first-contact\/","og_locale":"en_US","og_type":"article","og_title":"Ninject, the first contact. - Mario&#039;s TinyLab","og_description":"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; Continue reading Ninject, the first contact. &rarr;","og_url":"http:\/\/blog.fh-kaernten.at\/wehr\/2010\/04\/27\/ninject-the-first-contact\/","og_site_name":"Mario&#039;s TinyLab","article_published_time":"2010-04-27T11:07:29+00:00","article_modified_time":"2011-02-15T10:37:14+00:00","og_image":[{"width":450,"height":137,"url":"https:\/\/blog.fh-kaernten.at\/wehr\/files\/2010\/04\/NinjectLogo.png","type":"image\/png"}],"author":"Mario Wehr","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Mario Wehr","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/blog.fh-kaernten.at\/wehr\/2010\/04\/27\/ninject-the-first-contact\/#article","isPartOf":{"@id":"http:\/\/blog.fh-kaernten.at\/wehr\/2010\/04\/27\/ninject-the-first-contact\/"},"author":{"name":"Mario Wehr","@id":"https:\/\/blog.fh-kaernten.at\/wehr\/#\/schema\/person\/8761b4d788b13b547de9ccd6504ce62d"},"headline":"Ninject, the first contact.","datePublished":"2010-04-27T11:07:29+00:00","dateModified":"2011-02-15T10:37:14+00:00","mainEntityOfPage":{"@id":"http:\/\/blog.fh-kaernten.at\/wehr\/2010\/04\/27\/ninject-the-first-contact\/"},"wordCount":380,"commentCount":0,"image":{"@id":"http:\/\/blog.fh-kaernten.at\/wehr\/2010\/04\/27\/ninject-the-first-contact\/#primaryimage"},"thumbnailUrl":"http:\/\/blog.fh-kaernten.at\/wehr\/files\/2010\/04\/NinjectLogo-300x91.png","articleSection":[".net Stuff","Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/blog.fh-kaernten.at\/wehr\/2010\/04\/27\/ninject-the-first-contact\/#respond"]}]},{"@type":"WebPage","@id":"http:\/\/blog.fh-kaernten.at\/wehr\/2010\/04\/27\/ninject-the-first-contact\/","url":"http:\/\/blog.fh-kaernten.at\/wehr\/2010\/04\/27\/ninject-the-first-contact\/","name":"Ninject, the first contact. - Mario&#039;s TinyLab","isPartOf":{"@id":"https:\/\/blog.fh-kaernten.at\/wehr\/#website"},"primaryImageOfPage":{"@id":"http:\/\/blog.fh-kaernten.at\/wehr\/2010\/04\/27\/ninject-the-first-contact\/#primaryimage"},"image":{"@id":"http:\/\/blog.fh-kaernten.at\/wehr\/2010\/04\/27\/ninject-the-first-contact\/#primaryimage"},"thumbnailUrl":"http:\/\/blog.fh-kaernten.at\/wehr\/files\/2010\/04\/NinjectLogo-300x91.png","datePublished":"2010-04-27T11:07:29+00:00","dateModified":"2011-02-15T10:37:14+00:00","author":{"@id":"https:\/\/blog.fh-kaernten.at\/wehr\/#\/schema\/person\/8761b4d788b13b547de9ccd6504ce62d"},"breadcrumb":{"@id":"http:\/\/blog.fh-kaernten.at\/wehr\/2010\/04\/27\/ninject-the-first-contact\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/blog.fh-kaernten.at\/wehr\/2010\/04\/27\/ninject-the-first-contact\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/blog.fh-kaernten.at\/wehr\/2010\/04\/27\/ninject-the-first-contact\/#primaryimage","url":"https:\/\/blog.fh-kaernten.at\/wehr\/files\/2010\/04\/NinjectLogo.png","contentUrl":"https:\/\/blog.fh-kaernten.at\/wehr\/files\/2010\/04\/NinjectLogo.png","width":"450","height":"137"},{"@type":"BreadcrumbList","@id":"http:\/\/blog.fh-kaernten.at\/wehr\/2010\/04\/27\/ninject-the-first-contact\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.fh-kaernten.at\/wehr\/"},{"@type":"ListItem","position":2,"name":"Ninject, the first contact."}]},{"@type":"WebSite","@id":"https:\/\/blog.fh-kaernten.at\/wehr\/#website","url":"https:\/\/blog.fh-kaernten.at\/wehr\/","name":"Mario&#039;s TinyLab","description":"About cool\/interesting Projects and Technologies","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.fh-kaernten.at\/wehr\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.fh-kaernten.at\/wehr\/#\/schema\/person\/8761b4d788b13b547de9ccd6504ce62d","name":"Mario Wehr","url":"https:\/\/blog.fh-kaernten.at\/wehr\/author\/wehr\/"}]}},"_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}]}}