Skip to content

rickseven/dotnet-ninject-sample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Description

CoffeeApp is an example of a simple console application that applies Dependency Injection using Ninject.

Before continuing, I hope you know what Dependency Injection is.

There are three projects in this solution :

1. Coffee.Core

Coffee.Core is a core library that provides coffee service.

2. Coffee.Core.Test

Coffee.Core.Test is a unit test for Coffee.Core library that uses NUnit.

3. CoffeeApp

CoffeeApp is an application that uses the Coffee.Core library.

Usage

We will apply Coffee.Core to our application, say we have a simple application called CoffeeApp.

Ok, to call the service provided by Coffee.Core we will use Ninject.

Let's say we have a Beverage class that is dependent on the Coffee.Core service. The code is as follows :

using Coffee.Core.Services;
using System;

namespace CoffeeApp
{
    public class Beverage
    {
        private readonly ICoffeeService _coffeeService;

        public Beverage(ICoffeeService coffeeService) {
            _coffeeService = coffeeService;
        }

        public void GetIngredients() {
            var cup = _coffeeService.GetCoffee();
            foreach (string ingredient in cup.ingredients)
            {
                Console.WriteLine("- " + ingredient);
            }
        }
    }
}

To apply it, create a module called CoffeeModule that extends to NinjectModule. Then in the Load method, service bindings will be injected.

using Coffee.Core.Services;
using Ninject.Modules;

namespace CoffeeApp.Modules
{
    public class CoffeeModule : NinjectModule
    {
        public override void Load()
        {
            Bind<ICoffeeService>().To<CoffeeService>().WhenInjectedInto<CreamCoffeeService>();
        }
    }
}

We can call the service, as in the following code snippet :

IKernel kernel = new StandardKernel(new CoffeeModule());
ICoffeeService coffeeService = kernel.Get<CoffeeService>();
var coffeeBeverage = new Beverage(coffeeService);

Requirments

Coffee.Core
  • NET Standard Framework (v1.1)
Coffee.Core.Test
  • NET Core Framework (v2.2)
  • Ninject (v3.3.4)
  • NUnit (v3.11.0)
CoffeeApp
  • NET Framework (v4.5)
  • Ninject (v3.3.4)
  • Coffee.Core

About

CoffeeApp is an example of a simple console application that applies Dependency Injection using Ninject.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages