Far Away Developer

Sebastien Lachance

Archive for February, 2008

WHILSOF : ReadOnlyCollection<T>

Posted by Sebastien Lachance on February 26, 2008

Problem :

I have a collection of entities in my repository. Those entities are not supposed to be visible to the outside world. But, In a test I am supposed to see if all objects are still there and I can’t use the GetAll method because it goes to the database and return everything, not just the collection of entities in my repository.

Solution :

I could expose the collection but how can I be sure that nothing will be changed. I could create a copy of the collection but that would not reflect the “Entity” concept. I know that Java has support for returning a read-only collection. A little bit of search and I found the class ReadOnlyCollection<T>. This is thread-safe and any changes to the original collection will be reflected in it since it is only a wrapper.

public static IList<IRecipe> _identityMap = new List<IRecipe>();

  public IList<IRecipe> IdentityMap
  {
      get { return new ReadOnlyCollection<IRecipe>(_identityMap); }
  }

Could not be easier!

Posted in .NET, Learning, Project | No Comments »

WHILSOF : Unit Of Work

Posted by Sebastien Lachance on February 22, 2008

This is an interesting pattern. I was using it without giving it a name. I encountered this pattern while reading the Jimmy Nilsson’s book : Applying Domain-Driven Design and Patterns - With Examples in C# and .NET. The Unit Of Work is a way to create a logical group of object. The most basic example I can think of is the example of an Order. An order contains OrderLines. The Order and the OrderLines are part of the same logical group. We say that this is an Unit Of Work. I tend to think of it at the way I’ll save the Order. Do I call a service to insert the Order and another to save the OrderLines? Or do I call a single service that will persist it all at the same time (maybe using a transaction)? I will use a single service that will save the Order, the OrderLines and all other related information. Everything that must be persisted at the same time is said to be an Unit Of Work.

In the RecipeHelper project (the personnal project I am working on), I have a single Unit Or Work (at the moment). The Recipe is the root and contains a list of steps to follow and a list of ingredients. When I persist a recipe, I persist everything in the object graph.

Here is some resources from experts that can help you more with applying this pattern :

Posted in Agile, Design, Desing Patterns, Learning | 2 Comments »

WHILSOF : What Have I Learn SO Far

Posted by Sebastien Lachance on February 22, 2008

I have been pretty busy lately. I have so much fun working on my project, that I have neglected my blog a little. As part of my “Building a New Application Series“, I will introduce another series under it, the “What Have I Learn SO Far”. As you may know, I started this project as a challenge (The “What I can do in the next 6 months to become a better developer ) to learn more. This series will consist of various knowledge I acquired during the course of the project. I have already learn a lot and I have multiple items on my list to blog about. Various stuff about application architecture, xUnit, NHibernate, Domain Modeling, etc. I’ll try to deliver great content. 

Posted in Blogging, Programming, Project | No Comments »

Setting up a basic continuous integration server with TeamCity

Posted by Sebastien Lachance on February 6, 2008

What is TeamCity? It’s a software that you would use to do continuous integration. Continuous integration is very important and it should never be an option on a serious project (that’s is my opinion and I strongly believe in the values it provide).

If you have followed the Building A New Application Series, you may be wondering why I would want to do that after setting up a complete integration server with CruiseControl.NET. Well, I was skeptical when I first heard about all the fuss surrounding this JetBrains project. After all, CruiseControl.NET was doing everything I wanted and more.

So the reason I made the switch is because of the time it take to set it up : 20 minutes. Everything was fully functional in under 20 minutes. I absolutely got no error at all. I just got through all the step of the wizard and then : done. Unit testing, code coverage, build, reports, etc… And it’s absolutely free for small team.

The big downside for me was the lack of support out of the box for other unit testing framework (other than NUnit and MSTest). My project is using MbUnit so I will need to do some more research to integrate it in my build process. But it is possible if you want to invest time in doing so.

So let’s get started with this little tutorial :

1. Download TeamCity.

2. Start the installation.

Just follow all the steps. I encountered a slight problem due to something already on port 80, so I used the port 8080.

TeamCity_001

TeamCity_002

TeamCity_003

TeamCity_004

TeamCity_005

TeamCity_006

TeamCity_007

I left everything intact in this screen, but this is where you specify some default folder used bu the build agents. The tempDir and workDir will be used to store your application artifacts and even your source file before the compilation if you don’t give it one in the configuration (later).

TeamCity_008

TeamCity_009

TeamCity_010

That’s it, you have installed TeamCity. Now let’s add our project to it.

3. Open TeamCity !!!

TeamCity_011

TeamCity_012

Nothing too complicated here, just enter the username/password you want for the administrator account. The email will be used to sent important information the build server would want you to know..

TeamCity_013

What I like is that you will be taken by the hannd for the rest of the steps. Click on the Create Project link.

TeamCity_014

Name of the project and a description. It will be useful if several projects are on the same build server.

TeamCity_016

Now click the Create build configuration link. You can have multiple build configuration by project.

TeamCity_019

In the build configuration window, you can change the way the build number are generated and the default behavior when the build fail. You can also specify that you want to start with a clean project.

TeamCity_020

There, you can decide how you want the project to be retrieved from the source control repository and where to put him. You can also decide when to label your build. If you do not want a label on a failed build, you can do so. Next, click on Choose build runner.

TeamCity_021

TeamCity is not only for .NET project. Several build runner are included such as MSBuild, NAnt, sln2003, sln2005, sln2008, Ant, Maven2, etc… Because I am using MbUnit as a testing framework I have decided to stick with MSBuild. But if you are using NUnit or MSTest you may want to use sln2003, sln2005, sln2008 as they can run your unit tests and tell the coverage of your assemblies. You need to fill in the Solution File Path field with the name of your .sln, it’s relative to your project structure (if it’s in the root, enter only the name of your solution file. Fill the rest of the field if you are using NUnit or MSTest.

TeamCity_022

Last step! Go to the Version Control Settings tab and click Create and attach new VCS root. Give it a name and choose the version control system (VCS) you want to use. Fill in the required field.

Save, and you’re done!

This is the most basic setup you can have. Of course, there is a lot of other things to configure to your specific needs. You can even have integration inside Visual Studio or Eclipse. As I said before the only downside I found is the lack of support for MbUnit, but I will work something to bypass that.

I may do another post going more in depth if I have time. Or maybe a screencast, that would be a lot of fun. If you’re interested, just leave a comment and I will consider it.

Posted in Agile, Continuous Integration, Learning, Project | 5 Comments »