Archive for Microsoft

Pallet Town: Intro to the Repository Pattern MVC4

Pallet TownWelcome to Pallet Town! Pallet town is going to be an ongoing bi-weekly look at various development technologies and techniques from the perspective of someone new to that technology. For those who might know or (God forbid) are too young to get the reference, Pallet Town is the first town in the original Pokemon games for the Nintendo Gameboy.

I’ve been having a great time with Windows Azure  or more specifically ASP.Net MVC4. .Net developers will be familiar with the repository pattern, since it is a common pattern used in that community. Let’s take a look at a simple repository example using everyone’s favorite objects: Pokemon. To start we need a simple Pokemon class.

namespace PokeSample.Models
{
  public class Pokemon
  {
     public int Id { get; set; }
     public String Name { get; set; }
  }
}

We now need to create an interface:

namespace PokeSample.Models
{
  interface IPokeRepository
  {
    Pokemon Add(Pokemon pokemon);
    Pokemon Get(int id);
  }
}

And now it’s time for the repository itself:

namespace PokeSample.Models
{
  public class PokeRepository : IPokeRepository
  {
    private List pokemon = new List();
    private int nextId = 0;

    public PokeRepository()
    {
       // adding some test data
       Add(new Pokemon { Name = "Charmander"});
    }

    public Pokemon Add(Pokemon somePokemon)
    {
        if (somePokemon == null)
        {
           throw new ArgumentNullException("somePokemon is null");
        }

        somePokemon.Id = nextId++;
        pokemon.Add(somePokemon);

        return somePokemon;
    }

    public Pokemon Get(int id)
    {
       return pokemon.Find(p => p.Id == id);
    } 
  }
}

You’ll notice that we are seeding the ‘pokemon’ List and have creates some simple CRUD methods. The idea here is that you could call these methods in response to HTTP requests from the PokemonController. Assuming you’ve setup your dev environment correctly this should all work just fine on localhost.

However, there is one pretty obvious glaring issue with this implementation — the data is not persisted. Stay tuned for the part 2 of this series that will show how you can use the common DBContext (ok that’s my term but it’s catchy) pattern to persist your data using MSSQL or some other datastore.

Hope you enjoyed this first entry in Pallet Town and I would appreciate any feedback you might have to offer. Please feel free to contact me on Twitter or Google+.

Time to Divide the Empire

Last week I mentioned that Microsoft is in danger of becoming IBM. This got a good deal of response from the Microsoft fans on Twitter and it was of course not meant to be provocative; in fact, I was just stating the fact that the recent releases of Windows RT and Windows 8 have been less than phenomenal.  IBM faced a similar situation in the early PC days when, after having been beset by clone makers who were able to sell compatible machines without the albatross of Big Blue’s substantial R&D investment, they tried to continue to compete in an already saturated and frankly lost market.

With that said, not all of Microsoft’s recent releases have been so poorly received — Windows Azure seems to be doing extremely well. In fact, their recent addition of Oracle’s software (Java included) to the Azure platform is likely to contribute to the platform’s continued growth. Also, it is important to remember that a lot of enterprises run their business on Sharepoint, Microsoft Exchange,  and Windows Server.

The success of the Xbox business is debatable, since Microsoft has been loathe to break down how much that division takes to run and how much revenue it has generated for the larger company; in fact, the lack of corporate boasting suggests that any profits generated by the Xbox business are modest when compared to the company’s other divisions.

What’s not debatable is the abject failure of Windows RT. When you think about it, WinRT is a strange bird. In a lot of ways it is designed to solve a problem (battery life) that Intel has largely solved with its upcoming Haswell line of processors but makes a number of trade-offs, such as not being able to run traditional Windows software, that the market has been unwilling to accept. Given the time in which WinRT was developed and when it was released it is understandable why those concessions were made but the market was confused by the constant drum beating of “it’s just Windows” — an assertion that in reality was dubious. Worse still, the existence of WinRT has not stopped enterprises (Microsoft’s primary customers) from deploying iPad’s and Android tablets.

Windows Phone suffers from the same issues in the enterprise that WinRT does and, like WinRT, is desperate need of developer support. The install base for Windows Phone is nothing to brag about. Ironically, Windows Phone is one of the most innovative and frankly one of the best products that Microsoft has released on the consumer / client side; to clarify, we are talking about Windows Phone 8 here not Windows Phone 7 and certainly not Windows Mobile. Unfortunately, it was just too late to the market and the delay allowed iOS and Android to become entrenched.

One business Microsoft still dominates is the desktop OS business, but that has more to do with the other major players not being terribly interested in that market,  consumer inertia, and of course being the standard desktop OS for enterprises. It hardly seems wise to boast about winning a race that was won decades ago; especially, when the trajectory of technology looks to be making your choice of desktop OS less and less relevant.

Looking at all of these business, there seems to be a clear pattern: Microsoft does well in enterprise and backend markets, but is increasingly weak in the consumer market. At this point, would it not make sense for them to just dump or split off the consumer side of things and focus on their competency? Imagine and Xbox company that didn’t need to worry about the priorities of the other divisions; doesn’t the recent refusal to allow self publishing by independent developers and the response given by Microsoft (which boils down to “look at Windows 8 / WinRT” first) make it look like the Xbox business is being held back for the benefit (or perceived benefit) of the Windows division? I have a sinking suspicion that if we took a look at other decisions Microsoft has made in other divisions, we’d find that a lof of sacrifices are being made to keep the Windows client (Windows 8 / WinRT) safe — Office for iPad anyone?

 

Blue == IBM

azure-logoMicrosoft has been having a bad time in the consumer face for the last few years — they’ve failed to jump on the mobile train in any significant way and have tried to modernize Windows but face a lot of resistance from users and pundits. Personally, I like Windows 8; the changes in 8 were bold and necessary to bring Windows into the current day, though it does still feel a bit stodgy. Windows 8’s next update (Blue) is, however, a step in the wrong direction.

Sure the (relevant) changes in Blue are relatively small — bringing back the start button and allowing boot to desktop — but these changes show the core problem with Windows ever being a successful consumer brand — the enterprise market. Microsoft makes a ton of money in the enterprise space and (naturally) they do not want to upset some of their best customers. Though those changes might sound small, they are made with the sole purpose of placating enterprise customers who fear having to “train” their users; as an aside, I have personally seen enterprise office workers pretend to not how to do something in Office to avoid doing a task or delay other work functions and, in general, it has been my experience that many IT departments provide far too much “training” when a simple video course would suffice.

Even if the changes, somehow magically avoid the mythical training IT departments keep crying about, this move sense the wrong message to the consumer market and any developers who might have supported Windows 8 in that space; if Microsoft made these concessions will they make any more? given the sales numbers of WinRT it seems like a matter of time?

Unfortunately, in this age of iDevices the bar for a consumer product is significantly higher and users seem to want something a bit flashier to use on their off time. Meaning that developers need the platform to shed the legacy and be something new something flashy something other than Windows. Put a bit more simply, Microsoft has to learn how to say “no” to the enterprise. Unfortunately, Microsoft has shown its priorities and maybe it is time for it to just become the IBM knock-off that we all know it will.

Questions? Comments? Dogmatic rage? Find me on Twitter and Google+. This post was brought to you by Code Journal and Fingertip Tech, INC.

 

Programming Pitfalls: WinRT MediaElement URL Scheme

I’ve been doing a good deal of C# WinRT development recently and for the most part it hasn’t been bad. This week, however, I found a pitfall that is not only so simple it’s silly but also managed to waste an hour or so of my time. WinRT has a class called MediaElement that allows you to play different types of media using Window’s built in media engine.

As you might expect, instances of MediaElement take Uri’s for their source media.  So, let’s say you want to play a video from your app’s bundle — perhaps an introductory video or something like that. You might try:

// I am assuming you created a MediaElement called "player" in your XAML
player.Source = new Uri("/Appname/Assets/Media/Video.wmv" UriKind.Absolute);
player.Play();

Sadly, that will crash every-time. The good news is that your logic is fine, but the bad news is that you are missing a silly implementation detail of how Microsoft has decided to refer to in bundle URL’s.  To make that code work, you simply need to make one small change:

// I am assuming you created a MediaElement called "player" in your XAML
player.Source = new Uri("ms-appx:/Appname/Assets/Media/Video.wmv" UriKind.Absolute);
player.Play();

That’s it. Clearly, there is a little bit of magic here… hence the need for the prefix, but it works and is the prescribed way to do this according to MSDN. Hope that helps someone. Questions? Comments? Find me on Twitter and Google+. This post was brought to you by Code Journal and Fingertip Tech, INC.