Blogger Widgets
  • Sharing Photos using SignalR
  • TFS Extenstion - allows copy work items between projects
  • Displaying jquery progressbar with ajax call on a modal dialog
  • Managing windows services of a server via a website
  • Exploring technologies available to date. TechCipher is one place that any professional would like to visit, either to get an overview or to have better understanding.

Search This Blog

Thursday, 20 October 2011

Generate summary using multiple aggregations with NHibernate

NHibernate allows adding multiple aggregations at the same time for a single ICriteria and generates multiple objects. Below sample shows how this can be achieved

using (var session = _sessionFactory.OpenSession())
            {
                ICriteria criteria = session.CreateCriteria(typeof(InvoiceLine));
                criteria.Add(Restrictions.Where(i => i.Invoice.ID == 1234));
                criteria.SetProjection(Projections.ProjectionList()
                    .Add(Projections.Count("ID"))
                    .Add(Projections.Sum("Price"))
                    .Add(Projections.Sum("Quantity"));
                object[] retObjects = (object[])criteria.UniqueResult();    
            }

Now you have got some list of objects which you can convert to your desired type. So you have to manually covert them, how about if NHibernate does this for you. First create a summary class which does not need to be part of you database
public class InvoiceSummary
    {
        public int LineCount;
        public long LineTotalPrice;
        public long TotalQuantity;        
    }
Now populate the data
      using (var session = _sessionFactory.OpenSession())
            {
                ICriteria criteria = session.CreateCriteria(typeof(InvoiceLine));
                criteria.Add(Restrictions.Where(i => i.Invoice.ID == 1234));
                criteria.SetProjection(Projections.ProjectionList()
                    .Add(Projections.Count("ID").As("LineCount"))
                    .Add(Projections.Sum("Price").As("LineTotalPrice"))
                    .Add(Projections.Sum("Quantity").As("TotalQuantity")));
     .SetResultTransformer(NHibernate.Transform.Transformers.AliasToBean(typeof(InvoiceSummary)));
                InvoiceSummary summary = (InvoiceSummary)criteria.UniqueResult();
            }
NHibernate.Transform.Transformers.AliasToBean does the job of using the alias names to assign the properties of InvoiceSummary object Technological progress has merely provided us with more efficient means for going backwards. ~Aldous Huxley

Wednesday, 19 October 2011

Using NHibernate Criteria with Join to get row count

Google for lot of places for using Criteria object having joins and get rowcount either by using Projections.RowCount or CriteriaTransformer.TransformToRowCount. Now I have started looking into NHibernate source code which does also provides with examples of how to use it. So the example is as follows :-

  public void TransformToRowCountTest()
  {
   ISession s = OpenSession();
   ITransaction t = s.BeginTransaction();

   ICriteria crit = s.CreateCriteria(typeof(Student));
   ICriteria subCriterium = crit.CreateCriteria("PreferredCourse");
   subCriterium.Add(Property.ForName("CourseCode").Eq("MCSD"));


   ICriteria countCriteria = CriteriaTransformer.TransformToRowCount(crit);

   int rowCount = (int)countCriteria.UniqueResult();

   t.Rollback();
   s.Close();
  }
This does the job of counting number of students who are enrolled for a course with code "MCSD" The real danger is not that computers will begin to think like men, but that men will begin to think like computers. ~Sydney J. Harris

Wednesday, 12 October 2011

Add or Modify model data before submit in ASP.NET MVC when using jQuery Form Plugin

Having recently started using ASP.NET MVC for a website I am quite impressed the way MVC framework works as opposed to standard ASP.NET web forms.

Also coupled the website with jQuery Form Plugin which works seamlessly with ASP.NET MVC. jQuery Form Plugin basically provides various options such as
beforeSubmit - to validate before submitting data
success - to refresh/update content after success form submition

All works great, now what I actually need is to change data before submit so I have made some changes to jQuery Form Plugin as follows :-

.....
.....
// give pre-submit callback an opportunity to abort the submit
if (options.beforeSubmit && options.beforeSubmit(a, this, options) === false) {
    log('ajaxSubmit: submit aborted via beforeSubmit callback');
    return this;
}

// give addDataBeforeSubmit an opportunity to add custom data to be send along with form submit
if(options.addDataBeforeSubmit)
{
 var moreData = options.addDataBeforeSubmit();
 $.each( moreData, function( i, item ) {
  var bExists = false;
  $.each( a, function( j, aitem ) {
   if(aitem.name == item.key){
    aitem.value = item.value;
    bExists = true;
   }
  });
  if(!bExists)
  {
   a.push({ name: item.key, value: item.value });
  }
 });
}

// fire vetoable 'validate' event
this.trigger('form-submit-validate', [a, this, options, veto]);
if (veto.veto) {
 log('ajaxSubmit: submit vetoed via form-submit-validate trigger');
 return this;
}

.....
.....


So this should cope with changing/adding new values to data that is to be sent for controller action (post)

Man is still the most extraordinary computer of all.
~John F. Kennedy
Copyright © 2013 Template Doctor . Designed by Malith Madushanka - Cool Blogger Tutorials | Code by CBT | Images by by HQ Wallpapers