Archive for C#

Book Review: Programming C# 5.0

catI recently read Programming C# 5.0 by Ian Griffiths as part of O’Reilly’s Blogger Review program. For those who care about formats, I read the epub format and not the print one. Overall the book isn’t bad, but there are some issues that left me less than impressed. My primary issue with the book is that it isn’t clear who it is intended for. Certainly, targeting a generic C# book is going to be hard given the wide variety of areas C# is used in (Azure, XNA / MonoGame, WinRT, etc) and it seemed like there was a need for the book to be a little more specific in terms of domain.

There also are some issues in terms of the skill (or rather experience) level of the book’s target audience. It starts by explaining the history of C# and .Net and in particular how it related to Microsoft’s Java strategy — in that C# was their answer to Java. All of that is very interesting and probably something that a C# developer should know. However, things quickly fall of the rails when the author explains what ints and other basic types are. This trend continues for about four chapters but then the book quickly moves on to more advanced and more interesting topics.

Despite this odd and somewhat jarring inability to figure out who this book is for and what the scope of the text should be there is a good deal of value to get out of the text. For instance, the text teaches not just programming in C# but how to program in idiomatic C# and to a lesser degree how to follow the accepted, though admittedly somewhat informal and subject to personal tastes, conventions established by Microsoft and the .Net community.

Overall, I would recommend this book to someone in school or who is new to programming and suggest that person read it straight through. For more advanced users or users who have a few years of development experience in another language, I’d still suggest picking this book up but skipping to about chapter four or five.

 

Pallet Town: Intro to the Repository Pattern MVC4 pt 2

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.

This week we are carrying on with our introduction to the repository pattern in ASP.Net MVC4. We’ve done well so far if I can be allowed to say so myself, but we left one glaring issue in our application: we forgot to persist our data.

Let’s start by adding a database context to our existing Pokemon class:

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

  public class PokemonContext
  {
     public DbSet PocketMonsters { get; set; }
  }

}

Now let’s actually use out PokeContext in our repository:

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

    public PokeRepository()
    {
       this.pokemon = db.PocketMonsters.ToList();
    }

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

        somePokemon.Id = nextId++;
        pokemon.Add(somePokemon);
        db.PocketMonsters.Add(somePokemon);
        db.SaveChanges();

        return somePokemon;
    }

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

That’s it! If you are running on localhost with Visual Studio 2012, the data will be saved to a .mdf file on your App_Data directory. If you are running on Azure and publishing with a publisher profile, then it should all be set up for you, assuming you have properly set up a Azure site with a corresponding SQL database on the Azure web portal.

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+.

XNA is Dead, Long Live MonoGame

MonogameLogoIf you haven’t been following the indie game development scene, then you might not have heard that Microsoft has confirmed that XNA is dead. This comes as little surprise to me and many in the community, since Microsoft’s recent actions, such as not supporting XNA on WinRT and Windows Phone 8 (arguably their two most important platforms in terms of the consumer market going forward) have made it pretty clear that they haven’t been interested in the technology in some time. Currently, the only places that XNA still lives as far as Microsoft is concerned are Windows Phone 7, an obsolete system, and the Xbox Live Arcade.

Going forward, developers that are already supporting an XNA game have a pretty clear path forward in MonoGame. Migrating to MonoGame will require some work but the upsides are substantial: it is very actively supported by an eager open-source community of developers and it is specifically designed to be cross platform and was designed to be a port of XNA.

If you’re starting a new game development project, it is still a good idea to take a look at MonoGame. MonoGame has not only succeeded in being an adequate port of XNA but has also gone a lot further than XNA as a game development tool and is worth a look in its own right. The idea of a C# easy to use (relative to more traditional game development frameworks) is still a great one and opens the door to game development to a lot of developers who might find a C++ focussed game development tool too intimidating.

To me, this story is less about XNA and game development and more about the danger of basing your project or product on proprietary software. If it weren’t for MonoGame, the XNA developers would be in a poor state right now, since Microsoft was able unilaterally pull support for a technology that they had built their businesses on. If you are being affected by Microsoft terminating support for XNA, then I wish you well and also humbly suggest that you look toward open-source development platforms in the future.

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.