Nico's digital footprint

I grew up in the nineties, that makes me awesome by default

Sessions at NxtGenUG

by Nico

Last week I had the pleasure of travelling to the wonderful country of the United Kingdom (home of The Doctor!) to do some talks about Windows Azure Mobile Services and Windows Phone 8. I did a talk at the user group in Coventry and in Manchester. The response was great, a lot of people were impressed about the possibilities and the ease of use of WAMS. For those of you who’re interested, here’s my slide deck and the demo I build (live coding, didn’t fail, hooray for me Glimlach).

Download the demo


Tags:

Community | WP8 | Azure

Using MyGet for those common classes

by Nico

Every .net developer, be it a mobile, web or windows developer, uses (or should use) NuGet. A big, and fast growing, gallery of useful (and less useful but fun) libraries. The problem I was having with NuGet is that for every project I needed to go search in that big gallery for the packages I wanted. Luckily someone had the same problem and decided to do something about it. And so MyGet was born.

The MyGet team sells MyGet as Nuget-as-a-service. You can create your own feed, drop in the NuGet packages you tend to use (even mirror them) and add the feed into your package manager. This way you get your own personal gallery, no more searching for the ones you need, those feeds are public by the way, everyone can use them. If you want a private feed you’ll need to look at the MyGet price plans.

Getting started with MyGet is really easy, just go to http://www.myget.org log in with your Microsoft, Google, Yahoo, Facebook, OpenID, GitHub or StackOverflow account. After you login you can add feeds to your account.

Creating a feed is as easy as giving it a name and a description and selecting the feed type (public, community or private). Once your feed is created you can start adding packages to it. To add a package enter the search term just like you would do when you use NuGet in Visual Studio, click the package you want and click Add.

If you want, in the Add package dialog, you can select to mirror the package. This will copy the entire package from the NuGet feed into MyGet, meaning that you’ll still be able to install the package when NuGet goes down. To give you an idea of how a feed could look like, here’s some of the packages in my Windows Phone feed.

To use your freshly created feed you’ll need to add it as a package source in NuGet. Go to the Package Manager settings from the Visual Studio options menu (or open the Manage NuGet packages dialog in a project and click Settings). Here you can add a new package source.

Once added, the new package source will show up in NuGet and you’ll be able to install the packages from you own feed.

Managing my own package feed has really reduced the time I used to spent searching and adding packages. But the MyGet team has been working hard on a really cool feature.

Do the Wonka

The MyGet team (all Belgian developers by the way – hooray for Belgium Smile) developed a feature they codenamed “Wonka”. It’s currently in Beta but it’s working great so far. This is what Wonka is all about

They’ve setup their own Build server, including support for building Windows Phone 8 libraries (thanks to the command line compiler in the WP8 SDK). Hearing about such a cool feature and hearing that it’s in beta made me want to try it. And I started thinking, maybe I could put a bunch of classes that I tend to use in some form in multiple projects in one library and have that library in my feed.

The project itself is still very small but it will grow when I need extra functionality, it currently holds a Regex class to get rid of HTML tags in strings and a RestClient class that uses the portable HttpClient to do a Get operation. For reference, here are the classes.

Code Snippet
  1. publicstaticclassRegexHelper
  2. {
  3.     publicstaticstring RemoveHtmlTags(thisstring text)
  4.     {
  5.         //add some layout
  6.         text = Regex.Replace(text, "<br />", "\n");
  7.         text = Regex.Replace(text, "</p>", "\n");
  8.  
  9.         //replace weird characters
  10.         text = Regex.Replace(text, "&amp;", "&");
  11.         text = Regex.Replace(text, "&nbsp;", " ");
  12.  
  13.         //remove remaining HTML tags
  14.         returnRegex.Replace(text, @"<[^>]*>", string.Empty);
  15.     }
  16. }
Code Snippet
  1. publicclassRestClient : IRestClient
  2. {
  3.     privateHttpClient _client;
  4.  
  5.     publicasyncTask<T> Get<T>(CancellationToken cancellationToken, string url, bool includeResultProps = true)
  6.         where T : new()
  7.     {
  8.         try
  9.         {
  10.             _client = newHttpClient();
  11.  
  12.             var result = await _client.GetAsync(url);
  13.             string json = await result.Content.ReadAsStringAsync();
  14.  
  15.             if (!includeResultProps)
  16.             {
  17.                 JObject obj = JObject.Parse(json);
  18.                 json = obj["results"].ToString();
  19.             }
  20.  
  21.             returnJsonConvert.DeserializeObject<T>(json);
  22.         }
  23.         catch (JsonSerializationException e)
  24.         {
  25.             returnnew T();
  26.         }
  27.     }
  28. }

The project contains Json.net and the portable HttpClient, both referenced through NuGet. Now we need a way for the build server to get a hold of these packages, we can accomplish this by enabling the NuGet package restore. Right-click the solution and you’ll see the option right there.

This will add a .nuget folder to your solution that you’ll need to check-in into your source control.

So we have a working project with some classes that we would like to get into a NuGet package by using the MyGet build server. What we need to do is get this package into a repository. I used BitBucket for this because it has free private repositories. You can also add a GitHub or Codeplex repository.

If you use the from BitBucket button it will ask you for permission to access your account on BitBucket and will show you a list of all your repositories hosted there. Select the one you need and click Add. Do take notice of the warning.

Add the repository and click the edit button next to it. Enter your BitBucket credentials here so MyGet can access your private repository to fetch the sources. And that’s it, you can now hit the Build button to fetch the sources and try to build them. In my case this immediately resulted in a big fat red Failed message. The problem here is something of a chicken-egg problem (a big thanks here to the MyGet team for helping me figuring this one out!). So here’s the problem:

  • The project file needs the package to run
  • The project file is needed for package restore
  • package needs package restore

The solution here is to setup a build.bat in the solution folder. The MyGet build server will search for a build.bat to execute, if it doesn’t find one it will just use the sln available.

With the build.bat file we first fetch the necessary packages and then build the solution

Code Snippet
  1. @echo Off
  2. set config=%1
  3. if "%config%" == "" (
  4.    set config=Release
  5. )
  6.  
  7. set version=
  8. if not "%PackageVersion%" == "" (
  9.    set version=-Version %PackageVersion%
  10. )
  11.  
  12. REM Run package restore
  13. %nuget% install WindowsPhone\packages.config -OutputDirectory packages -Prerelease -NonInteractive
  14.  
  15. REM Build solution
  16. %WINDIR%\Microsoft.NET\Framework\v4.0.30319\msbuild WindowsPhone.sln /p:Configuration="%config%" /m /v:M /fl /flp:LogFile=msbuild.log;Verbosity=Normal /nr:false

Add this to your solution in Visual Studio, make sure it’s called build.bat and check it into your source control. Now it should build.

Automating builds

Next step to make everything even more awesome is to add a hook to BitBucket so that MyGet will fetch and build the sources everytime a commit is pushed into the repository. First you’ll need the HTTP POST link from MyGet, you’ll find it underneath your build source.

Once you’ve got that link, go to the BitBucket repository and click the Settings icon, in the Settings go to Services and add a POST service.

Past the URL and save the service. Now everytime you push a commit to BitBucket a new build should get queued in MyGet (the page refreshes automatically to show the build process).

And just like that we’ve created a NuGet package in a MyGet feed with continuous delivery, how cool is this right? If I check my feed from Visual Studio my package shows up!

Conclusion

In this post I’ve explained how I use MyGet, a NuGet-as-a-service, to create a package feed with only the packages that are relevant for me. I’ve also explained how I’ve added my own library of classes that I use in multiple projects in that feed and setup continuous delivery so that the package gets updated with every commit that gets pushed to the repository.

Resources:

A big shout out to the MyGet team for helping me getting started with their awesome service! http://www.myget.org/Home/Team


Tags:

.Net | continuous delivery | myget | NuGet | PCL

Porting a real win8 app to WP8–part 2

by Nico

Porting a real win8 app to WP8 – part 1

In part one of my adventure I described the architecture of my app and how I managed to move all viewmodels into a PCL (portable class library) but I didn’t use them yet. The main consensus of part one was that I needed to do quite some refactoring and add a lot of abstractions to the project in order to move over viewmodels to PCL projects. In part 2 I’ll describe how I got the viewmodels to work and how I re-enabled page navigation in the Windows 8 version. And with a bit of luck in part 3 I’ll be talking about the Windows Phone 8 version of this app.

A small note on part 1

I wasn’t very happy with part 1 since I needed to create some extra layers and abstractions. Since then the Microsoft Techdays have come and gone and with that two very interesting sessions that made me rethink some stuff. The session were from Laurent Bugnion (his blog, his Twitter) (the creator of the awesome MVVM Light toolkit) and Gill Cleeren (his blog, his Twitter) (a Belgian Silverlight MVP, RD and Win8 enthusiast). From Laurent I learned some tips and tricks concerning the ViewModelLocator class and specifically how to correctly use it in a portable library (more on that in part 3 since I haven’t implemented it yet). Gill gave a session on advanced MVVM tactics and one sentence kept vibrating in my mind “an extra class doesn’t cost a thing” next to that his demo app was filled with layers of abstractions way beyond the extra layers I needed to add to get the project to build again. After that session I felt reassured that I was going down a pretty good path with the revised architecture I had build in part 1. If you want to take a look at that session yourself, the recording of the SilverlightShow version is available here.

About part 2

The last couple of days I found some spare time to mess around with the project again. I’m now at the stage where the Windows Store app is using the portable ViewModels and the navigation is working properly. Those parts are what I’ll discuss here.

Using the ViewModels

Having the portable ViewModels ready it was time to switch to them. I deleted every viewmodel that was still in my client project, except for the ViewModelLocator, that one will be moved later. I registered the new abstraction layers in the ViewModelLocator, making sure that this only happened at runtime, not at designtime and tried to build the app. I couldn’t believe my eyes when I saw the message “Build succeeded”. I replaced the viewmodels in my client project with the ones in the portable library and it still builds! In case you’re interested, this is what’s getting registered in my ViewModelLocator at this time.

Code Snippet
  1. public ViewModelLocator()
  2. {
  3.     ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
  4.  
  5.     if (ViewModelBase.IsInDesignModeStatic)
  6.     {
  7.         // Create design time view services and models
  8.         //SimpleIoc.Default.Register<IDataService, DesignDataService>();
  9.     }
  10.     else
  11.     {
  12.         // Create run time view services and models
  13.         SimpleIoc.Default.Register<INavigationService, NavigationService>();
  14.         SimpleIoc.Default.Register<IService, SDKBroker.WinRT.Service>();
  15.         SimpleIoc.Default.Register<IMessageApi, MessageApi>();
  16.         SimpleIoc.Default.Register<IFrameworkApi, FrameworkApi>();
  17.         SimpleIoc.Default.Register<IDispatcher, Dispatcher>();
  18.         SimpleIoc.Default.Register<INetworkApi, NetworkApi>();
  19.     }
  20.  
  21.     //register viewmodels
  22.     SimpleIoc.Default.Register<MainViewModel>();
  23.     SimpleIoc.Default.Register<VolumeDetailViewModel>(true);
  24.     SimpleIoc.Default.Register<CharacterDetailViewModel>(true);
  25.     SimpleIoc.Default.Register<TeamDetailViewModel>(true);
  26.     SimpleIoc.Default.Register<IssueDetailViewModel>(true);
  27.     SimpleIoc.Default.Register<SearchViewModel>();
  28.     SimpleIoc.Default.Register<DetailViewModel>(true);
  29.     SimpleIoc.Default.Register<StoryArcDetailViewModel>(true);
  30.     SimpleIoc.Default.Register<LocationDetailViewModel>(true);
  31.     SimpleIoc.Default.Register<PersonDetailViewModel>(true);
  32.     SimpleIoc.Default.Register<CollectionViewModel>();
  33.     SimpleIoc.Default.Register<NewsFeedViewModel>(true);
  34. }

Everything in the else block gets registered only at runtime. The viewmodels themselves can get registered at designtime just in case I ever want to incorporate designtime test data (something I really should do actually). Notice that some ViewModel registrations get passed a boolean parameter? The Register method of SimpleIoc has an optional parameter stating whether or not the object should get instantiated immediately. Since some viewmodels are listening for messages from the Messenger class in MVVM Light they need their instance right of the bat so they can register as listeners.

I tried to run the app, it started and data was coming in, I could use the button to show the search charm but no navigation was working. That made sense since all navigation commands are now going to ISomePage instead of SomePage and those interfaces weren’t doing anything yet. So I had to make every page implement the correct interface and put together a way to navigate to the correct page from the interface.

Implementing the interface is easy enough (they’re all just empty interfaces). Next problem was that those interfaces are inside of a folder called View in the ViewModel PCL and they need to be known in the Framework.WinRT project (that’s where the NavigationService lives). So I’ve added another PCL called it ComicDB.View and moved all interfaces there. (I couldn’t reference the ViewModel project in the Framework project, it would create a circular dependency). After adding all references everything was building again but still no navigation. To get this to work I changed the Navigate method on the NavigationService from this

Code Snippet
  1. publicvirtualbool Navigate(Type destination, object parameter = null)
  2. {
  3.     try
  4.     {
  5.         _rootFrame.Navigate(destination, parameter);
  6.  
  7.         returntrue;
  8.     }
  9.     catch (Exception e)
  10.     {
  11.         returnfalse;
  12.     }
  13. }

to this

Code Snippet
  1. publicvirtualbool Navigate(Type destination, object parameter = null)
  2. {
  3.     try
  4.     {
  5.         NavigateToPage(destination, parameter);
  6.  
  7.         returntrue;
  8.     }
  9.     catch (Exception e)
  10.     {
  11.         returnfalse;
  12.     }
  13. }
  14.  
  15. privatevoid NavigateToPage(Type destination, object parameter)
  16. {
  17.     try
  18.     {
  19.         //get the implementation for the view
  20.         var instance = SimpleIoc.Default.GetInstance(destination);
  21.         var type = instance.GetType();
  22.  
  23.         _rootFrame.Navigate(type, parameter);
  24.     }
  25.     catch (ActivationException)
  26.     {
  27.         //no registered type found, just navigate to the destination, maybe it's not an interface
  28.         _rootFrame.Navigate(destination, parameter);                                
  29.     }
  30. }

The Navigate method passes everything to the NavigateToPage method that tries to resolve a registered instance of the passed in type, should this fail it throws an ActivationException, in that case just try to navigate. This allows us to navigate to pages that don’t use any interface. Once we get the instance out of the IOC we get its type and pass that into the navigate command of the frame. Obviously, before this starts working we need to register the pages in the IOC and that happens in the ViewModelLocator.

So to finish this part 2 of, here’s the complete constructor of my ViewModelLocator

Code Snippet
  1. public ViewModelLocator()
  2. {
  3.     ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
  4.  
  5.     if (ViewModelBase.IsInDesignModeStatic)
  6.     {
  7.         // Create design time view services and models
  8.         //SimpleIoc.Default.Register<IDataService, DesignDataService>();
  9.     }
  10.     else
  11.     {
  12.         // Create run time view services and models
  13.         SimpleIoc.Default.Register<INavigationService, NavigationService>();
  14.         SimpleIoc.Default.Register<IService, SDKBroker.WinRT.Service>();
  15.         SimpleIoc.Default.Register<IMessageApi, MessageApi>();
  16.         SimpleIoc.Default.Register<IFrameworkApi, FrameworkApi>();
  17.         SimpleIoc.Default.Register<IDispatcher, Dispatcher>();
  18.         SimpleIoc.Default.Register<INetworkApi, NetworkApi>();
  19.     }
  20.  
  21.     //register views
  22.     SimpleIoc.Default.Register<IMainPage, MainPage>();
  23.     SimpleIoc.Default.Register<IVolumeDetailPage, VolumeDetailPage>();
  24.     SimpleIoc.Default.Register<ICharacterDetailPage, CharacterDetailPage>();
  25.     SimpleIoc.Default.Register<ICollectionPage, CollectionPage>();
  26.     SimpleIoc.Default.Register<IDetailPage, DetailPage>();
  27.     SimpleIoc.Default.Register<IIssueDetailPage, IssueDetailPage>();
  28.     SimpleIoc.Default.Register<ILocationDetailPage, LocationDetailPage>();
  29.     SimpleIoc.Default.Register<INewsFeedPage, NewsFeedPage>();
  30.     SimpleIoc.Default.Register<IPersonDetailPage, PersonDetailPage>();
  31.     SimpleIoc.Default.Register<IStoryArcDetailPage, StoryArcDetailPage>();
  32.     SimpleIoc.Default.Register<ITeamDetailPage, TeamDetailPage>();
  33.  
  34.     //register viewmodels
  35.     SimpleIoc.Default.Register<MainViewModel>();
  36.     SimpleIoc.Default.Register<VolumeDetailViewModel>(true);
  37.     SimpleIoc.Default.Register<CharacterDetailViewModel>(true);
  38.     SimpleIoc.Default.Register<TeamDetailViewModel>(true);
  39.     SimpleIoc.Default.Register<IssueDetailViewModel>(true);
  40.     SimpleIoc.Default.Register<SearchViewModel>();
  41.     SimpleIoc.Default.Register<DetailViewModel>(true);
  42.     SimpleIoc.Default.Register<StoryArcDetailViewModel>(true);
  43.     SimpleIoc.Default.Register<LocationDetailViewModel>(true);
  44.     SimpleIoc.Default.Register<PersonDetailViewModel>(true);
  45.     SimpleIoc.Default.Register<CollectionViewModel>();
  46.     SimpleIoc.Default.Register<NewsFeedViewModel>(true);
  47. }

It registers all my api and service layers, all my views that have an interface and all viewmodels.

Part 2 conclusion

After a rough start in Part 1 it seems that this experiment has forced me to improve my code by making me add some abstractions. This makes for easier reusable code, both on the same platform as on other platforms. The DEV branch of my app project is once again a fully functional Windows Store app that reacts and behaves exactly as the one that’s in the store right now. The next step is to refactor out the ViewModelLocator and then it should be about time to start work on the Windows Phone version. See you in part 3!


Tags:

.Net | Devices | Metro | MVVM Light | PCL | Windows 8 | WinRT | WP8

  Log in

About the author

Hi,

My name is Nico, I’m an MVP Windows Platform Development living in Belgium.
I’m currently employed as a .NET consultant at RealDolmen, one of Belgium’s leading IT single source providers.

I'm also founding member and board member of the Belgian Metro App Developer Network, a user group focussed on Windows 8 and Windows Phone development. If you're in Belgium feel free to drop by if we're doing an event. http://www.madn.be

Since June 2012 I'm a proud member of Microsoft's Extended Experts Team Belgium. And in February 2013 I became a member of DZone's Most Valuable Bloggers family.

In 2013 I became a book author and wrote "Windows 8 app projects, XAML & C# edition".

In 2014 I received the MVP award for the very first time.

I hope to get feedback from my readers either through comments, mail (nico_vermeir@hotmail.com), twitter, facebook, …

 

mvp

 

mvp

 

 

MeetLogo

 

MVBLogo

mybook

 

Month List