The difference between NHibernate and EF (Entity Framework)

Source: Internet
Author: User

Original address:

Http://www.cnblogs.com/lukun/archive/2011/05/16/2047587.html

It's a nice piece of writing.

The memory of the Lost Youth
Source: http://www.cnblogs.com/lukun/
This article copyright to the author and blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph of the statement, and in the article page obvious location to the original connection, if there is a problem, you can contact me through http://www.cnblogs.com/lukun/, thank you very much.


For a long time, programmers and databases have always had a delicate relationship, and in business applications, databases must be essential components, making it necessary for programmers to learn SQL instructions to connect to and Access databases, at least for me, which I think is a very unpleasant thing. Therefore, many people in the information industry are studying how to integrate the program design model and the database, the object relationship (Object-relational Mapping) of the technology is born, such as Hibernate or NHibernate is the product of this technology, Microsoft has not yet launched a similar framework, and still relies on ado.net, the traditional data access tool. It is estimated that Microsoft also heard complaints from programmers, so from a objectspace (ObjectSpace) from one to 2005 years early. Proposed that the concept of the application being able to connect to and access the database in a fully object-based way, with a technical concept similar to that of NHibernate, was finally released in 2008 Ado.net Entity framework with the. NET Framework 3.5 SP1, An entity-oriented database access framework that comes with a graphical designer.

As shown in figure:

NHibernate

NHibernate is an object/relational database mapping tool that targets the. NET environment. Object-Relational Mapping (O/R mapping,object Relational Mapping) represents a technique for mapping objects represented by an object model to an sql-based relational model data structure. NHibernate is a ms.net o/R mapping Persistence framework that is ported from a java-based hibernate project. The O/R mapping is the record of the object to the mapping relational database, simply to realize that an object is stored as a record in the datasheet and a corresponding object is created by a record, and the data in the data table is the attribute of the object.

NHibernate not only manages the mapping of. NET classes to database tables, including mappings of. NET data types to SQL data types, but also provides methods for querying data and fetching data, drastically reducing the time we use to process data manually using SQL and ado.net during development. The goal of NHibernate is to liberate 95% of the programming tasks associated with the developer's usual data persistence. And keep in mind that nhibernate as a database access layer is tightly integrated with your program.


So why use the O/R Mapping. It is different from the traditional dataset/datatable.

 
The first is the difference in design, when using O/R mapping, more from the object's perspective to design the program, and the data (object properties) stored details behind, can be completely object-oriented (OO) way to design, while using dataset/datatable, It is just the object that holds the data, looks more like a data table, cannot express the business concept intuitively.

nhibernate Architecture

A very abstract overview of the NHibernate architecture

Demonstrates that NHibernate provides a persistent layer between the database and the application.

The first picture seems very simple. In fact, NHibernate is more complicated. We understand two extreme situations, lightweight and heavyweight architectures. Here's the second picture: Lightweight systems, where applications provide ado.net connectivity and manage transactions themselves.

Last Picture: Heavyweight system: All the underlying ado.net APIs are abstracted.

Hibernate Advantages:

(1) object/relational database mapping (Basic O/R Mapping)

It only needs to manipulate objects, so that the development of more object-oriented, abandoned the idea of the database center, fully object-oriented thinking.

(2) Transparent persistence (persistent)

A Single-threaded object with a persistent state with a business function that has a short lifetime. These objects may be ordinary Javabeans/pojo, this object does not implement a Third-party framework or interface, the only special is that they are associated with (just one) session. Once this session is closed, the objects are detached from the persisted state, which can be freely used by any layer of the application. (for example, a data transfer object used to deal with the presentation layer.) )

(3) transaction transaction (org. Hibernate.transaction)

An object that the application uses to specify the range of atomic action cells, which is single-threaded and has a short lifecycle. It separates applications from the underlying JDBC, JTA, and CORBA transactions through abstraction. In some cases, more than one transaction object may be included within a session. Although it is optional to use this object, the opening and closing of transaction boundaries is essential whether using the underlying API or using the transaction object.

(4) It is not intrusive, that is, the so-called lightweight framework.

(5) Transplant will be very good.

(6) Caching mechanism. Provides first-level caching and level two caching.

(7) Concise HQL programming.

Hibernate Disadvantages:

(1) Hibernate in the batch data processing is a disadvantage.

(2) For an object (a single object) simple check change \ delete increase, not batch modification, deletion, suitable for using Hibernate, and for batch modification, deletion, not suitable for hibernate, which is also the weakness of the or framework; To use a specific optimization mechanism for a database, Not suitable for hibernate.

Entity Framework

 

In the. Net framework SP1 Microsoft includes an Entity Framework (Entity framework) that can be understood as an ORM product of Microsoft. Used to enable developers to create data access applications by programming the conceptual application model rather than directly to the relational storage schema. The goal is to reduce the amount of code required for data-oriented applications and to mitigate maintenance efforts.

The Entity Framework application has the following advantages: applications can work through more application-centric conceptual models, including types with inheritance, complex members, and relationships. Applications no longer have hard-coded dependencies on a particular data engine or storage schema. You can change the mapping between the conceptual model and the storage-specific schema without changing the application code. Developers can use a consistent Application object model that can be mapped to various storage schemas, which may be implemented in different database management systems. Multiple conceptual models can be mapped to the same storage schema. Language-Integrated Query support provides a query with compile-time syntax validation for a conceptual model.

The Entity Framework Entity Framework is a set of technologies in ado.net that support the development of data-oriented software applications. The Entity Data Model (EDM) in the EF is defined by the following three models and the mapping file with the appropriate file name extension. Conceptual schema definition language file (. csdl)-defines a conceptual model. Storage schema definition language files (. ssdl)--Define the storage model (also called the logical model). mapping specification language files (. MSL)-Defines the mapping between the storage model and the conceptual model.

The Entity Framework uses these xml-based models and mapping files to transform the creation, read, update, and delete operations of entities and relationships in the conceptual model into equivalent operations in the data source. The EDM even supports the mapping of entities in the conceptual model to stored procedures in the data source. It provides the following ways to query the EDM and Return objects: LINQ to Entities--provides language-integrated query (LINQ) support for querying entity types defined in the conceptual model. Entity SQL-A storage-independent SQL dialect that directly uses the entities in the conceptual model and supports EDM features such as inheritance and relationships. Query Builder methods-you can construct Entity SQL queries using LINQ-style query methods.

The following illustration shows the Entity Framework architecture for accessing data:

Some of the drawbacks I think:

· EDMX contains all the objects of the CSDL,SSDL,MSL file, too large, if you want to manually modify this file, accidentally, eyes look at the flowers, it is wrong. (As with the dataset).

· Currently the EF supports tables, attempts, stored procedures, other objects are not supported, and there are many restrictions on using stored procedures (there are currently efextension to provide more object support).

· In addition to the visual design interface that MS SQL Server can provide directly, other databases currently do not provide a visual design interface (but can be implemented by themselves, as described later).

· Performance issues. (Online see there is said than ado.net slow 70,000, and some people say faster than ado.net, the specific situation I have not tested, but I feel like this type of framework, performance is certainly more than the original ecological ado.net slow)

Case:

The

Entity Framework uses an abstract data structure to convert each database object into an Application object (Entity), while a data field is converted to a property, and the relationship is converted to a binding attribute (association), allowing the database's The E/R model is completely turned into an object model, so that programmers can invoke access in the most familiar programming languages. Under the abstract structure, it is a highly integrated and corresponding structure of the concept layer, corresponding layer and storage layer, as well as supporting the Entity Framework of the data provider (provider), so that the work of data access to the smooth and complete. The Ado.net Entity Framework is based on the Entity data Model (EDM), which divides the logic layer into three blocks, namely conceptual schema, Mapping schema and Storage schema three Layers:
(1) Concept layer: Responsible for upward object and attribute exposure and access, so that the upper layer of the application code can be as object-oriented way to access data. This section is automatically generated by the designer and is represented in a series of classes.
(2) corresponding layer: The above concept layer and the underlying storage layer of the data structure, responsible for the upper layers of the concept layer structure and the lower storage structure of the members together to confirm the source and flow of data. This part is realized by the description language and can be modified freely.
(3) storage layer: According to different database and data structure, and revealing the data structure of the entity, responsible for the database management system (DBMS) in the datasheet to do the entity correspondence (physical Mapping), so that the data can be entered in the correct data source, or from the correct data source. &NBSP

for visitors (upper-level logic), there are three ways to access the Edm:entity client,object context and linq 

1. Entity Client (Database operation access method)

The Entity client is the local client (Native client) in the Ado.net Entity Framework, and its object model is very similar to the other client side of Ado.net, with Connection, Command, Datare Ader, but the biggest difference is that it has its own SQL instructions (Entity sql), you can access the EDM in SQL, simply, the EDM as an entity database.

Entityconnectionstringbuilder Entitybuilder = new Entityconnectionstringbuilder (); Entitybuilder.provider = providername; entitybuilder.providerconnectionstring = providerstring; using (entityconnection conn = new EntityConnection (entitybuilder.tostring ())) {Conn. Open (); EntityCommand comm = new EntityCommand ("SELECT * from Student", conn); EntityDataReader reader = Comm. ExecuteReader (); Conn. Close (); }

2. Object context (How to access Objects operations)

Because the Entity client is too standard, and not very consistent with the spirit of ORM, so Microsoft on the upper level of Entity client with a direct access to the programming language interface, it can take the EDM as an object-like access, this interface is the objects context (Ob Ject Service), any action on the EDM in Object context is automatically converted to Entity SQL to be executed in the EDM. I personally prefer to use this approach.

Dbforentityentities db = new dbforentityentities (); ObjectQuery < Student > que = db. Student.where ("it. Stname= @P1 ", New ObjectParameter (" P1 "," N1 ")); foreach (Student st in Que) {//Todo something}

3. LINQ (Access mode for LINQ operations)

The object context changes the access of an EDM to a collection of objects, which gives LINQ the space to play, so that LINQ to Entities is also generated, simply by using LINQ to access the EDM, so that the functionality of LINQ can be According to the library to play.

Dbforentityentities db = new dbforentityentities (); ObjectQuery < Student > students = db. Student; IQueryable < Student > Que = from P in students select p;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.