ASP.NET 5 Configuration

So I've been playing around with ASP.NET 5 and of course I encountered the new configuration model. And a lot of things are nicely documented. But one thing I could not find on the internet (maybe I am just bad at binging/googling): how to get these configuration settings in your controller/viewmodel.

So what is the problem? For starter you setup your configuration(s) in the Startup class, for example:

public Startup(IHostingEnvironment env)
{
  var configuration = new Configuration().AddJsonFile("config.json");
  Configuration = configuration;
}

This code sets the configuration as the Startup class's Configuration property. This property is not easily accessible in your Controller, which is a good thing.

What?! A good thing? Yes, because it you should not use Configuration directly anywhere else. Dependency Injection to the rescue!

First thing you want to do is to think about which configuration settings you need (per viewmodel/controller), and then wrap this into a class:

public class MemConfig
{
  public string FromMem1 { get; }
  public string FromMem2 { get; }
}

This example will wrap two settings from configuration.

Now I want need these settings in my viewmodel (or controller, same technique), so we will add the MemConfig class to the viewmodel as a dependency:

private MemConfig mem;

public ConfigurationViewModel(MemConfig mem)
{
  this.mem = mem;
}

Now we only need to tell dependency injection how to create MemConfig. We do this again in the Startup class:

public void ConfigureServices(IServiceCollection services)
{
  services.AddSingleton<MemConfig>((isp) =>
   new MemConfig
   {
      FromMem1 = Configuration["FromMem1"],
      FromMem2 = Configuration["FromMem2"]
   });
   services.AddTransient<ConfigurationViewModel>();
}

So how does this work. Each time my controller needs a viewModel I make DI inject my viewmodel into the controller. The viewmodel also has a dependency, so DI will inject a MemConfig instance into the viewmodel.

I configured DI to use a singleton instance for the MemConfig class, and I told it how it should be initialized from Configuration. This last part is centralized in the Startup class, and we don't need any specific configuration logic anywhere else.


What I remember about /Build 2015


The excitement was there again, in San Francisco, where Microsoft displayed their latest innovations, to us, humble developers.

So what do I remember from /Build? That Microsoft is really going cross-platform. It is now possible to develop on Windows with Visual Studio, but now you can also create .NET applications and web sites on Linux and Mac on .NET core using the brand new Visual Studio Code editor. To enable this they've build a new execution environment (DNX). 


For building ASP.NET 5 web sites Microsoft has integrated bower (which is a javascript package manager), gulp (a javascript task manager, for example to minimize your .css and .js files) and grunt (another task manager) into Visual Studio. What this means to us developers is that you can choose: you can develop on Windows with Visual Studio, or you can develop on Windows/Mac/Linux with your choice of tools. You can mix, someone using Visual Studio, someone not... it's up to you.


On the Windows 10 front, Microsoft now has a unified stack for building apps on desktop, tablet, phone, ... Of course not every device has the same capabilities.. This used to mean conditional compilation (!?). Now, in Windows 10, there are libraries that allow you to check if some capability is there (for example camera) and if it is not, the library still has stubs to the methods, except they don't do anything. This means one binary for all devices. But this was already common knowledge. 


Closing the "app gap": Microsoft showed a demo with a simple iOS game being recompiled into a Windows Phone app. Yes, take an Objective-C application (no Swift support) and recompile it to a Windows 10 app! And you can do the same for an Android app! Amazing! The question remains: how far can you take this? Are all standard iOS/Android libraries supported? Time will tell. I really hope that Microsoft can make all of this work.


Run your web site as a Windows 10 app: Microsoft also demonstrated that you can take your web site (I think this will only work for ASP.NET based web sites) and wrap it into an app, then modifying the web site to take advantage of Windows 10 features.


What really excited me at /Build was Windows 10 Continuum. Just imagine, you walk into your office, take out your phone and place it on your desktop. The keyboard, mouse and screen on your desktop connect to your phone, allowing you to continue working on your phone, but now with a full desktop experience. Later, going home, you take your phone with you and you can continue working on it on the train, except of course now you have the small screen experience. No more carrying around a bulky device!


Project Spartan, which is an internal name, now got its real name: Microsoft Edge. This new browser is available in the latest Windows 10 insider build. I ran it against html5test and got a score of 390. It looks like they still have some work to do, but hey, this is preliminary software...



Team Build has been redesigned. Anyone ever needing to customize team build will testify that using XAML based Workflow Foundation to describe the build process was far from simple. Adding custom steps was even harder... Now Microsoft has made it really simple to customize builds using tasks:

Easy customization

Including cross-platform tasks out-of-the-box:

Cross-platform builds

What is also really neat is the ability to compare the build definitions, so you can figure out what was modified in that build definition:

View the details


Microsoft has done a lot of work to integrate docker into azure. If you have no idea what docker is, look here. In a nutshell it allows you to take your code, wrap it into a container and that run that container anywhere... You can then connect your container with other containers to make things happen. You can also build workflows from containers...


Companies with a lot of small databases will be happy to learn that they can now save costs with Azure SQL Database elastic databases. I think (sorry) that it allows you to pool your databases into one database server (still nicely separated into your tenants) and configure the minimal cpu requirements for each database.  I'm not that into database stuff so more about this here.


The star of the show: HoloLens. This kind of -- almost science fiction -- hardware makes a lot of people drool. Of course! Allowing people to run inside a virtual environment and react with it. Architects, product designers, game builders, the applications -- once they become available -- will open up a whole new world.
Some people had the luck to try it out for themselves, I was not one of them :( 
But my colleague was, and his major comment was that it did not track where you were looking, it tracked where HoloLens was looking. I think they will have to work on iris tracking -- hoping the HoloLens device won't cost as much as a jet fighter's helmet :)