Tag Archive for mvc4

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

A Look at Azure and MVC 4

ASP.Net MVC 4 is amazing. There I said it! I know I’m supposed to be the ‘Mac guy’ or ‘Linux guy’ or possibly even the ‘Ruby / Rails guy’ depending on where you know me from but the truth is that I love all technology and often find myself trying out new platforms or playing with some shiny tech toy. For the last month or so ASP.Net MVC 4 has been that new toy along with Windows Azure.

The Good

Azure provides easy to use web-based GUI’s for basically everything you’d want to do to configure, administer, and monitor your app. If you’re not a fan of GUI’s, there are also commandline tools for Windows and Mac.

For you Heroku lovers out there, the MVC / Azure development and deployment experience is very similar to the Rails / Heroku experience. In fact, Microsoft has gone so far as to build in Git deployment to Azure and has even provided an easy to use web-based GUI tool for setting it up. Overall, I was really impressed with what I saw.

Visual Studio still rocks. I’ve been working with Visual Studio 2012 and have been loving the code generation and debugging tools it includes. It simply is one of the most advanced IDE’s on the market today.

Microsoft has come along way since the days of IE 6. .Net’s Razor View Engine provides full support for HTML5. Not only was I happy to see this, but I was also a little surprised to see how far they’ve come in the area of web standards.

The Bad

MVC 4 has one achilles heel: price. Azure itself, like Heroku is expensive, but with Heroku you can, though with a bit of effort, migrate to a generic Linux server. The issue with Azure is that you’d have to pay for a Windows server license and other licensing fees if and when you want to migrate off of Azure. However, the costs can really start to pile on when you factor in Microsoft SQL Server.

The Ugly

There’s not much ugly in the Microsoft web development stack these days.

The Bottom Line

Overall I am very impressed with what I saw and will be keeping a close eye on ASP.Net and Azure. If you’ve been a Rails or Django developer and have never taken a look at the Microsoft stack, Azure and MVC 4 are definitely the place to start.