NHibernate vs EF 4.1+

I am trying to solve the pros and cons of 2 ORMs primarily in this area.

  • Compatible with Sql Server 2008 R2 and 2012.
    • This is very important because we simply do not have the resources to debug poor support for the existing MS technology stack.
    • Also, planning ahead since 2012 is largely coming out and plans to switch to it on the spot.
  • Support for .NET 4.0 and 4.5 (upcoming)
    • Again, it is very important for the same reason as above.
  • Transaction processing and table hints, for example. forcecan forceek read unstudied.
    • Many times, the query optimizer does its job well; in other cases, I want the flexibility to tell you what to do.
  • Talk support, self-support and disconnection
    • This is somewhat fundamental. I hate to keep the session open for a long period. Especially in a distributed computing / web farm environment.
  • Ability to process the first development of code, create and update the scheme without destroying data.
    • EF 4.1 seems desirable in this regard, although 4.3 is a leap and a snap. They also like data annotations better than individual mapping classes in NH. The reason I want to be able to send to the class, and from there I can create a save model without extending the method for the matching classes.
  • IRepository Template Support
  • LINQ Support
    • Somewhat related to (6) above, I want very good linq support. I do not want developers to guess around with a lower level of implementation and force themselves to adhere to one specific ORM.
  • Performance
  • Support for massive CRUD operations without having to load data into the application layer. For instance. I want to increase all the rows in a column of a particular table by 1. I do not want to load this into memory and grow rows one by one. That would be crazy for such a simple operation. Linq2Sql used Magiq to handle the kind of things that NH and EF have?
  • Caching, reliable loading, lazy loading, navigation properties.
    • Let me be frank. I hate these things when this is done implicitly. The reason is that it's hard to distinguish what is cached out of date from the new. In EF, I usually refuse all navigation properties because I do not want these properties to be loaded in the top 5, because this is what the current operation requires, but even further downstream another developer tries to make an account that will be inaccurate.
    • So, my personal policy is that all SOAs should be stateless unless there is a good reason. If you need links to data, get them from perseverance. It will be slower, but the code will be more readable and flexible.
    • Similarly for caching. It is quite complex, since it is in a distributed environment, I want all caching to be very explicit. If a developer wants to encode a cache, he should do it, not the code against ORM, and make him look like he is pulling fresh data out of perseverance when in fact he gets stale data.
    • Now the question is whether NHibernate has any concept / abstraction that makes caching, impatient / lazy loading much more explicit than the one currently available in EF. In this case, I do not care much about the ease of development; I care more about clarity and clarity.

Also, I don't care about OSS against a proprietary argument. The team cannot afford the time to look under the hood and start messing with other people's code. I care more about how β€œit just works” than anything else.

+6
source share
5 answers

You can use bltoolkit;) β†’ http://www.bltoolkit.net/Doc.Linq.ashx

Just read 2 minutes to read it -> http://www.bltoolkit.net/Doc.LinqModel.ashx

But shorter

  • very good Linq support.
  • DML Operations (nr 9)
  • excellent performace / bulk support
  • perfectly generated Sql
  • enum support; -)
  • lack of lazy loading / entity tracking / special caching, you are now the things that usually cause problems.
  • No magic features β†’ less abstraction β†’ less leaks β†’ less problems β†’ lesser learning curve

btw it is nr 3, it has the TableFunction and TableExpression attributes to support UDF table values, tooltips and other decorations in the table. This way you can write your own extension methods for use in your Linq instructions, for example

[TableExpression("{0} {1} WITH (TABLOCK)")] public Table<T> WithTabLock<T>() where T : class { return _ctx.GetTable<T>(this, ((MethodInfo)(MethodBase.GetCurrentMethod())).MakeGenericMethod(typeof(T))); } 

and then

 using (var db = new TestDbManager()) { var q = from p in new Model.Functions(db).WithTabLock<Parent>() select p; q.ToList(); } 

DML Operation Example

  db.Employee .Where(e => e.Title == "Spectre") .Set(e => e.Title, "Commander") .Update(); 
+6
source

While I wrote about some of these problems in yet another question , I think it is worth answering this on one point.

  • Both are compatible with all versions of SQL Server.
  • Both are compatible with .NET 4.x. NH is still focusing on 3.5, which is not a problem.
  • Both have transaction processing. It also does not support query hints in native query languages, but both allow you to use SQL.
  • Both allow you to reconnect disconnected objects. I personally don't think this is a good practice (I prefer passing DTO around)
  • Schema migration is more mature and flexible in EF. Mapping is the way, better in NH. It has support for matching with attributes, which is much more powerful than the EF equivalent that you will quickly find, does not even support basic things (e.g. specifying the precision of a decimal field).
  • You can implement your repository on top of one of them.
  • Both support LINQ. EF supports a wider range of requests, at the cost of creating extremely inefficient ones. You should not omit your methods of development; Each request method has its advantages, and a good developer should know the options.
  • Performance is usually better with NH due to display flexibility and support for batch processing, caching, and DML.
  • EF does not support bulk operations (DML). NH does using INSERT / UPDATE / DELETE statements, which are very similar to SQL, but on the object model (for example, you can specify conditions using dot syntax instead of explicit joins)
    • EF does not support caching, period. There are some unsupported samples that are not ready for production. All caching in NHibernate is explicit (you specify which entities, collections, and requests you want to cache, for how long, etc.). It supports distributed caches such as memcached, so it can also keep track of obsolete cache data.
    • Both allow you to explicitly download links and collections. NH has, in my opinion, the best proxy design that does not pollute your model with FK (this is a long topic, you can post the next question if you want). And as usual, you have more flexibility in deciding how all relationships should be loaded.

In a nutshell: NH is more flexible and more efficient, hands down. EF has better migration support, a slightly lighter learning curve, and a more comprehensive LINQ provider.

+10
source

First of all, I prefer NHibernate in various aspects. Entity Framework is missing something else in version 5.0.

  • It is very simple to add a new type to NHibernate, so if SQL Server 2012 has new types, you can implement a relative dialect / provider, but the community always works.
  • I know that NH team is working on support for FW 4.5, EF supports it from 5.0
  • Using NH, you can do whatever you want.
  • NH supports the Merge method to reattach an object, EF also
  • EF does not support naming conventions, NH does, I also write auto mapper based on DataAnnotations, here is a link to the old version, wait 2 weeks for the new one.
  • Using NH or EF, you can implement a data layer with repository and unit of work templates, I must also release this package on NuGet
  • EF fully supports LINQ, NH, but you can fix it with the point extension
  • I think that performances are on the same level, NH better supports the second level cache, therefore it is easy to prevent getting into the database.
  • NH has improved batch support, I don't know about EF (especially 5.0)
  • NH has a better implementation of extension points, so proxy / caching / logging is more powerful than EF, but in EF you can write wrapper to get what you need.

After all, with NH it’s easier to implement a DDD template because the display is more flexible.

+8
source

You do not want any of them. You will not be able to specify arbitrary hints for tables, and it will not be as flexible as you want.

If all these are requirements, there is no ORM on the planet that will meet them.

EDIT:

Based on your comment. nHibernate will give you maximum power, but at the cost of increased complexity when setting up your model. There are some tools that can help, but each one has pros and cons.

EF is easier to set up and use, but less powerful. nHibernate is the opposite. You must choose between power and complexity.

By the way, regarding operations with Bulk, and what is not, you can simply infer SQL directly from the framework for such operations. Or call the stored procedure.

+1
source

Just to add to the other answers ...

Ricardo Perez wrote a very good, unbiased comparison of NHibernate and Entity Framework a few days ago. This may help you in your research.

You can find it here: http://weblogs.asp.net/ricardoperes/archive/2012/06/07/differences-between-nhibernate-and-entity-framework.aspx

+1
source

Source: https://habr.com/ru/post/917909/


All Articles