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

Xamarin Forms Core components Part 1 Dependency Service

Intro Xamarin Forms is a collection of controls that can be used and rendered on multiple platforms, and in order of them to function as they are suppose to, they do need a set of core components that defines the way these controls, in how they are created, how they are rendered to how they are used, of course every platform is different and sometimes a platform specific extra configuration is required, specially that there are so many differences between the different platforms in matter of design, user experience and operating system behavior. So one of the core components of  Xamarin  Forms is the Dependency Service, and by the name suggest it does act as the dependency resolver for all forms controls, if you are not familiar with IOC " I nversion O f C ontrol" and Dependency Injection please refer to the link for a quick intro i wrote a while ago on IOC  IOC Part 1 - Inversion of control Principle .  And as of such the Dependency Service is the concrete inboxe

Xamarin Forms: XAML Creating Custom Controls The MVVM Way

Intro For a growing UI page there always comes a need to create sub views that can be used inside a bigger view, and for that we need custom controls, which are controls that are derived either from a layout or a simple view which is the basic control for almost any UI component in Xamarin forms, and therefore for a start i will use that as an example. And in such example we will create a custom control with a set of bindable properties and explain how they work, also how to add an event that can also be bound to Commands Lets Create the control  I have just went ahead and created a custom control that inherits from Xamarin.Forms.View and have some properties such color and checked all properties to be bound to by the ViewModel serving as the datacontext of the this control. here is the code below using System; using Xamarin.Forms; namespace UserControls { public class CustomView : View { public Color Color { get ; set ;

Windows Phone 8 - Application bar command binding MVVM

This is a short post in which i will explain how on Windows phone 8 to bind the application bar button or menu item, first this is only a fix for the  BindableApplicationbar  which supports windows phone 7 only, i just made it support windows 8 no features added or anything. i have uploaded the dll file here so it can be accessible easily here is the link to download http://sdrv.ms/RApUal now that you got the link lets check how we gonna use it you can refer to BinableApplicationbar  or check out the code here that i actually used in my app and i already read it there !  Add a reference to the BindableApplicationBar library here is the link again  http://sdrv.ms/RApUal Add XML namespace declaration in your page's XAML: xmlns : bar ="clr-namespace:BindableApplicationBar;assembly=BindableApplicationBar" Set Bindable.ApplicationBar property on your page code as in the snippet below: < phone : PhoneApplicationPage >     …     < bar : Bindable.App