NHibernate Linq Released

27. July 2009

Just as a reminder, even though you might have seen it before on twitter or nhforge or Ayende’s blog

As of today, we released the first version of NHibernate.Linq. It is currently based on criteria api, and can handle many situations that Criteria API can handle.

Oh one more thing, NHibernate.Linq was the first project that I contribute as a committer (it was in Rhino Tools), it is a strange feeling to see its release.

I would like to thank Oren Eini(Ayende), Chad Lee and Will Shaver for making this project come true.

You can read the official announcement here on NHForge

Implementing EnrichWith(of StructureMap) with Castle

15. July 2009

UPDATE: This facility made its way into Castle Microkernel, with name OnCreateFacility. I also made it possible to specify more than one actions.

In one of Joshua Flanagan's recent post he mentioned about how they handle application configuration and I have to say that I liked their way. I also liked how SM can post-modify an object created, and looked for a way to do it in Castle. As many other stuff, I was able to achieve the same effect with a custom Facility.

If I go further in the details, I had to catch ComponentCreated event of Kernel.

public class EnrichWithFacility:AbstractFacility
{
	public const string ExtendWithPropertyKey = "extendwith";
	protected override void Init()
	{
		Kernel.ComponentCreated += Kernel_ComponentCreated;
	}
	void Kernel_ComponentCreated(ComponentModel model, object instance)
	{
		if(model.ExtendedProperties.Contains(ExtendWithPropertyKey))
		{
			var action = model.ExtendedProperties[ExtendWithPropertyKey] as ExtendComponentDelegate;
			action(this.Kernel, instance);
		}
	}
}

Whenever a component is created, I will catch it and ask if there is any EnrichWith registered for the ComponentModel, and if there is any, invoke the action.

I also added a fluent registration extensions (Castle style!) in order to make it easy to register enrichments.

container.Register(Component.For<IService>().ImplementedBy<MyService>()
			 .EnrichWith((kernel, instance) => ((IService) instance).I++));

 

The code for the facility, fluent registration interface,and the tests can be found on our never-ending blog engine, BlogSharp codebase.

, ,

Long Live, Nikola Tesla!

10. July 2009

This is the 153th birthday of The Man Who Invented 20th Century (and also 21st century). There are a lot to say on his works and life, and how Edison prevented his work, etc, but i am not going to go over and detail them.

Tesla was a great man, and a researcher mostly known for his interest in Magnetism and Electricity. He is also the real inventor of Radio. He’s the inventor of Alternative Current, which we use today. He designed the future in his mind. Rumor is that he worked on transferring energy in the air and actually designed them. Research is still going on this topic, roughly 100 years after his works.

I found a great video on his life and works, recommend you to watch.

 

Tesla doesn’t have the fame he deserve today, but for me, he is the hero of today’s technology, not Edison!

Querying on Child Count With NHibernate

10. July 2009

This is a recent question raised in NHibernate Users Group. The user wanted to realize the following query with Criteria api.

var result = db.Person.Where(x => x.Pets.Count > 0 && x.Alive)
.OrderBy(x => x.Name);

This is not a simple query, but it has a solution

DetachedCriteria crit = DetachedCriteria.For(typeof (Person), "p2")
    .CreateCriteria("p2.Pets","Pets")
    .Add(Restrictions.EqProperty("p.Id", "p2.Id"))
    .SetProjection(Projections.Count("Pets.Id"));

ICriteria c = s.CreateCriteria(typeof (Person), "p")
    .Add(Restrictions.Gt(Projections.SubQuery(crit), 0))
    .Add(Restrictions.Eq("p.Alive",true))
    .AddOrder(Order.Asc("p.Name"));

What we had to do is to create a DetachedCriteria and on that execute CreateCriteria so that we can do querying on our collection.

The other way is simpler, but requires you to use HQL (below query is provided by Fabio Maulo)

session.CreateQuery("from Person p where  size(p.Pets) > 0 and p.Visible = true order by p.Name")

or

session.CreateQuery("from Person p where  p.Pets.size > 0 and p.Visible = true order by p.Name")

 

I hope this helps.

Quote of the Day / Year

7. July 2009

I saw this one on Hudson Akridge’s Gtalk status

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

Brian W. Kernighan

Well put!

What is a build script? Writing MsBuild Scripts - Part I

6. July 2009

I was thinking of writing post series on NAnt but then I decided to write some on MsBuild first.

This post will briefly introduce you the basic concepts of automation of build, why we need build tools/scripts, and what alternatives we have.

Let’s first start with the definition of build script/tool.

What is a build script?

A build script is all about automation. When we compile projects, we also want to perform some other steps such as running tests, compiling documentation, create an examples package, move some files from one location to another, and in the end zip them, or create an installer for the project. Doing them by hand is not really an option, and you may have thought of preparing a batch file/shell script before you learnt the build files.

For me, Makefile was the first place that I heard about the term “build script”. It is mainly used in Linux to compile applications.

When we take a look at a sample makefile


OBJS = main.obj io.obj
CC = bcc
MODEL = s
CFLAGS = –m$(MODEL) 



project.exe : $(OBJS) tlink c0$(MODEL) $(OBJS), $(.TARGET),, c$(MODEL) /Lf:\bc\lib main.obj : main.c $(CC) $(CFLAGS) –c $(.SOURCE) io.obj : io.c $(CC) $(CFLAGS) –c $(.SOURCE) $(OBJS) : incl.h

First we have several properties which we’ll use later, they are like variables.
Then we have some targets which has “:” on the right. On the right of “:”, we have their “dependencies”. A dependency is a unit that has to be run before the dependent, or a file that our file depends on. For example, main.obj depends on main.c and in case main.c is modified, we will run that target again. The lines after the target are the commands which will be run as part of the target.

This dependency idea is key to every build system and main points in writing a build script is to find dependencies of your project. After you define them, the rest is a piece of chocolate cake!

OK, i spoke too much Linux for a .net blog :) Let’s turn to our beautiful .net World.

In .net world, there are 2(AFAIK, if not please comment) build tools written specifically for .NET. One is the famous NAnt, and the other is MsBuild. NAnt is usually used in OpenSource project. MsBuild is being used since VS 2005, and it is part of the framework. Your csproj files are actually MsBuild files.

Most people say that that nant is more flexible and mature, but they also say that the nativeness of MsBuild into visual studio is a plus. Some they say that they delegate the build to MsBuild while using Nant for other stuff.

In the next post, we’ll write(and use .csproj) a basic MsBuild script.

TeamCity Remote Runs / Personal Builds

3. July 2009

This is a killer feature of TeamCity against other CI servers, I believe.

TC allows you to test your changes before committing them to the Source Control Repository, which means that you don’t have to worry about breaking the build, because you won’t :)

I needed this in order to try a fix that I had for an issue in NH, it worked on my machine but  not sure if it works on another one.

Krzysztof Kozmic told me in the same evening that he’s going to try a fix against DP with personal build, and I thought it was a great idea!

image

 

Then it runs your changes on one of the build agents.

image

There we go! Patch didn’t break anything (even though it may not be how it should be)

Keep up the good work, JetBrains!

, ,

Ignoring files/folders when committing to SVN repository

3. July 2009

This feature of SVN is very handy. After you build your projects, there will be dlls in bin folders etc which you don’t want to commit.

Using svn:ignore property of SVN, you can eliminate the possibility of committing those files!

image

As many other things, I learnt this from NH :)