>public int AddSite(Model.Site site) > { > int result = 0; > > using (ITransaction transaction = this.Session.BeginTransaction()) > { > try > { > result = (int)this.Session.Save(site); > transaction.Commit(); > } > catch (HibernateException) > { > transaction.Rollback(); > throw; > } > } > > return result; > } >>
>public Model.Site GetSite(int id) > { > Model.Site result = null; > > using (ITransaction transaction = this.Session.BeginTransaction()) > { > result = this.Session.Get<Model.Site>(id); > > transaction.Commit(); > } > > return result; > } >>
>public void SaveSite(Model.Site site) > { > using (ITransaction transaction = this.Session.BeginTransaction()) > { > try > { > this.Session.Update(site); > > transaction.Commit(); > } > catch (HibernateException) > { > transaction.Rollback(); > throw; > } > } > } >>
>public IList<Model.ContactUs> GetContactUs(int startIndex, int rowCount, string sort, int siteIDFilter, string submitterLastNameFilter, bool showAllFilter) > { > IList<Model.ContactUs> result = null; > > using (ITransaction transaction = this.Session.BeginTransaction()) > { > ICriteria query = this.Session.CreateCriteria<Model.ContactUs>(); > > //Add paging... > query.SetFirstResult(startIndex).SetMaxResults(rowCount); > > //Add filters... > query.Add(Expression.Eq("Site.ID", siteIDFilter)); > if (submitterLastNameFilter != null) > { > query.Add(Expression.Like("LastName", submitterLastNameFilter, MatchMode.Anywhere)); > } > if (showAllFilter == false) > { > query.Add(Expression.Eq("IsNew", true)); > } > > //Add sort... > switch (sort) > { > case "IsNew ASC": > query.AddOrder(new Order("IsNew", true)); > break; > case "EnteredDate ASC": > query.AddOrder(new Order("EnteredDate", true)); > break; > case "SubmitterName ASC": > query.AddOrder(new Order("LastName", true)); > break; > case "IsNew DESC": > query.AddOrder(new Order("IsNew", false)); > break; > case "EnteredDate DESC": > query.AddOrder(new Order("EnteredDate", false)); > break; > case "SubmitterName DESC": > query.AddOrder(new Order("LastName", false)); > break; > } > > result = query.List<Model.ContactUs>(); > > transaction.Commit(); > } > > return result; > } >Hi,
//Dummy parameters string submitterLastNameFilter = "Tom"; int startIndex = 100; int rowCount = 50; bool isNew = true; var query = Session.ContactUs .Where((c)=> c.LastName.StartsWith(submitterLastNameFilter)) .Where((x)=> x.IsNew == isNew) .Skip(startIndex) .Take(rowCount); var result = query.ToList();I've obviously dropped the conditional code but could implement the same thing in the same way is neccessary. Even so, to me, my code looks both more compact and more readable. Plus I get type safety which you don't