Thursday, 4 July 2013

Ninja Coders for MvvmCross Part 4



One of the best things about MvvmCross (and Mvvm) is its testability.

With the MvvmCross architecture I can test most of my code without the need for a phone (I’m assuming phone development is your focus!).


One of the common excuses for not doing unit testing is 'I haven’t got time' - I’ve been guilty of this too, the problem for me is lack of decent tooling for creating unit tests.

It’s too time consuming - creating unit test projects, unit test files, sorting out the mocking of the interfaces blah, blah.

I’m not going to get into a debate about unit testing - there is better info out on the interweb that can explain the pros and cons of doing it.
I will say one thing though when you are doing multi-platform development that require hard to use equipment like phones I think you will find with a bit of effort and discipline doing unit testing will help enormously and speed your development time not slow it down.

You probably saying 'That’s great Adrian thanks for the lecture - but I know all that, so what’s this go to do with the Ninja?'

I’m glad you asked :-)

I want to take some of the time consuming steps out of doing unit testing so here is what I have in the pipeline for Ninja Coder for MvvmCross v1.2.0 coming out very soon (next couple of days hopefully!).

1) I switched the Testing project template from MsTest to NUnit (there is hardly any difference between the two but I agreed to switch)

2) Provided the test project template with Mocking support using Moq.

3) Created a BaseClass which all unit test classes derive from - this means some of the laborious tasks are done in the base class and don’t need to be done again.

4) Created a Mock Dispatcher (required for the unit testing to handle ShowViewModel requests amongst other things).

5) When you create a new MvvmCross Solution it will create a test project for the tests (this isn’t actually new but just wanted to re-iterate the point).

6) When you create a new ViewModel and Views from the Ninja it will create a TestViewModel file in the test project by default.

7) When you add plugins to your MvvmCross solution it will do a couple of things.

a) The viewmodels will now have their constructors amended automatically to inject the interfaces required to use the plugins.

b) **Relevant (stub) tests will be added to the TestViewModel file in step 6 - all interfaces will be mocked up ready to go.

** Work in progress on this one I can see more being done in this area.

8) The soon to come Add Service functionality will follow the same pattern as the testing support for Add plugin.

The above isn’t going to make unit testing a zero effort activity but should make things a LOT easier.

As always if people can see ways of improving this then get in touch!

Thursday, 23 May 2013

Ninja Coders for MvvmCross Part 3


ViewModels and View

Mvvm is primarily built around ViewModels and Views. 

With MvvmCross you have a view for each UI plaform and a ViewModel that resides in the Core library.

ViewModels and Views are not particularly hard to create manually - But Ninja Coder does make it slightly easier by creating Visual Studio item templates that implement base class for ViewModels and the UI Views and linking all views to the viewmodel.

Below the 'Add View Models and Views' screen is shown.

 

Again the View Model and View item templates can be used with out the Ninja Coder Addin.
 

Converters

Converters are very much a work in progress (some people, myself included would say the whole project is a work in progress!). At the moment it will add a relevant converter file as shown in the screen shot below and add the file to the Converters folder in the Core project.

Again the Converter item templates can be used with out the Ninja Coder Addin - can you sense a theme here?  




Plugins

Now things start to get interesting :-)

Will update when i get time :-)


Thursday, 9 May 2013

Ninja Coders for MvvmCross Part 1


 

This blog post is in the wrong order - what should come before this one about MvvmCross and the work Stuart Lodge has done.

Stuart's work with MvvmCross is excellent - it’s hard to imagine one person doing the work he has done - I know he has had help along the way - and I am a holder of an MvvmCross badge of awesomeness to prove it! - but he is the main developer and driving force behind it.

In a nutshell MvvmCross allows C# developers to use their Mvvm skills across multiple platforms hence the name.

More on MvvmCross in another blog post.

This rant is about the work I'm doing to try and utilise my current love of code generation/automation techniques and MvvmCross.

Stuart with help from another has managed to implement Nuget packages to create the bare bones projects to support MvvmCross development.

This is a huge step in the right direction but I feel I can add a little more to make it slightly easier to get up and running with MvvmCross - the problem with the Nuget approach alone it still leaves a few manual tasks that in an ideal world would be automated - and this is where 'Ninja Coders for MvvmCross' steps up to the plate.

So what am I baffling on about?

This will be an ongoing process of trying to make MvvmCross development easier via automation.

The first part is to create the project templates - these will be:-

  • MvvmCross.Core
  • MvvmCross.Core.Tests
  • MvvmCross.iOS
  • MvvmCross.Droid
  • MvvmCross.WindowsPhone
  • MvvmCross.Wpf
  • MvvmCross.WindowsStore


Hopefully the names explain themselves!

I did try to integrate the project templates directly with the Nuget MvvmCross packages but after failing miserably and wasting lots of time I gave up that approach. 

What I then decided to do was what I would have done had the Nuget packages for MvvmCross not been created, which was to create a 'Lib' directory, adding in all the assemblies to be referenced into this folder. This approach means that when I export the project to a template this folder and its contents are exported making the references still valid.

I may revisit the Project Template/Nuget issue at some point - but at the moment I'm favouring keeping the templates as they are as it just seems a little neater and more controlled which also make it simpler to version and source control. Just my opinion!

That is enough talking for now I need to get back the code and finishing off the templates.



Ninja Coders for MvvmCross Part 2




So I have now created all the visual studio project templates for Code Ninja for MvvmCross.

Before we move on to the templates it is worth mentioning that PCL (Portable Class Libraries) support is not complete in Visual Studio for Android and iOS development. Ninja Coder for MvvmCross installs the missing profiles files to allow PCL support - which saves developers a manual task of copying files :-)

Below we can see all the new project templates waiting at the starting grid!





The next step is to link all those project templates together via a visual studio addin to make the creation of an mvvmcross multi-project solution as simple as clicking on the tools menu in visual studio selecting the submenu 'Ninja Coder for MvvmCross/Add Projects' as shown below.







*Note:- The project templates can be used on their own without the use of the studio addin - it will just mean a few manual tasks will need to be completed to get the solution to compile.

Selecting this option shows the screen shown below. From here we specify the name of the project, select which projects we require, press OK and watch the magic happen.





After pressing OK we will be presented with all the projects created and all references to the core project added as shown below.



The first version of the Studio AddIn has now been packaged up into an MSI and put on the Visual Studio Gallery - see link below.


Ninja Coder  for MvvmCross Visual Studio Gallery Page


Thursday, 25 April 2013

Xamarin, C# and mobile development





I had read about Xamarin (called Ximian I think at the time) back in the days of them getting C# to run on Linux but didn't really pay too much attention as I was a Windows/Mac guy who wasn't too interest in Linux apart from the occasional install and 10 minute play with Ubuntu.

I think I really sat up and took notice when they announced support for C# development for the iPhone in 2009, around this time I was attempting to get my head around objective c, Xcode, interface builder and all things apple development.

I did managed to get an iPhone app up and running in objective-c but I wasn't about to become a fully paid up member of the apple developer community (though i did buy the apple developer license)

I had an affair with HTML/JavaScript for mobile development before landing at the feet of Xamarin and their mobile development offering.

Xamarin is the love child of Miguel de Icaza and Nat Friedman, they have had rough ride with their child, some unloving grandparents called Novell and Attachmate didn't see the value of this C# baby but Miguel and Nat did. After being laid off by Attachmate they set up their own company as they were convinced that their child had a future.




Xamarin offers the ability to write C# software that will run on Windows, Mac, iOS, Android and Windows Phone (others platforms probably available!) - I don’t think it should be underestimated what a feat of engineering this is.

It’s almost as if Xamarin is in the middle of Apple selling Macs, Microsoft selling Windows and Google/Android phone makers selling Google services and phones - that’s not a group of companies you would want to be in the middle of should the lawyers come knocking at the door!

Miguel is obviously a seriously talented guy who appears likeable and respected - his company Xamarin is one of those companies that deserves respect and to be honest our money if you want to do cross platform development be it mobile or Windows/Mac.

The best advice I can give is download it, play with it, produce great apps and make money :-)

The deal winner for me with Xamarin is its Visual Studio plug in on the Windows platform. That means you can produce your cross platform C# code in the comfort of your favourite IDE.
Because you are using Visual Studio you can take advantage of all your favourite tools and plugins like ReSharper, TFS, StyleCop and more.

The recent release of Xamarin 2.0 has taken things to the next level - You can now build iOS apps directly from visual studio. For me this is huge, sat in my comfy chair with ReSharper and StyleCop wagging their finger when i do something wrong is a brilliant productivity gain. On a side note I disabled ReSharper today to see if my Visual Studio addin I was creating would run quicker, I felt like a swimmer without armbands - I intend to blog about ReSharper soon.

Not only can you build iOS apps from Visual Studio, you can also run them and debug them - You still need a Mac to do this as its part of Apple's license to build their apps, but the way
Xamarin has done this is spectacular. It remotely connects to the Mac via a service running in the background called Mac Build Server - I'm not sure how they have done this but it allows
you to step through the code still sat in that comfy visual studio chair.


If only Microsoft showed the same love towards C# as Xamarin the world would be a better place.



Tuesday, 23 April 2013

A C# dinosaur in a mobile world



As a C# developer of many years I've come to think of myself of a bit of a dinosaur in this brave new mobile world.

With the love from Microsoft around c# waning away with the demise of WPF and Silverlight and new kids on the block like HTML5/JavaScript being welcomed to the Windows8 party, C# developers start to question their future.

I've spent many long evening trying work out how the hell this WPF data binding, command routing, converters and view models are supposed to work, I'm no way an expert on WPF but I've come through the other end with a decent understanding of how things hang together.

In other moments I've tried to build an iPhone app with objective c - it was a painful experience - I paid my $99 dollars to be an Apple developer, bought several books, read many blogs and even signed up for the on-line Stanford iPhone course.

Xcode is no Visual Studio and Interface Builder is not my UI builder of choice - I still don’t understand outlets.

I'm not interested if c# or objective c is better, I'm interested in what’s best for me and it isn't objective c.

I did manage to build a fairly complex app and got it running on my phone and felt fairly pleased with myself until a non-technical friend said to me ‘Can you put on my Android phone?’

<Sigh> - No way was I going through that again in Java!

Still determined to crack this mobile development I turned to HTML5/JavaScript – after all this means I could build for both iOS and Android.

Now I quite like JavaScript especially JQuery and the assorted libraries - so I went into this in a positive frame of mind that this was the way to go.

It was easy to knock together a HTML app that ran in the browser, but it sort of felt easy to do it wrong - it reminded me of my early visual basic days when it was easy to put together (badly) a windows app.




You can’t argue that developing mobile apps in HTML and JavaScript is definitely an option and for many people the right option.

I used PhoneGap (other frameworks are available) that allowed HTML apps to run locally on the phone.

Somehow the app still felt like it was a HTML app and not a native app – like I say I was probably doing things wrong but I wasn't happy with what I had produced. Building a HTML app for mobile felt like I was doing it this way because it was the easiest not the best.

I saw one quote that said building mobile apps in HTML was guaranteed to be cr*p on all platforms – I think that is unfair but I understand the sentiments.

I still yearned to use C# to do mobile development, I wanted to take the familiarity of using my tools of choice, things like Visual Studio 2012, ReSharper, TFS, my own plugs in, code generators, templates, code snippets and network of people and websites built upon over the years.

What would be even better is if I could take all those painful long nights learning data binding etc. with WPF into my mobile development world.....

Enter stage right Xamarin and MvvmCross