<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5026909408907045059</id><updated>2011-07-08T00:20:04.393-07:00</updated><title type='text'>Dependency Injection - NInject</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://ninject.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5026909408907045059/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://ninject.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Rohit Sinha</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5026909408907045059.post-3387774860507818392</id><published>2009-06-26T15:47:00.001-07:00</published><updated>2009-07-15T16:30:27.934-07:00</updated><title type='text'>Dependency Injections - NInject</title><content type='html'>There are many ways of &lt;a href="http://en.wikipedia.org/wiki/Dependency_injection"&gt;injecting dependencies&lt;/a&gt;, you can use &lt;a href="http://www.springframework.net/"&gt;Spring.NET&lt;/a&gt;, &lt;a href="http://structuremap.sourceforge.net/Default.htm"&gt;StructureMap&lt;/a&gt; etc. &lt;a href="http://ninject.org/"&gt;NInject &lt;/a&gt;is one way of achieving this. It encourages relying on code for dependency rather than depending on XML which always prone to human errors (http://ninject.org/). All you need to do is to configure NInject e.g. Bind&lt;&gt;().To&lt;&gt;();. In the following example I have implemented &lt;a href="http://en.wikipedia.org/wiki/Inversion_of_control"&gt;Inversion of Control&lt;/a&gt; with minimal changes using NInject:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Problem:&lt;/span&gt; Lets take an application that prints user info. The printed output needs to be different (text file, PDF etc.). It depends on how external party implements printing.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Solution:&lt;/span&gt;&lt;br /&gt;Define a printer Interface :&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Linq;&lt;br /&gt;using System.Text;&lt;br /&gt;namespace PrintInterface&lt;br /&gt;{&lt;br /&gt;public interface IPrintDriver&lt;br /&gt;{&lt;br /&gt;void Print(string Name, string Age, string Address, string Phone);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Write an implementation, say "Implementation1" doing nothing:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Linq;&lt;br /&gt;using System.Text;&lt;br /&gt;using PrintInterface;&lt;br /&gt;namespace PrintImplementation1&lt;br /&gt;{&lt;br /&gt;public class PDFPrinter : IPrintDriver&lt;br /&gt;{&lt;br /&gt;public void Print(string Name, string Age, string Address, string Phone)&lt;br /&gt;{&lt;br /&gt;//Do Nothing&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now bind the interface with implementation. I will include Ninject.Core library and extend StandardModule:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Linq;&lt;br /&gt;using System.Text;&lt;br /&gt;using Ninject.Core;&lt;br /&gt;using PrintInterface;&lt;br /&gt;using PrintImplementation2;&lt;br /&gt;namespace DICustomModule&lt;br /&gt;{&lt;br /&gt;public class CustomModule : StandardModule&lt;br /&gt;{&lt;br /&gt;public override void Load()&lt;br /&gt;{&lt;br /&gt;Bind&amp;amp;ltiprintdriver&gt;().To&lt;&gt; ();&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Let's create the main application. First create a simple class, pass IPrinterDriver as an argument in parametrized constructor. Call print method of the interface and pass User Info:&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Linq;&lt;br /&gt;using System.Text;&lt;br /&gt;using PrintInterface;&lt;br /&gt;&lt;br /&gt;namespace PrintMyInfo&lt;br /&gt;{&lt;br /&gt;class Print&lt;br /&gt;{&lt;br /&gt;private IPrintDriver _prnDrv;&lt;br /&gt;&lt;br /&gt;public Print(IPrintDriver prnDrv)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;_prnDrv = prnDrv;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void PrintInfo()&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;_prnDrv.Print("John", "25", "123 Fake Street, Dallas", "xxx xxx xxxx");&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Create a simple window forms, include Ninject.Core namespace, create an instance of CustomModule (bind IPrintDriver is to actual object). Create a kernel object of&lt;br /&gt;type IKernel by passing CustomModule, get the handle and call its Print method:&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.ComponentModel;&lt;br /&gt;using System.Data;&lt;br /&gt;using System.Drawing;&lt;br /&gt;using System.Linq;&lt;br /&gt;using System.Text;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;using DICustomModule;&lt;br /&gt;using Ninject.Core;&lt;br /&gt;&lt;br /&gt;namespace PrintMyInfo&lt;br /&gt;{&lt;br /&gt;public partial class Form1 : Form&lt;br /&gt;{&lt;br /&gt;public Form1()&lt;br /&gt;{&lt;br /&gt;InitializeComponent();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private void Form1_Load(object sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;CustomModule module = new CustomModule();&lt;br /&gt;&lt;br /&gt;IKernel kernel = new StandardKernel(module);&lt;br /&gt;&lt;br /&gt;Print printMyInfo = kernel.Get();&lt;br /&gt;&lt;br /&gt;printMyInfo.PrintInfo();&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;//This is entry point&lt;br /&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Linq;&lt;br /&gt;using System.Windows.Forms;&lt;br /&gt;&lt;br /&gt;namespace PrintMyInfo&lt;br /&gt;{&lt;br /&gt;static class Program&lt;br /&gt;{&lt;br /&gt;///&lt;br /&gt;/// The main entry point for the application.&lt;br /&gt;///&lt;br /&gt;[STAThread]&lt;br /&gt;static void Main()&lt;br /&gt;{&lt;br /&gt;Application.EnableVisualStyles();&lt;br /&gt;Application.SetCompatibleTextRenderingDefault(false);&lt;br /&gt;Application.Run(new Form1());&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now I need to print the output to text file (not PDF). Create another implementation of IPrintDriver:&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Linq;&lt;br /&gt;using System.Text;&lt;br /&gt;using System.IO;&lt;br /&gt;using PrintInterface;&lt;br /&gt;&lt;br /&gt;namespace PrintImplementation2&lt;br /&gt;{&lt;br /&gt;public class TextPrinter : IPrintDriver&lt;br /&gt;{&lt;br /&gt;public void Print(string Name, string Age, string Address, string Phone)&lt;br /&gt;{&lt;br /&gt;string printText=null;&lt;br /&gt;printText = Name + ", " + Age + ", " + Address + ", " + Phone;&lt;br /&gt;TextWriter tw = new StreamWriter("Print.txt");&lt;br /&gt;&lt;br /&gt;// write a line of text to the file&lt;br /&gt;tw.WriteLine(printText);&lt;br /&gt;&lt;br /&gt;// close the stream&lt;br /&gt;tw.Close();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Update the CustomModule and attach new object to IPrintDriver:&lt;br /&gt;&lt;br /&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Linq;&lt;br /&gt;using System.Text;&lt;br /&gt;using Ninject.Core;&lt;br /&gt;using PrintInterface;&lt;br /&gt;using PrintImplementation2;&lt;br /&gt;&lt;br /&gt;namespace DICustomModule&lt;br /&gt;{&lt;br /&gt;public class CustomModule : StandardModule&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;public override void Load()&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;Bind( &lt;&gt; ).To( &lt;&gt;);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;All I needed to do was to update the intermediate layer (CustomModule). Main program doesn't need to be recompiled.&lt;br /&gt;&lt;br /&gt;Isn't a simple approach!&lt;br /&gt;&lt;br /&gt;&lt;a style="display:none" href="http://www.codeproject.com/script/Articles/BlogFeedList.aspx?amid=6400742" rel="tag"&gt;  codeproject  &lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5026909408907045059-3387774860507818392?l=ninject.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://ninject.blogspot.com/feeds/3387774860507818392/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://ninject.blogspot.com/2009/06/dependency-injections-ninject.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5026909408907045059/posts/default/3387774860507818392'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5026909408907045059/posts/default/3387774860507818392'/><link rel='alternate' type='text/html' href='http://ninject.blogspot.com/2009/06/dependency-injections-ninject.html' title='Dependency Injections - NInject'/><author><name>Rohit Sinha</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
