Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
EF 4.0 vs. NHibernate
Message
De
07/01/2010 03:57:43
 
 
À
06/01/2010 14:41:12
Mike Cole
Yellow Lab Technologies
Stanley, Iowa, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
01442310
Message ID:
01442567
Vues:
55
>>>>http://ayende.com/Blog/archive/2010/01/05/nhibernate-vs.-entity-framework-4.0.aspx
>>>>
>>>>There isn't much of a comparison. Microsoft should just scrap EF and adopt NHibernate as their official ORM solution.
>>>
>>>Terrific. I just received an EF book from Amazon yesterday, planning to read it on the train. Maybe not the best investment of my time at this point?
>>
>>Hi,
>>
>>I'd take the info on Mike's link with a pinch of salt (not a large pinch, but a pinch nonetheless) - NH may be better than EF (I'm not qualified to judge) but EF is not as inferior as implied. Also, there are a lot of new features in EF4 that, AFAICS, weren't really taken into account in that assessment.
>>And that syntax I posted (copied from the link) looks horrific. I might be wrong but the argument that NH can produce more optimised SQL seems to be based on the fact that you have to tell it exactly what to do using very convoluted syntax.
>>
>>What book do you have - I'd guess Julia Lermans?
>>If so I'd say it's a good book but in danger of making EF sound more compicated than it need be for general use.
>>
>>Best,
>>Viv
>
>Viv,
>That is not entirely the case. That was more of an extreme scenario, and not one I've had to deal with since using NHibernate. Here are more practical samples:
>
>Insert...
>
>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;
>        }
>
>
>Select...
>
>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;
>        }
>
>
>Update...
>
>public void SaveSite(Model.Site site)
>        {
>            using (ITransaction transaction = this.Session.BeginTransaction())
>            {
>                try
>                {
>                    this.Session.Update(site);
>
>                    transaction.Commit();
>                }
>                catch (HibernateException)
>                {
>                    transaction.Rollback();
>                    throw;
>                }
>            }
>        }
>
>
>Select w/ paging and dynamic ordering...
>
>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,
OK. I'll argue for EF for the sake of it (g)
The syntax above looks similar to EF. Just taking the last one (since it's the most complex) I'd use something like this is in EF:
//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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform