Dec 30

Hidden vs Overridden methods in C#

Posted By Ahmed El-Kilani On 30 Dec 2007 No Comments »

The difference between hiding and overriding methods is that override keyword will completely override the method over its parent's, but new keyword will only hide the method from its class reference.
The following code snipset illustrates some trials for hiding and overring methods :

class Parent
{
    
public void HiddenMethod()
    {
        Console.WriteLine(
"Parent Hidden Method");
    
}
    
public virtual void VirtualMethod()
    {
        Console.WriteLine(
"Parent Virtual Method");
    
}
    
public virtual void VirtualHiddenMethod()
    {
        Console.WriteLine(
"Parent Virtual Hidden Method");
    
}

}
class Child : Parent
{
    
public new void HiddenMethod()
    {
        Console.WriteLine(
"Child Hidden Method");
    
}

    
//Compilation error, since HiddenMethod in the base class is not virtual
    //public override void HiddenMethod()
    //{
    //    Console.WriteLine("Child Hidden Method");
    //}


    
public override void VirtualMethod()
    {
        Console.WriteLine(
"Child Virtual Method");
    
}
    
public new void VirtualHiddenMethod()
    {
        Console.WriteLine(
"Child Virtual Hidden Method");
    
}

}

Non-virtual methods can only be hidden while virtual methods can be hidden or overridden; but the difference between virtual and new comes into the picture when playing with object references:

static void Main(string[] args)
{
    Child c 
= new Child();
    
c.HiddenMethod();
    
c.VirtualMethod();
    
c.VirtualHiddenMethod();

    
Console.WriteLine("----");

    
Parent pc = new Child();
    
pc.HiddenMethod();
    
pc.VirtualMethod();
    
pc.VirtualHiddenMethod();
}

Output :

Child Hidden Method
Child Virtual Method
Child Virtual Hidden Method 
----
Parent Hidden Method
Child Virtual Method
Parent Virtual Hidden Method 

pc is a Child object with a Parent reference. pc calls Parent hidden methods and Child overriden methods.

Dec 27

Rounded-edge HTML Layout without images

Posted By Ahmed El-Kilani On 27 Dec 2007 No Comments »

Smooth-edged tables, Div elements are most famous simple and stylish layout. To make your square table smooth you gotta have four small images
for the corners, slicing images then fitting them into tables plus every image for each color if we are about to make multiple themes are sort of boring. Here comes the magic of CSS where we can create smooth layouts without images.
Here is the result:

Here our contents go 


How are the previous fantastic sets of DIV elements shaped?
Simply if we understand how computers draw a curved shape we can understand this. It consists of four div elements for the curved header and upside down another four for the footer, playing with their widthes (from smallest to largest) for the header and on the contrary for the bottom.
Let's see the DIV elements:

<div id=round>
<div id=top>
<div class=s1></div>
<div class=s2></div>
<div class=s3></div>
<div class=s4></div>
</div>
<span>Here our contents go</span> 
<div id=bottom>
<div class=s4></div>
<div class=s3></div>
<div class=s2></div>
<div class=s1></div>
</div>
</div>



Inside the top DIV four div elements classed from s1 to s4 and inside the bottom div the same four upside down.

Now let's see the css features the shape:

   #round{
    width
:250px;    
    background-color
:gray;
    color
:white;      
   
}
   
#round span{
    padding
:10px;    
   
}
   
#top,#bottom{
    background-color
:white;    
   
}
   
.s1,.s2,.s3,.s4{
    height
:1px;    
    background-color
:gray; 
   
}
   
.s1{margin:0 5px;}
   
.s2{margin:0 3px;}
   
.s3{margin:0 2px;}
   
.s4{margin:0 1px;height:2px;}



By playing with different margins for the classed divs we get the range of widthes for those elements which shape the curve.

Pretty easy, is'nt it?

Dec 26

Qyering Many-To-Many data with HQL

Posted By Ahmed El-Kilani On 26 Dec 2007 1 Comments »

Yesterday while I was coding in my company media gallery project I faced a case in which multiple joins between  many-to-many tables have to be constructed. With a simple HQL query I could easily have it done.
Again, NHibernate rocks.

Channels have many media TV shows to be shown which media may be shown in a channel or more.

With this one line we could get all media under a certain channel.

Select m From Media m,Channel c WHERE m in elements(c.ChannelMedia) AND c.Id=:Id

Channel Entity has ChannelMedia collection which has a list of media and Media Entity has ChannelMedia collection which has a list of channels. Because they are related together many to many, so I only have to check that Media is an element in the ChannelMedia collection of the given Channel.

Dec 24

Fixed NHibernate Codesmith template

Posted By Ahmed El-Kilani On 24 Dec 2007 9 Comments »

Why NHibernate and ORM:
Since I've started web programming I used to use shared hosting to host my web applications, which noticeably slow down the application performance, that always made me care about performace issues, like:
Always Using stored procedures, custom database paging, only select the needed columns when binding results to tabular controls (-> repeaters) and etc.. in fact that was pretty cool but I always face a problem of time management.
When my company started to go into more complex application I found out that with enterprise applications time is more important and code design with organizing are more handy and we always need a reliable memory resources. So now my mession is to think more about time and less about performace, so the best was using ORM.
NHibernate is the best, because handling data access are more time consuming than handling business rules, so NHibernate, here, beats CSLA .Net, I am not going deeply into NHibernate now but it is very cool for me;)

What is Codesmith?
Always Nhibernate or ORM terms come with generation tool.The real easy task that nhibernate provides the real boring creating its classes and the mapping files, so if you do not wish to use generators, you'd better not use ORM.
Codesmith is the most famous generation tool for most famous design models including Nhibernate and CSLA.
Codesmith generates classes,xml files, stored procedures and really everything you wish, using its provided templates or your own ones.
Building templates is the most complex thing in codesmith, so it is not the time for discussing how to build your template, soon i will be posting an article discussig how to create or customize your own template easily...
A template for NHibernate is shipped with codesmith but unfortunetly it has some bugs and really not reliable. So i decided to fix the problem and publish my point of interest with it.

What should we generate?
We should generate a class and an xml mapping file (maps an object-oriented domain model to a traditional relational database) for every entity.

We have three templates, one for generating classes, another for mapping files and the third for looping the database and executes both templates to generate mappings and classes for the database.

HBM generation template:
A mapping file would be like this:

< ?xml  version ="1.0"  encoding ="utf-8"  ? >
< hibernate-mapping  xmlns ="urn:nhibernate-mapping-2.2">
    
< class  name ="MyNamespace.Data.Agent, MyApp.MyAssembly"  table ="Agents"  lazy ="false">
        
< id  name ="AgentID"  type ="Int32"  unsaved-value ="null">
            
< column  name ="AgentID"  length ="4"  sql-type ="int"  not-null ="true"  unique ="true"  index ="PK_Agents"/>
            <
generator  class ="native"   />
        </
id >
        
< property  name ="AgentName"  type ="String">
            
< column  name ="AgentName"  length ="64"  sql-type ="nvarchar"  not-null ="true"/>
        </
property >
        
< property  name ="AgentLogo"  type ="String">
            
< column  name ="AgentLogo"  length ="256"  sql-type ="nvarchar"  not-null ="false"/>
        </
property >
        
< bag  name ="Companies"  inverse ="true"  lazy ="true"  cascade ="all-delete-orphan">
            
< key  column ="AgentID"/>
            <
one-to-many  class ="MyNamespace.Data.Company, MyApp.MyAssembly"   />
        </
bag >
    
</ class >
</ hibernate-mapping >


Id Maps the primary key, property maps other columns and bag includes mappings for relations (one-to-one; one-to-many; many-to-one; many-to-many).

Fixing:
1-For NHibernate 1.2 change the root mapping element to:

< hibernate-mapping  xmlns ="urn:nhibernate-mapping-2.2">

2-Attribute lazy="false" should be applied to avoid lazy initialization of the type, although NHibernate can use lazy initialization using dynamic proxies, for more info read about castle project...

3-In Nhibernate.inc file the method CollectionName() gets the name of the collection by making table names plural.
It is a good work using regular expression to make the noun plural but it is not suitable here becoz according to standerizations db table names are plural, so it is ugly making a collection named Orderses rather than Orders.

4- There is really a bug in many to many relation generation, I am not sure if this template was really well tested but what makes me wonder that it is shipped with codesmith as it is :(.

< %  if (IsManyToManyTable(primaryKey.ForeignKeyTable)) {    % >
        
< bag < %=  CollectionManyToManyNameAtt(primaryKey)% > < %=  CollectionTableAtt(primaryKey)% >  inverse="false" lazy="true" cascade="none">
            
< key >
                
< %  foreach(ColumnSchema column in primaryKey.ForeignKeyMemberColumns) { % >
                
< column < %=  ColumnNameAtt(column) % > < %=  ColumnLengthAtt(column) % > < %=  ColumnSqlTypeAtt(column) % > < %=  ColumnNotNullAtt(column) % > < %=  ColumnUniqueAtt(column) % > < %=  ColumnIndexAtt(SourceTable, column) % >  />
                
< %  } % >
            
</ key >
            
< %  foreach(TableKeySchema tableKey in primaryKey.ForeignKeyTable.ForeignKeys) { % >
            
< %if  (tableKey.PrimaryKeyTable ! =  SourceTable) { % >
            
< many-to-many < %=CollectionManyToManyClassAtt(tableKey)  % >     
                
< %  foreach(ColumnSchema column in tableKey.ForeignKeyMemberColumns) { % >
            column="
< %=ColumnName(column)  % > "
                
< %  } % >                 
            />
            
< %  } % >
            
< %  } % >
        
</ bag >

The fixed condition if (tableKey.PrimaryKeyTable != SourceTable) makes sure that only the collection for the table that is related as many to many is included.

5- IsManyToManyTable() method was fixed.So that detection of many to many tables are much more accurate.
We cannot really detect many to many table 100% accuretly but we depend on the usual database desgin that consists of composite primary key with 2 member columns and the many-to-many table should reference the current table, I've added a condition to make sure that both primary key columns are foreign keys.

Class generation template:
1- In the class template the primary key memeber is named Id and if the property ForceId is set to true it has the name of the pk column, the template did not handle composite primary keys. If we have a non-many-to-many table with composite primary key e.g (CountryId, CityId) both mapped columns would be the same "CountryId" because it use the only the first member column of the key, so the fix should be like:

< %  for(int x =0;x<SourceTable.PrimaryKey.MemberColumns.Count;x++)  {% >
        protected 
< %=  SourceTable.PrimaryKey.MemberColumns[x].SystemType % >   < %=  IdMemberName(SourceTable,x) % > ;
        
< %  } % >

And IdMemberName() method would  be:

public string  IdMemberName(TableSchema table, int  x)
{
    
if  (ForceId)
        
return  "_id" ;
    else
        return 
MemberName(table.PrimaryKey.MemberColumns[x]) ;
}

2- In the CompareTo() method. An error will be generated if a table has a binary column since binary columns represented as byte[] array in C#, so we cannot call CompareTo() to the byte[] array , also it is not suitable to sort by binary columns so the following line of code shoud be added (see the line that is bold):

< %if(  column.DataType ! =  DbType.Binary){% >
                case "
< %=  PropertyName(column) % > ":
                
< %  if (column.AllowDBNull ) { % >
                    relativeValue = (this.
< %=  PropertyName(column) %>  != null) ? this. < %=  PropertyName(column) % > .CompareTo((( < %=  ClassName(SourceTable) % > )obj). < %=  PropertyName(column) % > ) : -1;
                
< %  } else { % >
                    relativeValue = this.
< %=  PropertyName(column) % > .CompareTo((( < %=  ClassName(SourceTable) % > )obj). < %=  PropertyName(column) % > );
                
< %  } % >
                    break;

So we do not compare binary columns.

Conclusion:
First thanks for codesmith and all people implemented the template despite the described bugs. I hope I have helped people newer to codesmith and have a great enthusiasm writing their NHibernate projects easily. I have not really tested this template well , but it is working nice enough with Northwind. So if you face any bug I have missed please , tell me I may fix it later.

You can download the template with a working project for NHibernate from attachments.

 

FixedNHibernate.rar (471.23 kb)