GithubHelp home page GithubHelp logo

rickseven / dotnet-ninject-sample Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 10 KB

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

C# 100.00%
ninject csharp-library dependency-injection

dotnet-ninject-sample's Introduction

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

dotnet-ninject-sample's People

Contributors

rickseven avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.