Skip to main content

IOC Part 3 – Using Ninject

Open visual studio and click new Project and choose a console application and name it Ninject.Console you should have your solution like that



Now lets get the Ninject, I prefer Nuget If you don’t have it installed get it from here http://nuget.codeplex.com/releases or you can search for it in the VS extensions Tools->Extension manager. Open Nuget by right clicking the reference folder in the solution and click manager nugget packages and then search for Ninject and click install Package see screen shot below



Configuration part
Depending on the type of you app you need to set the configuration cause they must take place at the starting of the app, and since we are in a console application then the most convenient place is the program.cs. there we must create our standard kernel which is the handler which does all the binding.
Binding is the operation of registering a type to its interface “will be explained by code later”
So for now we need to create a standard kernel check below in programs.cs
//A build error will occur stating the construct for standard kernel take 1 parameter
var kernel = new StandardKernel();

As it says the a build error will say that he constructor need a kernel module to operate
What is the kernel module : It holds all the configuration for the types created before so for instance it hold that object A is the implementation for interface IA
How do we create it ?
Like this
Since it is only gonna be used in this assembly I am going to define it as internal so add a new class and call NinjectBootstrapper and implement the kernelmodule interface and override the method load cause there we will put our configuration AKA bindings


internal class NinjectBootsrapper : NinjectModule
    {
        public override void Load()
        {

        }
    }

Now Let’s Create our derived types
First create a class called Processor and an interface for it called IProcessor as below



Now lets add the Configuration to Ninjectbootstrapper like below
 public interface IProcessor
    {
        /// <summary>
        /// Process something 
        /// </summary>
        void Process();
    }

    public class Processor : IProcessor
    {
        /// <summary>
        /// Process something 
        /// </summary>
        public void Process()
        {
            System.Console.Write("Processing ............");
        }
    }
And then lets go back to program.cs and add some usage to it for processor like below
And the hit run notice the screen below WOW it works and I can resolve object based on its interface configuration see screen shot below



Thanks for reading

Comments

Popular posts from this blog

(AsyncWebClient) Async Webclient for windows phone 8

Using windows phone web client is pretty common but the thing is you can not use it using Async -> Await mechanism so i used threading to create an async functionality for The Download string and upload string methods here is the code below // Comment public class AsyncWebClient { public Task DownloadString(Uri uri) { var task = new TaskCompletionSource(); try { var client = new WebClient(); client.DownloadStringCompleted += (s, e) => { task.SetResult(e.Result); }; client.DownloadStringAsync(uri); } catch (Exception ex) { task.SetException(ex); } return task.Task; } public Task UploadString(Uri uri, string content) { var task = new TaskCompletionSource(); try { var client…

RuntimeBinderException cannot resolver property in Unit Test in case of dynamic return.. Solved

Xamarin UI Tests – Deep dive Part 1

IntroAs the title may describe this is the first of the series of articles that will cover the UI Test of Xamarin in a deep dive, we will start simple and then dig deeper as we go. Since this is very well the first of the series, it will mostly cover up the architecture and the testing technique that Xamarin UI Tests uses.

The Technology
Xamarin UI Test is an automation testing framework similar to selenium, Watir, Watin (.net), Robot and Sikuli, if you have used BDD (Behavioral Driven Development) or a more advanced TDD (Test Driven Development) approach at least you must have come across one of these frameworks, and in that perspective comes Xamarin UI Testing as an automation framework designed specifically for Xamarin Automation testing, For starter Xamarin UI Tests was not build from scratch, instead it was built on top of another UI Automation testing framework that is targeting android and iOS sepecifically which is Calabash, I have to say that this choice was made wisely as of n…