Apr 29

Powerful Codesmith data layers templates

Posted By Ahmed El-Kilani On 29 Apr 2008 No Comments »

Codesmith tools is one of the powerful template-based code generation tool. For more info go to www.codesmithtools.com

Codesmith has various predefined templates for genrating domain model, N-tires, CSLA and NHibernate classes or mapping files.
In fact none of predefined templates are suitable for easy and quick use. NetTiers is the best but it needs a lot of customization and also generates a lot of code which is not necessary in most cases.
Today I come with a Generator template to generate domain model classes and data access layer and also CRUD stored procedures.

The final output of the template (generated for Northwind Category table) is figured below:

SqlDB class is the class containing Execute, ExecuteScalar, ExecuteReader, FillTable methods, which are responsible for creating database connection and execute queries and stored procedure on the database.

AbstractDataProvider is the base class for each Entity DataProvider class, it abstracts the definitions for the common base methods such as select, insert, update and delete.

AbstractEntity Class is the base class for each Entity class. IComparable interface is implemented to allow dynamic sorting for domain objects into lists; for more info about default comparer see :
http://blogs.a-h-m-e-d.com/Blogs/post/Another-dynamic-comparer-for-sorting-lists-(-very-simple-).aspx

Features:
- DeleteByFlag:

This feature allows you to specify a boolean column in a given table to be a flag for deleted rows instead of deleting them permenantly from database.
Select stored procedures select only rows of a table which has the DeleteFlagColumn set to 0 (False). Delete stored procedure just updates the colum to 1 (True) instead of the delete action.

-UseCollection:
Returned rows can be returned as generic lists if UseCollections option is set to True else a DataTable is returned.

-Generator template:
This template will generate all classes and stored procedures needed for interaction with database for given table(s).

Generator.rar (13.38 KB)

Apr 10

Drag and drop over images (Javascript)

Posted By Ahmed El-Kilani On 10 Apr 2008 No Comments »

To drag an image or drag and drop over images you need to handle the mousedown, mousemove and mouseup events, But you cannot drag an image in an html document directly, this means you cannot handle those events for the img element itself. You cannot say:


    

All we have to do is to handle those events for the document element and detect what we are draging over.
The source/target element which is sent to the handler is the element we are draging over:
//Called when the mouse moves over the document.
function documentDragOver()
{
    var el = e ? e.target : event.srcElement;
}
Here is the complete code in order to drag an image:
var ie = document.getElementById;
var dragStarted = false;
var objToDrag;

function mouseMove()
{
  if (objToDrag && dragStarted)
  {    
    objToDrag.style.left = (ie ? event.clientX : e.clientX) - diffX;
    objToDrag.style.top  = (ie ? event.clientY : e.clientY) - diffY;
    return false;
  }
}

document.onmousedown = function() 
{  
  objToDrag = ie ? event.srcElement : e.target;
  
  if (objToDrag && objToDrag.className == "ddImg")
  {
    dragStarted = true;
    diffX = (ie ?  event.clientX : e.clientX) - parseInt(objToDrag.offsetLeft);
    diffY = (ie ?  event.clientY : e.clientY) - parseInt(objToDrag.offsetTop);
    document.onmousemove = mouseMove;
    return false;
  }
}

document.onmouseup = function(){
	dragStarted = false;
}