dependency injection with ninject - overridethis/usgsclient GitHub Wiki

This recipe showcases how to use the UsgsLibrary using a IoC container, Ninject.

  1. Create a blank new Console Application using your code editor of choice.
  2. Install the necessary Nuget Libraries.
Install-Package Ninject
Install-Package GeoJSON.Net -version 1.0.60-alpha
Install-Package UsgsClient
Install-Package ConsolesTable
  1. Add the following class QuakeModel.cs to be used as a data model for the incoming data from Usgs.
namespace SampleApp 
{
    public class QuakeModel
    {
        public string Id { get; private set; }

        public string Description { get; private set; }

        public double Magnitude { get; private set; }

        public double Longitude { get; private set; }

	public double Latitude { get; private set; }

        public static QuakeModel MapFrom(Feature feature)
        {
            return new QuakeModel
            {
                Id = feature.Id,
                Description = feature.Properties["place"].ToString(),
		Magnitude = double.Parse(feature.Properties["mag"].ToString()),
                Longitude = ((Point)feature.Geometry).Coordinates.Longitude,
		Latitude = ((Point)feature.Geometry).Coordinates.Latitude
            };
        }
    }
}
  1. Replace the Program.cs with the following code.
using Ninject;
using Ninject.Modules;
using UsgsClient;
using UsgsClient.Quakes;
using ConsoleTables;

namespace SampleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var kernel = new StandardKernel(new Module());
            var svc = kernel.Get<IQuakesFeedService>();
            var quakes = (await svc.Summary()).Select(QuakeModel.MapFrom);   
            ConsoleTable
                .From(quakes)
                .Write();    
        }
    }

    public class Module : NinjectModule
    {
        public override void Load()
        {
            this.Bind<IQuakesFeedService>().ToMethod(ctx => Usgs.Quakes.Feed());
        }
    }
    
}
  1. Run and Debug.
⚠️ **GitHub.com Fallback** ⚠️