Friday, February 22, 2008

The three project solution

Ayende just brought up the issue of project structuring, and his experience was so similar to mine that it cannot be a coincidence.

In my old job I spent a lot of time trying to "do things properly". Splitting up into a lot of logical assemblies, promoting reuse by having special helper assemblies and so on. The result was a huge amount of lugggage that each application had to carry around. When we deployed a simple windows service or console program, it consisted of 9 or 10 assemblies. And don't get me started on versioning all of these, because we were very methodical there too. It sort of worked, but only because we had build a very rigid CI monster controlled by a horde of NAnt scripts that no one - even me who wrote most of them - could figure out after two months away from them.

Today I consider that a learning experience, and I go for the "three project solution". Ayende says two, but I think having a third is worth the hassle. Especially when you create solutions that have different application components in a workflow.

The three projects goes a little something like this:

  • Application.Core : Holds all the logic and data for the solution. I split up classes in a logcal folder/namespace structure. As I favor using MVC pattern a lot, it usually holds Model, Controller, DataAccess and Helper folders.
  • Application.Core.Test: A test library for the core lib, same structure as the Core.
  • Application.<Type>: This is the application itself. The type can be Website, ConsoleApp, Service or whatever it is that I am creating. If there are more apps, there usually is, they get their own projects. All application projects have as shallow code as is can - usually only the View parts of the MVC structure.

I find that it reduces friction both when developing, but especially when deploying. Only two assemblies to deploy. No inter version dependency nightmares and so on. Simple and smooth.

Monday, February 04, 2008

Mass Salvation

Nordic Game Jam is over, and as last year, it was a hoot and a holler! Man, we had fun! I was lucky to get a great team. crew_highFour programmers, four game desginers/artists/sound engineers and a great idea. So we managed to get a full a full game up and running on the XBox using XNA in only one and a half day.

The game is called "Mass Salvation" and in accordance with the theme for the Jam, which was Taboo, is about religions. The "back story" is that it is the day before judgement day, actually 1 minute and 30 before, and humanity is a mess. Everybody is atheists and now reckognize their mistake. You take the role as a religious leader and have to convert as many followers to your cause and send them to the sweet afterlife in style.

It is played using a controller geometry wars style by up to four players. You are playing short rounds (1:30), and the pace is frantic. You can pick up and play the game right away, but as you play more, you will learn how to use the rally point to your advantage as well as different strategies for grabbing followers. So the gameplay has several levels of skill.

Below is a video of the mayhem. I will post a link to a downloadable version of the game soon. Just have to repackage it and make a ccgame version as well. So stay tuned!

Tuesday, January 29, 2008

OMG! It Rocks!

Not that I had any doubts about it. But I just tried Rock Band today, playing the guitar and drums. Man, it is fun. Why, didn't I sing? Dunno. Next time. Playing the drums was just as muc fun as I had imagined. The guitar takes a bit getting used to. It is much more sturdy and heavier than the GH guitars and has a much different feel. But it actualy feels more like playing a real guitar than GH does, much more than I had thought. Is there a flipside?! Yes, the version I have access to here two months prior to launch is a PS3 version - so no achs for me for the time being. Darn it.

Sunday, January 06, 2008

XNA Tweener library

My second open source project is live now at Codeplex - and it has a release and everything! Check it out here.

The project is a Tweener library for XNA making it easy to create animations for your games. I was inspired by the Tween class in ActionScript although it is not a direct port. All the cool easing functions from Robert Penner that you know and love are of course there, ported from the js code. So the release is functional.xnatweener

I am actually quite pleased with myself for doing this, because it has been short and focused work. The first code was created this Thursday - this was the basic Tweener class and the first ported equation as well as a very basic demo. Today (technically yesterday) i squeezed in the rest of the work, adding stuff to the Tweener class and the last equations. Then I spend some time polishing up the demo so it would look cool - getting to know my own library in the progress.

Best of all: I didn't create the library alongside a specific other project. This made it easy to focus on not making all kind of proprietary stuff and KISS. I am pleased with the result, and will use it in my games. But i find that I lack some of the fancy dynamic features from JS lacking in C# which could have made the class so much cooler - but I have some slick ideas for making it rock even more.

The project is made on Codeplex, and having tried both Codeplex and SourceForge now - I will say that Codeplex really whips the llamas a**. Or at least SF's. When I made the d20Sharp project on SF it took me ages to get up and running properly. With Codeplex it literaly took 5 minutes. And the interface on Codeplex is much better and more intuitive than the one at SF, and I won't even begin on the features... I would like the Codeplex interface for my own projects - I know it is somehow based on TFS but I haven't checked out the product as I know it is way out of my league at Gosuman pricewise. If there were a hosted licensed version out there I would probably consider it.

Anyway - download the library and take it for a cruise. Then tell me what you think. Then I will improve it.

kick it on DotNetKicks.com

Thursday, December 13, 2007

More Mocking of HttpContext for MVC

I was actually preparing this for a comment to my last post in answer to chris7's comment. But it got long. You know!

Chris7 asked if it was possible to abstract out the Record/Playback syntax in the example to make the code clearer. And you can of course do that and put the Replay call into the MockContextFactory like this:

protected IHttpContext MockContextFactory(string url)
{
IHttpContext httpContext = mockery.Stub<IHttpContext>();
IHttpRequest httpRequest = mockery.Stub<IHttpRequest>();
SetupResult.For(httpRequest.AppRelativeCurrentExecutionFilePath).Return("~" + url);
SetupResult.For(httpContext.Request).Return(httpRequest);
mockery.Replay(httpRequest);
mockery.Replay(httpContext);
return httpContext;
}


The code in the test method would then look this way:



[Test]
public void WorldDetailsRouteExists1()
{
IHttpContext httpContext = MockContextFactory("/World/Details/9/3");
RouteData routeData = routes.GetRouteData(httpContext);
Assert.IsNotNull(routeData, "There is a route to the URL");
Assert.AreEqual("World", routeData.Values["controller"], "We are using the world controller");
Assert.AreEqual("Details", routeData.Values["action"], "With the details action");
Assert.AreEqual("9", routeData.Values["x"], "And the correct x");
Assert.AreEqual("3", routeData.Values["y"], "And the correct y");
mockery.VerifyAll();
}


I actually chose the Record/Playback model as I find it nicer to look at than the other syntax. But I guess it is a matter of taste. In this example it is probably not obvious why I prefer that syntax, but for more involved mocking scenarios it makes it easier to see what is record and what is playback.



Phil Haacked also commented with a post of his own describing how to the same thing using extension methods instead. I encourage you to look at that as well to see yet another way of doing it.



kick it on DotNetKicks.com

Tuesday, December 11, 2007

Mocking HttpContext for MVC route testing

First I have to say thx a bunch to MS for releasing the CTP of the MCV framework. This really is Da Bomb. I think they are doing a great job here.

So I am setting up my first MVC.NET based site, while digging through all the relevant posts on how to use the framework. So while setting up my routes, doing the proper TDD, I stumble upon the MockHttpContext for testing routes in ScottGu's part 2. This bugger is not explained anywhere nor part of the framework. So you have to whip up your own. Googling it - lazy as I am turned up nothing. DIY time. But being the friendly coder, I will share what I made.

So without further ado - here is how you could create a MockHttpContext for testing your routes using Rhino.Mocks. Note, I have used the same way of setting up the routes as Scott is showing in his post.

[TestFixture]
public class RoutesTest
{
protected RouteCollection routes;
protected MockRepository mockery;

[SetUp]
public void SetUp()
{
routes = new RouteCollection();
Global application = new Global();
application.RegisterRoutes(routes);
mockery = new MockRepository();
}

protected IHttpContext MockContextFactory(string url)
{
IHttpContext httpContext = mockery.Stub<IHttpContext>();
IHttpRequest httpRequest = mockery.Stub<IHttpRequest>();
SetupResult.For(httpRequest.AppRelativeCurrentExecutionFilePath).Return("~" + url);
SetupResult.For(httpContext.Request).Return(httpRequest);
return httpContext;
}

[Test]
public void TestRouteRegistration()
{
Assert.IsNotEmpty(routes, "We have filled the routes list");
}

[Test]
public void SearchRouteExists()
{
IHttpContext httpContext;
using (mockery.Record())
{
httpContext = MockContextFactory("/Search/Beverages/2");
}
using (mockery.Playback())
{
RouteData routeData = routes.GetRouteData(httpContext);
Assert.IsNotNull(routeData, "There is a route to the URL");
Assert.AreEqual("Search", routeData.Values["controller"], "We are using the search controller");
Assert.AreEqual("Results", routeData.Values["action"], "With the details action");
Assert.AreEqual("Beverages", routeData.Values["query"], "And the correct query");
Assert.AreEqual("2", routeData.Values["page"], "And the correct page index");
}
}
}
kick it on DotNetKicks.com

Friday, October 12, 2007

CodeBehind handlers in ASP.NET

I was creating an HTTP handler for an ASP.NET project today for returning a CSV report to a customer. And naturally I wanted this to CodeBehind style as everything else. This turned out to be a small gotcha, since it is not supported out of the bag with VS.NET 2005. So for further reference here is what to do.

1. Create the handler as usual - for instance MyHandler.ashx.

2. Create a new code file for the handler - MyHandler.cs - make sure this goes in the App_Code directory otherwise it will not be compiled properly and you will get a "Could not find class: MyHandler" error.

3. Copy all code from MyHandler.ashx to the new code file, replacing all code there.

4. Add CodeBehind="~/App_Code/MyHandler.cs" to the WebHandler directive at the top of the MyHandler.ashx file.

Done deal. Why the code file has to go into the App_Code directory and cannot stay alongside the ashx-file as other handlers do is an open question. I am betting that there is some odd web.config fix for this.