Monthly Archives: August 2008

Announcing open source project Bunian

What could be better, as an Open Source project, than a charity application! where your code is really worth something valuable; a smile on an innocent orphan face, or new clothes for a needy family in Eid.

Announcing new open source project Bunian; the excitement is indescribable! eager to reach with it a stage when it’s really alive, doing something good out there for needy people. I understand it’s going to take so much to drive a successful project, but with the right contributions from the right people like you…I am sure things will just work fine.

Here is the summary I came up with to describe the project in the least amount of words:

Bunian is a charity web application that will make it easy for the generous people, who want to give, to reach for the poor, who need and yet cannot be reached.
Charity organizations who use Bunian, will enable Sponsors to browse through list of Beneficiaries already registered with the organization, depending on different criteria, giving the Sponsors the ability to Request to sponsor certain Beneficiary, who can be Orphan, Family…etc.
For the time being, there will be no handling for charity transactions, the web application is only meant for communication.

The basic target technology is ASP.NET 3.5, NHibernate, and Microsoft SQL database, the initial structure and code is already uploaded on the source control over codeplex (an open source project hosting website), you can download the source code here.

There are lots to do, and lots of ideas that can enrich the project, I surely cannot do it alone, so if you think you have the time and skills, then you are more than welcome to join the project. Even if you don’t want to join, your comments and advices are still highly appreciated, and thanks in advance.

More posts will be coming concerning Bunian, there is still lots to say which I will postpone till its right time.

NHibernate Inverse attribute

I had to read documentation, articles, many blog entries and go into discussions with colleagues… all to get the root of this ambiguous attribute of NHibernate, it is trickey!
This blog entry is another trial to explain the attribute, but with taking one more detailed step into the explanation.

NHibernate is meant to persist objects as well as to manage their relations to each other, lets take a look at the following example of Parent and Child classes:

public class Parent


        public virtual int Id { get; set; }

        public virtual string Name { get; set; }

        public virtual IList<Child> MyChildren { get; set; }


    public class Child


        public virtual int Id { get; set; }

        public virtual string Name { get; set; }

        public virtual Parent MyParent { get; set; }

and we have corresponding tables to these two classes like the following:



(note 1: as we are proceeding that there is no Null constraint on the ParentId foreign key in the Child Table)
Finally, we check the interesting part of the mapping files:

  1. Parent:

    <bag name=MyChildren table=Child cascade=all>

          <key column=ParentId/>

          <one-to-many class=Child/>

  2. Child:

    <many-to-one name=MyParent class=Parent >

          <column name=ParentId/>


(note 2: all what the cascade=”all” attribute in the MyChildren bag does is that when ever you save the Parent, all the Child objects in the MyChildren collection are forced to be saved as well; and we are only talking about the Save operation; not interfering or changing any of the values of the Child properties)

Now if we execute the following code:

Parent par = Session.Get<Parent>(8);

            Child ch = new Child();

            ch.Name = “Emad”;



As you may expect, both objects “par” and “ch” will be saved due to note 2 we mentioned above, but the surprise is that when you check the values in the database, you will see that the ParentId field was set as well although we didn’t set it explecitly in the code!


The reason is that there is a hidden attribute called “Inverse” in the bag part of the Parent map file whose default value is “false”; when this attribute is set to false like the default value, then the Parent says: “Oh, so it is my responsibility to maintain the relationship with my child objects, ok then when ever a child is added to my collection, when I am saved…I will perform an update SQL statement to their foreign key to point at me”

So when the Save is called, the “par” object is saved, and the “ch” is inserted because calling Session.Save(object) when the object is new it will be inserted.  And after all that happens, an explicit update SQL statement will be executed to update all the child objects to set the foreign key ParentId to the par object Id.

This goes all fine in our case, only due to note 1 (scroll up again); we don’t have an Null constraint on the foreign key ParentId, so the Insert statement is excuted without exceptions, but in most cases in the world, DBA do put this constraint, by that we will get a “cannot insert Null value in ParentId” exception!

The solution is to set the Inverse attribute to “true”, which means that the Parent will NOT updated the Child objects foreign key, it will only call Session.Save(ch) due to the cascade attribute, so the result will be like the record 6:


But then how to solve this problem?! we want to set the value of ParentId AND be able to preserve the Null constraint; so we need to set the MyParent property of the Child to the Parent “par” like line 4:

    1 Parent par = Session.Get<Parent>(8);

    2             Child ch = new Child();

    3             ch.Name = “Emad”;

    4             ch.MyParent = par;

    5             par.MyChildren.Add(ch);

    6             Session.Save(par);

and to make it graceful, we can create custom collection for the Children and in the Add method of the collection we set the passed Child objects property MyParent to the parent.

You can download the code sample here.

I hope this is detailed enough to explain what the Inverse attribute exactly is, how to go about the Null exception, and to understand that the attributes “Inverse” and “cascade” are different things.

Mapping Enumeration of type int in NHibernate

I wanted to map an integer enumeration type in NHibernate, I googled “mapping enumeration in NHibernate” and the best explanation was of Jeremy Miller in his post here.

But as it appears (and according to my understanding) that the enumeration type should be mapped to a database column of characters type (varchar, char,…etc).
What if the database column was int? well…do exactly like what jeremy did except simply use “NHibernate.Type.PersistentEnumType” instead of “NHibernate.Type.EnumStringType“.

The sole purpose of this post is that I didn’t find this solution fast enough on google, so I hope it helps others faster.


ذات يوم خلال الأسبوع الماضي، صدف أن استضافني صديق لي في منزله. جلسنا أمام التلفاز، ثم بدأت عملية القفز العشوائي التي اعتدناها – نحن كمجتمع – بين محطات اللاقط.

و في خضم المتعة العشوائية، استوقفت مضيفي محطة معروفة، كان يعلو منها حينها صراخ و عويل. و بنظرة منهكة، توجه إلي صديقي و سألني: “هل تعرف ما هذا؟” لم تكن الصور صورا إخبارية حقيقية، لكن كانت صورا من مسلسل…ثم سأل متنهدا :”هل سمعت عن التغريبة؟”، كانت علامات الاستغراب التي اعتلت وجهي جوابا كافيا له. فاستطرد قائلا: “إنها التغريبة الفلسطينة…تحكي قصة ضياع فلسطين!”.

لوهلة كأني سمعت الكلمة لأول مرة “ضياع فلسطين”!… وجهت أنظاري مرة أخرى للشاشة تتحرك فيها صور التهجير، و المعاناة، و الجوع، و الخوف. ثم و كأن الصور بدأت بالتدفق من عيوني إلى صدري، فضاق، فخفت، ثم كان شعور الغربة و الخسارة، خسارة الأرض، و خسارة الأحبة، و خسارة الشجاعة، و خسارة العزة!
اكتأبت، و عندها بدأت أفهم النعام، بدأت أشغل نفسي بأي شيء، أبحث عن أي شيء يصرف تفكيري عن…عن حقيقة أننا منذ إذن.. و نحن نهرب دون توقف…هربنا من الموت حينها، و الآن من ضمائرنا نحن نهرب.

 خرجت الكلمات على استحياء مطالبا بإقفال التلفاز، و كأني أقول لنفسي…آسف.

How we decide what to have for lunch

Do you go through the same, time-consuming, frustrating discussion with your colleagues when you want to decide what to have for lunch? well…this is exactly what we have been going through where I work.

To solve the problem (partially, because humans can never totally agree! ), I made a small, quick and dirty web application; LunchPoll.

The user (Active Directory user, it works on Windows Authentication) would go to the home page, select the available poll:

Then a list of all available restaurants will show up. The restuarants’ names are in arabic, so don’t be frightned if you didn’t understand the names 🙂 :

The user will assign a weight for each restaurant depending on his taste for today, and no two restaurants can have the same weight.

And after submitting the poll, the user will see the results; all the restaurants will appear, each one with the value of its weigt next to it, along with the voters names who voted for now:

you can download the source code here. bon apetite 😉


p.s. There is no administration interface, so you will have insert restaurants in the Restaurants table, and Insert an entry for each new poll in the Polls table, giving the Status column the value “1”

p.p.s That never stopped the arguments 😛