Click here to Skip to main content
15,943,131 members
Articles / Programming Languages / C#

Weather App with MVVM, IoC, DI and API

Rate me:
Please Sign up or sign in to vote.
5.00/5 (9 votes)
29 Jul 2016CC (ASA 2.5)5 min read 20.4K   9   7
This is an application made for Windows (Universal) 10 devices made with MVVM, IoC-containers and dependency injection. There is also an API from Yahoo and SQLite service.


I've made an application that works on Windows Universal 10 devices. It gives the forecast for several cities in this world by an API (Application Programming Interface) from Yahoo. It contains the MVVM (Model View View-Model) pattern (more specific MVVM light library) and IoC (Inversion of Control) architectures. I use also SQLite (Structured Query Language lite) database for storing the search results. But what are there? Here comes some information:

MVVM (Model View View-Model)

Architecture of MVVMMVVM is a software architectural pattern and it's built in three parts like it said. In your project, you have three parts: models, views and view-models like show on image behind.

The models give access the database, but for this project, I use APIs from Yahoo mapped into the folder Repositories.

View-models contains ICommands and some binding properties. All view models inherit form ViewModelBaseClass where the interface INotifyPropetyChanged has been implemented.

The last part are the views. This contains the XAML pages and data templates. The last one is stored into the folder DataTemplates.

Each part is communicated with each other:

  • The views with the view models
  • The view models with the models (and repositories)
  • The repositories access the API (or another data source)

In theory, this is correct, but in practical situations communicate the view also with the models. This must be for the data bindings of the element properties to the model properties.

On the image, there is also a fourth part named unit tests. These are tests you can make for testing your code. This is not implemented in the project and not explained in this article.

IoC-Container (Inversion of Control Container)

IoC containers are containers with basic functionality needed to register and resolve instances. For the project, we go use the SimpleIoc class from MVVM light. The methods we go use in these project are:

Name Description

Registers a given type with the possibility for immediate creation of the instance.


Provides a way to get an instance of a given type. If no instance had been instantiated before, a new instance will be created. If an instance had already been created, that same instance will be returned.

Remark: If the class is not registered, the method will return null.

For more information and methods, you can read the documentation on

There is also a default constructor without parameters that initializes a new instance of the SimpleIoc class.

Dependency Injection or DI

Dependency injection is a way of working that allow you to make code independent of the class where you are working on. For this, you can use interfaces. They defined only the signature of the methods, properties and variables. An example you can use is to use a dummy class when developing or a web service when deploying. Here, you've a code example:

public interface IDataSource
    IEnumarble<object> Get(); // used for get all the objects

public class DummySource : IDataSource
    public IEnumarble<object> Get()
        return new List<object> {
            new { Letter = "A", Number = "1" },
            new { Letter = "A", Number = "1" }

public class WebSource : IDataSource
    public IEnumarble<object> Get()
        //return all objects of a web service 

On the place where you call the Get() method, it doesn't matter what the class is. It could be the dummy class or the webservice.

Note: On the place where you call the method, don't use the classes but use the interface like code below:

IDataSource source = new DummySource();
IEnumarble<object> dummyData = source.Get();

source = new WebSource();
IEnumarble<object> webData = source.Get();

This code will work because the variable source, is independent from the class DummySource and WebSource because I've made a "wrapper" interface named IDataSource that has a signature of the methods.

Building the App

Making Of the Solution

In the solution, I've made three projects named:

  1. Data contains repositories for accessing the API. Download the NuGet Newtonsoft.Json package for this project.
  2. Models contain the classes we need.
  3. UWP what stands for Universal Windows 10 Project and has the following folders:
    • Assets is default folder added in the project and contains images for the logo into the Windows store.
    • DataTemplates for store data templates
    • Pages contains the pages we go use
    • Services contains the IoC container
    • ViewModels contains the view models and one folder
      • core for storing the core data

We also need some NuGet packages you can download from the NuGet manager. For the data project is this Newtonsoft.JSON and for the UWP project, we need MvvmLight.

Making of the Pages

If the UWP project is made, you see next items into that project:

  • Properties contain the properties of that project.
  • References contain the references needed for that project.
  • The folders we've just created.
  • App.xaml contains important recourses and information about your project.
  • MainPage.xaml is the first page that is shown when the application runs.
  • Package.appxmanifest
  • project.json
  • Wheater.UWP_TemporaryKey.pfx

First, we need MainPage.xaml.

Use of APIs (Application Programming Interface)

An API or Application Programming Interface is a service for getting routines, protocols, tools or data powered by other instances (like Yahoo) that software programmers can access into their programs, applications and websites. But can find by robots, Dominica, drones and much more.

The First URL We Need

First of all, we must access the API from Yahoo to get the data from. The URL (Uniform Recourse Locator) that we go use is this:*%20from%20weather.

I found the URL on You see the parameter q which is a SQL statement:

FROM weather.forecast
WHERE woeid 
IN (SELECT woeid
    FROM geo.places
    WHERE text = @placemame)

This will go select all the fields of the table weather.forecast where woeid (WOEID stands for Where On Earth IDentifier) is equal to the value of the select from the table geo.places where the text field is equal to variable @placename. This variable must have a value so that the URL is valid in this case, is this a string or for saying it in SQL language is this from the type nvarchar or text depending on the used SQL language by Yahoo.

In the URL above, you can also see a second parameter called format. For this example, we go use JSON (JavaScript Object Notation) as receiving value.

Convert JSON Code to C# Classes

If you navigate to the URL, you see this JSON code. Here, you've the forecast from March 1st, 2016 from Meenen, Belgium:

            "title":"Yahoo! Weather - Meenen, BE",
            "description":"Yahoo! Weather for Meenen, BE",
            "lastBuildDate":"Tue, 01 Mar 2016 6:00 pm CET",
               "sunrise":"7:28 am",
               "sunset":"6:30 pm"
               "title":"Yahoo! Weather",
               "title":"Conditions for Meenen, BE at 6:00 pm CET",
               "pubDate":"Tue, 01 Mar 2016 6:00 pm CET",
                   "date":"Tue, 01 Mar 2016 6:00 pm CET",
               "description":"\n<img src=\"\"/>
                        <br />\n<b>Current Conditions:</b><br />\nCloudy, 45 F<BR />\n<BR />
                        <b>Forecast:</b><BR />\nTue - Showers Late. High: 48 Low: 42<br />
                        \nWed - PM Rain/Wind. High: 48 Low: 36<br />\nThu - Showers/Wind. 
                        High: 44 Low: 34<br />\nFri - Showers. High: 45 Low: 31<br />
                        \nSat - PM Showers. High: 46 Low: 33<br />\n<br />\n
                        <a href=\"
                        Full Forecast at Yahoo! Weather</a><BR/><BR/>\n(provided by 
                        <a href=\"\" >The Weather Channel</a>)<br/>\n",
                     "date":"1 Mar 2016",
                     "text":"Showers Late"
                     "date":"2 Mar 2016",
                     "text":"PM Rain/Wind"
                     "date":"3 Mar 2016",
                     "date":"4 Mar 2016",
                     "date":"5 Mar 2016",
                     "text":"PM Showers"

Of course, you could make the correct C# classes manually, but on the internet, you have some easy tools that do this for you. One of them is (only to C#) or (many other languages) that convert all the code for you. Import the classes to your project by copy paste them and rename RootObject to YahooWeather.

Use the of API in the App

Make a new class into the data project named WebRepo and place this code for use of the API.

public class WebRepo
    public static async YahooWeather GetData(string placename)
            using (HttpClient client = new HttpClient())
                string url = $@"

                string yahooGeoJSON = await client.GetStringAsync(url);
                return JsonConvert.DeserializeObject<YahooWeather>(yahooGeoJSON); 
            return null;

Later, we'll make dependency injection for this class.


This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License

Written By
Student NMCT, HoWest Kortrijk
Belgium Belgium

I'm Hein Pauwelyn, I'm studying the bachelor education NMCT @ HoWest Kortrijk (a city in Belgium).

I'm programming with .NET, Android. I like things to learn about programming and helping other people.

Form February till June, I go do an internship by Poppr at Gent. I go make a project about Web VR.


Hein Pauwelyn

LinkedIn: Hein Pauwelyn | GitHub: HeinPauwelyn | Blog site:

Comments and Discussions

QuestionIs this application available to download? Pin
kkankala4-Aug-16 3:56
kkankala4-Aug-16 3:56 
Suggestionchange the title... it is "Weather" not "Wheater" Pin
safrasmim1-Aug-16 20:04
safrasmim1-Aug-16 20:04 
QuestionTitle Qn "Wheater" Pin
Vander Wunderbar31-Jul-16 12:01
Vander Wunderbar31-Jul-16 12:01 
PraiseNice Pin
SteveHolle29-Jul-16 6:04
SteveHolle29-Jul-16 6:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.