Thursday, December 30, 2010

My standard .hgignore

One of the things that I do as soon as I am starting on a new project is create either a Git or Mercurial repository for the project.  At the moment I tend to be using Mercurial more that Git, because bitbucket offerss free private repositories.

One of the things you want to do when setting up the repository is add an ignore file so that you don’t add build artefacts and other user specific files to the repository.  You don’t want to add these files as everyone that works on the project will be constantly changing these files which will pollute your history with lots of unnecessary changes that don’t really effect the project at all. 

I thought I might share my current default .hgignore file with you all.  I am curious what other people have in theirs.

# use glob syntax.
syntax: glob

*.suo
*\bin\*
*\Bin\*
*\obj\*
*\_ReSharper*
*.ReSharper.user
glob:Source/Shell/publish/
*.csproj.user
*\TestResult.xml
*.csproj.VisualState.xml
*\App_Data\*

Monday, December 27, 2010

Deploying ASP.NET MVC 3 RC2

I was recently working on a ASP.NET MVC3 proof of concept application.  My client wanted to be able to preview my work with daily builds.  It was suggested that the simplest way for me to go about this would be to deploy it to Azure.  This turned out to work fairly well, but I did have a few dramas getting things to work.

The main problem that I had was making sure that I deployed all of the MVC assemblies along with my app.  This is required as it isn’t part of the .net framework.  I found a few sites indicating which assemblies were needed, but I found I had to add a few more to list before things worked.  This was a frustrating process, because I had to wait 15mins every time a tried something that didn’t work before I knew that it wasn’t going to work. 

To hopefully save others from having the same problem I have included a list of the assemblies I had to deploy along with my app to get it working.  To include a referenced assembly in the deployment make sure that Copy Local option is set to True in the properties of assembly reference.

Without further ado the assemblies you should include are:

  • Microsoft.Web.Infrastructure
  • NuGet.Core
  • System.Web.Helpers
  • System.Web.Mvc
  • System.Web.Razor
  • System.Web.WebPages
  • System.Web.WebPages.Administration
  • System.Web.WebPages.Deployment
  • System.Web.WebPages.Razor
  • WebMatrix.Data

I was quite surprised to see NuGet.Core and WebMatrix.Data in there as I can’t quite see why the MVC framework should have dependencies on these, but my application did fail to start trying to load these assemblies if I didn’t include them, so it would appear that it was necessary.

Tuesday, December 14, 2010

Windows Phone 7 Theme Resources

Recently I have been working on a windows phone 7 application.  In a few of the project template XAML files I noticed references to some StaticResource styles and brushes.  I couldn’t find a nice way in Visual Studio or Expression Blend to see a list of these resources, so I set out to find one on the web.  After a brief search I found the Theme Resources for Windows Phone page on MSDN.  The page lists all of the available resources included in the wp7 theme, but doesn’t show you want the look like.  I decided to make an app which would show the the theme resources to make choosing the correct resource easier.

Please welcome Theme Resource Preview for windows phone 7!

imageA view of the font name resources

The application contains pretty much all of the resources except for the colour resources.  The reason I left the colours out of it is that they seemed to be the same as the brush so figuring out what colour to use shouldn’t be very difficult.

imageimageimageimage
Some more shots of the application in action

I have posted the source code for the application to my bit bucket account so for anyone who is interested you can grab a copy of the source here: https://bitbucket.org/caleb_vear/wp7themeresourcepreview

Or for your convenience you can use one of the following hg commands to clone (one is https and the other ssh):

hg clone https://caleb_vear@bitbucket.org/caleb_vear/wp7themeresourcepreview
hg clone ssh://hg@bitbucket.org/caleb_vear/wp7themeresourcepreview
The only requirement for the application is that you will need a copy of Visual Studio 2010 and the Windows Phone development tools.  The express version of Visual Studio should to just fine, although I used ultimate.

Wednesday, November 24, 2010

SkipObject Bug Solution

Yesterday I blogged about a bug I was having while implementing a serializer for my append only file storage.  I blogged about it here.

The problem with the code is found by gaining a better understanding of how the += operator works:

x += y;

Is equivalent to writing

x = x + y;

The problem in this case can is explained in the following snippet:

The fixed version of the function can be seen here:

Tuesday, November 23, 2010

Where is the bug?

I was recently working on an append only file data store.  I am building a list of the data at load time.  The code that builds the index doesn’t know how to read anything, but the header information for an object.  I have another class which implements ISerializer to handle reading and writing the objects that I want to store.  The ISerializer interface has a method called SkipObject which is supposed to advance the the stream past the object, which will have it at the start of the next object’s header.  The serializer can know how long the object is because the first 4 bytes of the object make up an integer that is the length of the object in bytes.

Here is what I initially had for the SkipObject method:

Can you spot the bug? I'll post the answer tomorrow.

Saturday, October 30, 2010

Customising the SharePoint search box

On a recent project I worked on some custom branding for a SharePoint site.  One of the things I wanted to do to help layout the page they way the wanted was to change the mark-up that was used by the search text box.  I found an excellent MSDN article detailing how to do this and as it worked splendidly I decided to share it with you.  Head on over to the article here: How to: Customize a Delegate Control

Monday, October 11, 2010

ACT Libraries Rock!

I needed to meet up with a fellow Readifarian today so that we could work together on a joint PD (professional development) project.  We needed to decide on a place to work and I suggested the Woden library.  The last time I went there they had a couple of tables upstairs where people could study, the only facilities were the tables and chairs, but I figured that was better than a table at a coffee shop. 

When we arrived today we were pleasantly surprised to discover a fantastic setup including free WiFi internet and power outlets at some of the tables.  We were able to whip out our laptops and get straight into business in a clean, quite environment with absolutely no hassles.

If you are in the ACT and need a nice peaceful place to work I highly recommend checking out the local library.

Sunday, October 3, 2010

Event Sourcing: Automatic Registration of Domain Event Handlers

Recently I have been investigating the use of the Command and Query Responsibility Segregation (CQRS) pattern combined with event sourcing for a personal project. I need individual client applications to be able to run completely disconnected from my server processes, but when they come back online they need to synchronise both their changes and changes made by others while they were disconnected.

Currently the software tries to achieve this by capturing each change to the data as events detailing what changed. The problem with this approach is that we lose the “why” of the change. This becomes a problem when we have conflicting or duplicate changes which can’t determine that they are duplicates. For example someone might end up charging a client twice for something. The domain classes would normally not allow that to happen, but the data sync doesn’t know about domain rules so puts the invalid data in the database anyway.

While investigating CQRS + Event Sourcing I have found Mark Nijhof’s articles and sample code to be particularly helpful in demonstrating an effective implementation of those two patterns. I highly recommend checking them out.

I have been developing a proof of concept to prove that I will be able to solve my main problems by applying these patterns. For the prototype I have been borrowing heavily from Mark’s sample code, but making modifications as I need to. One change that I have made to Mark’s code is to add a convention for registering domain event handler methods. In the sample solution Mark is registering each method manually. In his article about event handlers he does mention the possibility of setting up some sort of convention for registering them automatically, which is what I have done and I am going to share my implementation in this post.

The only way to find methods that meet the conventions criteria is to use reflection. Calling methods by using reflection is slower than calling them from statically compiled code. I wanted my solution to be fast, but I was willing to sacrifice some speed for more painless setup. The first thing whenever performance is a question is to measure your current performance so you have something to compare against.

My first step was to setup the following test program:

I haven’t shown the Account class’ implementation here, but it isn’t really important. With manually registered event handlers as used in the Fohjin.DDD project it produces the following output:

For the sake of completeness the apply event method looks like this:

My first attempt was to simply use reflection each time an event is applied to find the method that should handle the event. My implementation looked like this:

And produced the following output:

We can see from this that using reflection takes about twice as much time as manually mapping the call. That sounds bad, but the reality is that this solution is still performing well enough that it would only be in rare situations anyone would care, but seeing as how I am a little OCD when it comes to optimising things I decided to see if I could do better.

My next approach was to do my reflection search once and then cache the results to be shared between all instances of the class. I use a static class called EventHandlerMappings (I don’t quite like that name so if you can think of a better one let me know) to manage the mapping and get my reference to the handler dictionary in the constructor and then uses it in the Apply method like so:

We now arrive at the following performance results.

Now we are taking roughly 1.5 times the baseline instead of twice as long, but I still think we can do better. Instead of just caching the method info we can create a delegate from it and use that instead. The downside to this is that the first time we will pay a higher price for the conversion. This isn’t quite as straight forward as it sounds, because delegates parameters are strongly typed. We want the parameters of our delegate to be for the base types of IDomainEvent and AbstractAggregateRoot, but our method will take a specific type. To solve this problem we need to wrap our delegate call in another delegate that will cast the parameters to the correct type.

Here is my implementation for the EventHandlerMappings:

And here is my implementation of the Apply method now:

Which give me the following output:

This result surprised me at first, because it is faster than the original manual implementation. After looking over things I am fairly certain that the reason it is faster is that our original code creates the handler mapping dictionary for each instance and I am guessing that is where the extra time comes from.

It is worth mentioning that the caching code puts additional overhead on the first call for a given type. So if you are only running one or two iterations worth of code in a typical application life time then you would be better off with the straight reflection implementation.

So there you have it, convention based automatic wireup of event handlers in an Event Sourced entity.

Friday, September 3, 2010

How to set a binding on a DependencyObject from code.

Sometimes it is useful to be able to set bindings on DependencyObjects in code.  If the object you are working in derives from FrameworkElement you can just use the object’s SetBinding method, but what if the object is derived from something else such as a Freezable?

The answer is that you use the static BindingOperations classes' SetBinding method, which unsurprisingly is what the implementation of SetBinding on FrameworkElement calls to do its work anyway.

Thursday, September 2, 2010

Hello Blogosphere

My name is Caleb Vear and I am a software developer currently living in Canberra, Australia.  I have just accepted a new role as a developer with Readify, a leading Australian-based consulting company. As part of my new role I will be doing a fair bit of travel and I hope to work on improving my skills by working on small projects that interest me during this time (along with any other free time).  My hope is that I will be able to learn new technologies at the same time as making something I find useful.  The plan is to share my experiences with you on this blog.

Along with posts relating to software development I may also post about other things which I think are interesting from time to time.  To make things easier my posts will be tagged so if you are only interested in a particular topic you can just subscribe to that.  The following is a list of tags I am currently planning to use, others may be added later and will hopefully be self explanatory.

Tech – the post relates somehow to technology.
Dev – the post relates to software development.
Politics – the post relates to politics :S.
Life – the post is something about my life in general.

This is my first post for my new blog.  Previously I had a blog over on live spaces, but I hadn’t updated it in a while and I decided to start a new one with a new look.  I will be reposting some of the better posts from my old blog to this one so that nobody misses out.

That will be all for now – Caleb out.