Web API Tutorials Part 2 : Translation PCL with Dependency Injection

Portable Class Libraries afford great benefit when it comes to sharing code across platforms/usages.  We can share a single library across our UWP App where it’ll run locally and on our Web API where it’ll execute behind our endpoint.  Dependency Injection has been made very accessible with the latest ASP.NET MVC Web API.

Outcome

The current MorseCoder.PCL targets only Windows 8.1 as it’s point of API Reference.  We’ll need to change that to extend it to the wider .NET Framework (we’re not doing anything prohibitive, so this isn’t a problem).  By the end of the post we’ll have updated our PCL to target what we require, and integrated it into the Web API Project using Dependency Injection to pull in an instance of our Translator from the IServiceCollection container.

Prerequisites

Implementation

 MorseCoder.PCL

As above, we’ll need to modify which platforms our MorseCoder.PCL project targets, we can do that through the Project Properties…

2016-03-05-14_33_46-MorseCoder-Microsoft-Visual-Studio.png
Morse Coder PCL Target Properties

Add in the .Net Framework 4.6 and hit OK.

The PCL should now be eligible to reference from our Web API, add it in, and hit Build to ensure it still Builds successfully…

2016-03-05-14_46_53-MorseCoder-Microsoft-Visual-Studio.png
Morse Coder PCL Reference

Controller

We can now start to use our Translation Implementation from the Controller.

Create an ITranslator interface and new up an instances of our AlphabetToMorseTranslator from the PCL (and add the appropriate Using statements):

public ITranslator _translator = new AlphabetToMorseTranslator();

Now we can implement our Get Method using the translator:

// GET api/values/input
[HttpGet("{input}")]
public string Get(string input)
{
return _translator.Translate(input);
}

Great, we’re done, give it a quick test drive if you like.

Dependency Injection

But let’s think about what we’ve done there.  Our AlphabetToMorseController knows too much about what it needs to do.  We went to the trouble of creating an Interface around our AlphabetToMorseTranslator, but we’re not really using it to our benefit here at all.  Wouldn’t it be nice to be able to inject our Translator into the Controller.  Fortunately Web API makes it really easy to do this…

There’s a ConfigureServices method on the Startup class that enables us to register our Translator as a Service with the Applications IServiceCollection.

Let’s add the Registration:

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();

services.AddSingleton<ITranslator, AlphabetToMorseTranslator>();
}

We’re adding our Interface in along with a reference to AlphabetToMorseTranslator, such that when an instance of Type ITranslator  is requested, an AlphabetToMorseTranslator will be returned.  (This will be a problem when we implement translation the other way, we’ll cross that bridge when we come to it!)

Let’s wire up our ITranslator Interface to pull an instance out of our Service Collection:

[FromServices]
public ITranslator _translator { get; set; }

Try it!

As in the previous post, hit your endpoint in a Browser and see what happens…

2016-03-05-15_21_01-MorseCoder-Running-Microsoft-Visual-Studio.png
Web API Translation

Success!

Summary

Where are we?

  • We integrated our MorseCoder.PCL Library;
  • Implemented Dependency Injection;
  • Implemented our GET Method using an ITranslator interface.

Where are we heading?

  • Implementing MorseToAlphabetController;
  • Deploy to Azure;
  • Consuming our new Endpoint from our UWP app;
  • Implementing a LiveTileUpdate;
  • Making the app User Experience (UX) more friendly.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s