Showing posts with label NHibernate. Show all posts
Showing posts with label NHibernate. Show all posts

Wednesday, July 9, 2008

How to copy hibernate.config to the VSTS Output Folder?

VSTS is now included by default in the standard/professional version of vs.net 2008. Until now i was using NUnit + TestDriver.net, etc.

I've decided to test VSTS and found 1 big problem : my nhibernate.config file was not copied to the output directory of the test output folder. I was expecting this to be automatically since I've set the "Always Copy" in the build property for the file.

Doing some basic Google searches I've noticed I am not the only guy having this problem.

The solution is to open the "localtestrun.testrunconfig" file which is added automatically to the solution by VSTS, go to "Deployment" and then add the file you want to deploy/copy.










Once this is done, the hibernate.config file copied every time we run a test. This can be seen in the image below. VSTS creates 1 output folder for each test run.

Tuesday, April 8, 2008

ObjectDataSource : Manually controlling the sorting expression.

By default the GenWise ASP.NET Templates uses the object datasource SortParamterName. When that property is used , the objectdatasource will manipulate internally the sort expression (change to asc , desc) .

  269     ObjectDataSource runat="server" ID="ObjectDataSource1" TypeName="BOLayer.CustomerFactory"
  270                 SelectCountMethod="Count" EnablePaging="True" UpdateMethod="Save" DeleteMethod="Delete"
  271                 SortParameterName="pSortExpression" MaximumRowsParameterName="pMaxResult" StartRowIndexParameterName="pFirstResult"
  272                 SelectMethod="GetAll">
  273                 
  274                     Parameter Name="pSortExpression">Parameter>
  275                     Parameter Name="pCriteria">Parameter>
  276                 
  277                 
  278                     Parameter Name="CustomerID">Parameter>
  279                 
  280             ObjectDataSource>


For more advanced sorting scenarios you might want to manually control what's passed to the pSortExpression method parameter. This can be done in the following way :

Subscribe to the ObjectDataSource Selecting method (genwise does that automatically)
   44 ObjectDataSource1.Selecting += new ObjectDataSourceSelectingEventHandler(ObjectDataSource1_Selecting);



  253    protected void ObjectDataSource1_Selecting(object pSender, ObjectDataSourceSelectingEventArgs pEventArgs)
  254         {
  255             if (pEventArgs.ExecutingSelectCount)
  256                 pEventArgs.InputParameters.Remove("pSortExpression");
  257             else
  258                 pEventArgs.InputParameters["pSortExpression"] = _newSortExpression;
  259 
  260             pEventArgs.InputParameters["pCriteria"] = Browse1_GetCriteria();
  261         }

Where does _newSortExpression come from ?


  198         string _newSortExpression = null;
  199         protected void Browse1_Sorting(object pSender, GridViewSortEventArgs pEventArgs)
  200         {
  201             // Calls this method to adapt the sort syntax (required for Composite sorting).
  202             _newSortExpression = WebUtils.SortExpressionTransformer(pEventArgs);
  203             pEventArgs.Cancel = true;
  204         }


SortExpression Tranformer takes the ObjectDataSource parameters and manipulates them to make them compatible with the GenWise Factories sorting syntax.

Below is the code of this method:

   63   public static string SortExpressionTransformer(GridViewSortEventArgs pEventArgs)
   64     {
   65         if (pEventArgs == null) throw new ArgumentNullException("pEventArgs");
   66 
   67         if (pEventArgs.SortDirection == SortDirection.Ascending)
   68         {
   69             return pEventArgs.SortExpression;
   70         }
   71 
   72         string newSortExpression = pEventArgs.SortExpression;
   73         string DESC = " DESC";
   74 
   75         if (pEventArgs.SortDirection == SortDirection.Descending)
   76         {
   77             if (pEventArgs.SortExpression.Contains(","))
   78             {
   79                 // Case : We have a composite sorting ( Field1,Field2) 
   80                 newSortExpression = string.Empty;
   81                 string[] sortParts = pEventArgs.SortExpression.Split(',');
   82                 for (int i = 0; i <> 
   83                 {
   84                     string sortPart = sortParts[i];
   85 
   86                     int sortDirectionIndex = sortPart.IndexOf(DESC);
   87                     bool isDescending = (sortDirectionIndex > -1);
   88 
   89                     string property = sortPart;
   90                     if (isDescending)
   91                     {
   92                         // sta : udpate templates.
   93                         //property = sortPart.Substring(0, (sortPart.Length - sortDirectionIndex + 1));
   94                         property = sortPart.Substring(0, (sortPart.Length - sortDirectionIndex + 1 + DESC.Length));
   95                     }
   96 
   97                     if (i > 0) newSortExpression += ",";
   98                     newSortExpression += property;
   99 
  100                     // Only add the DESC to all except the last (Last one will be controlled automatically by the object datasource.
  101                     if (!isDescending)
  102                     {
  103                         if (i < (sortParts.Length - 1))
  104                             newSortExpression +=  DESC ;
  105                     }
  106                 }
  107                 pEventArgs.SortExpression = newSortExpression;
  108             }
  109         }
  110         return newSortExpression;
  111     }

Friday, December 21, 2007

NHibernate and WinForms article (1st draft)

As I've mentioned before, I am trying to create a Article for using NHibernate with Windows Forms (WinForms).

Both the article and the example project are not finished (it's work in progress) but I can't wait any longer , and therefore decided to share and get some initial feedback / reactions.