Introduction to NHibernate Session at Jordev Was Good

Filed Under (Development, Misc, NHibernate, Personal) by Emad Alashi on 05-12-2008

Tagged Under : , , , ,

The feedback was very good, and I was glad that everybody liked it. Jordev is really moving ahead, and I am very excited being part of it :)

Below is the slide show (it’s an enhanced version from my previous one):

[slideshare id=821222&doc=introductiontonhibernate-1228487480885456-9&w=425]

Code is the same of the previous one which you can download from here

Mapping Enumeration of type int in NHibernate

Filed Under (Development, NHibernate) by Emad Alashi on 19-08-2008

Tagged Under : , , , ,

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

Filed Under (Development, Misc, Personal) by Emad Alashi on 02-08-2008

Tagged Under : ,

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 :P

Are code comments important?

Filed Under (Development) by Emad Alashi on 19-07-2008

Tagged Under : , ,

From the very first day I have heard about Agile development and I have been hearing things like “code comments are not such a good thing

The main idea behind that is that the code should be self explanatory; giving functions good names and splitting concerns into different functions.

But what about the “why”; why this certain code is written this way? why not do it that way? and this is what I have gone through today!

I was working on this bug I had which resulted with a really nasty Timeout Runtime error!
I instantly remembered that there was some place in the code that had the same functionality, so I jumped to it and found it is doing the same thing….almost!

Why my code isn’t working?!…my code saves the domain object…and that code saves the domain object too! Ok, he is using the repository directly…and i am using the service…so what?!
Being that guy my team leader Muhammed Tobji, who happens to be a really smart guy, I was sure I was on the right track!

After spending sometime struggling with that bug, I noticed a lot of comments above the that line of code of Tobjis, it said:
“Don’t use the service…use the repository…or you will have a Timeout exception!!”
GGRRRRR!!!

It was there! I don’t know how much time I could have wasted trying to find the solution, when the solution was already there! The code was almost cleanly refactored, the name of the functions were logical and self explantory, and yet..that was not enough! there was we still a need for comments because it was exceptional situaion (and software has lots of exceptional situations ;) )

my conclusion is “use comments”! Be wise though, don’t comment the obvious, but in such situations…please…do post your code comment :)

(thanks Tobji ;) )

My “Introduction to NHibernate” presentation and slides

Filed Under (Development, NHibernate) by Emad Alashi on 02-07-2008

Tagged Under : , ,

I have delivered the presentation I talked about in my previous post here.

Actually it was pretty simple and straightforward, the slides them selves don’t have code content; all the code was shown in VS directly (I always found it better to see the code in its really environment to better understand).
The attendees were handful, but if felt really great when they expressed how excited they were about the whole thing.

You can find the Power Point slides and the sample code in the following zipped file:

http://www.freedrive.com/file/395364,emadnhibernatepresentation.zip

I intend also to share with you the process I went through in order to conclude to the presentation in its final state.

I hope you benefit from it :)

Update: I did this presentation again with enhanced slides, you can find those slides on this post

Columns’ case-sensitivity in NHibernate

Filed Under (Development, NHibernate) by Emad Alashi on 28-06-2008

Tagged Under : ,

The other day I wanted to create an HQL query to retrieve data from one object (WorkOrderFault) that has many-to-many relation with another. so I created the following:

ISession session = NHibernateOrmSessionFactory.CurrentNHibernateSession;

IQuery query = session.CreateQuery(

   “select wof from WorkOrderFault wof join wof.WorkOrderTechnicians as tech where tech.Id = 43334″);

IList<WorkOrderFault> objects = query.List<WorkOrderFault>();

The query ran successfully, and I got my results.

Then I wanted to use paging and get certain amount of results starting from certain record, so I added these two lines directly after I instantiated the IQuery object :

query.SetMaxResults(10);

query.SetFirstResult(0);

Simple and nice, but instead I got the following error:

System.Data.SqlClient.SqlException : The column ‘FaultId10_’ was specified multiple times for ‘query’.

When I checked my mapping file of WorkOrderFault, it had the following lines (I am including the lines we are interested in only):  

<property name=FaultId type=System.Int32 column=FaultID not-null=false access=field.camelcase-underscore/> 

<many-to-one name=Fault class=GRP.Maintenance.Domain.Settings.Faults column=FaultId

                fetch=select insert=false update=false not-found=exception

                access=field.camelcase-underscore/>

Ok, I know it’s wrong to map the same column for two different properties (don’t ask about the reason), but this is the current situation; one property to hold the Id (as an integer), and another property to hold everything. There might be more justifying situations where you want to map two properties to one column, so let’s assume it’s ok.

NHibernate is smart enough, when using queries, to query the database field only once; in situations like this NHibernate figures out that there are two properties mapped to one column so it shouldn’t retrieve it twice (i.e select columnx as x1, columnx as x2…..).
But not in this case!! It just didnt’ work!

I had no explanation for this, except when I looked closely to the map file, I noticed that the field that was causing the problem “FaultID” was written once with capital d (D), and the other with small d (d)!

So as it appears, NHibernate has schizophrenia when it comes to database columns case sensitivity; because SQL itself is case insensitive, but NHibernate code distinguishes between the uppercase and lowercase.

Keep an eye on your map files, try to make them EXACTLY the case like the database is, and unify that through all your map files.

UPDATE:
the effect SetMaxResults() produced is it wrapped the original SQL sentace with “WITH query AS (…“, and only then the SQL refused the duplicate columns in the result, hence the SQLException took place.

original SQL: “select workorderf0_.RecID as RecID10_, workorderf0_.WorkOrderID….

SQL after SetMaxResults: “WITH query AS (SELECT TOP 10 ROW_NUMBER() OVER (ORDER BY CURRENT_TIMESTAMP) as __hibernate_row_nr__,  workorderf0_.RecID as RecID10_…

Suspecting already used code

Filed Under (Development) by Emad Alashi on 22-06-2008

Tagged Under : , , ,

In one of the modules I am working on, my unit tests used to take tremendous amount of time (4 minutes per test  clip_image001). I inherited the NHibernate config file from previous NHibernate test module that was used somewhere before!

At the beginning I thought that this is something common when you use NHibernate with rich applications, so I didn’t pay much attention, but then it became a real headache.

One of the best colleagues notified me about an important note, which is that I was loading other domain objects from a shared  module (which I didn’t really need).
I checked the NHibernate config file, and removed the mapping line that includes those shared domain objects, and the time shrank to 15 seconds!clip_image002

The moral of the story is that it’s ok to suspect code we inherit from others, other modules or projects, we might not gain anything by doing that, but I am almost sure that we won’t lose!

What is next

Filed Under (Development) by Emad Alashi on 21-06-2008

Tagged Under : , , , ,

Ok, now I am facing a hard decision; what to do next in my technical life?

In order to reach for better decision, I drew a mind map using this wonderful online tool Mindomo, it’s like  the following:

most important

Those nodes are the things I think I want to study/do most.
I marked the most important ones with the red, yellow and then blue flags. Then marked the ones I need to do most urgently with 1 being the most urgent, 2 less urgent and so on.

We use NHibernate at work, it’s interesting technology and very much talked about recently, knowing it in details helps to be more productive and give my experience real credit for it.
So I thought about doing some session about it with anyone interested about it here at work (we need to refresh out technical enthusiasm),  my plan is to give a brief introduction on it with some samples, then have a longer discussion about it.
I gave it the highest mark of urgency because we usually are pressured at work and we don’t have the luxury to “sharpen our saws” by having such sessions (I know…don’t look at me like that!). so it takes the first place.

Second in  place, studying for the ASP.NET exam of Microsoft (which is something really cared about here), and I better take the exam before it’s too late.

Thy cycle goes on clock-wise, till it reaches this charity web application, then I thought why not use this chance of creating web application to learn the “Javascript in depth”, “Ajax”, and  “jQuery”?!
and this is what I will do by god willing.

If I want to do that I better commit to what i have just posted, so wish me luck…and if you think you can enjoy open source project, contact me and we might do the charity web application together :)

Builds while deployment

Filed Under (Development) by Emad Alashi on 15-06-2008

Tagged Under :

In my company, we are working on this big GRP project, lots of pages, projects and workflows, soon we are delivering some modules to the customer.
So today, while trying to test a workflow that we have been working for days, something weird happened; the users of the workflow didn’t exist anymore!!

After searching for a while, it appeared that we are delivering some modules, and real data needed to be deployed on a database especially for deployment, basically the Users table was the primary table! so as simple as it gets…they created the new database and changed the users we used for the real users (along with the hierarchical structure of the employees).
Ok…that’s fine for now, but the problem is that they wanted us to work on the new data; the action was that they changed the users on the development database on our development environment!

That took place with late alert, not only that, but I believe that the change should always start from the development database (not the opposite),  notifying all developers of the plan and including them in the change, and only then…the change is reflected to the deployment database.

The team I work with was halted for more than half a day, trying to reorganize the users with the proper structure to start the workflow again!

How do you handle deployment issues when it comes to real data deployment?