Article Source: http://www.cnblogs.com/GoodHelper/archive/2011/02/14/nhiberante_01.html
I. Introduction of NHibernate
what is? NHibernate? NHibernate is an object/relational database mapping tool for the. NET environment. The term object/relational database mapping (Object/relational mapping,orm) represents a technique used to map objects represented by object models to SQL-based relational model data structures.
NHibernate's goal is primarily for programming tasks related to data persistence, freeing developers from the writing of the original tedious SQL statements, freeing the developer to invest in the implementation of the business logic. For data-centric programs, developers often use stored procedures in a database to implement business logic, in which case nhibernate may not be the best solution, but for middle-tier applications that are based on. NET and are able to implement the OO business model and business logic. NHibernate is the most useful. NHibernate can help users eliminate or package SQL code for specific vendors and help users transform the result set from a tabular representation into a series of objects.
NHibernate is one of the most extensive open source object-relational mapping frameworks currently in use, It has a very lightweight object encapsulation of Java JDBC (similar to ADO), which allows programmers to manipulate the database at will using object programming thinking, which is now popular in the domestic Java development Community, hibernate+ Spring is often a requirement for many Java companies to recruit. And NHibernate, as the name implies, as Nunit,nant, is based on. NET hibernate implementation, but the current introduction of NHibernate very little information, lack of a systematic complete tutorial to fully show and deep nhibernate, and now NHibernate document is incomplete, less poor, Many nhibernate learners tend to learn through Hibernate's documentation, but not all of them. NET developers are familiar with Java, and not all of us have the time to learn Java, so I'm going to start a step by step NHibernate tutorial so that interested friends can quickly familiarize themselves with NHibernate, Experience the fun of nhibernate development faster.
NHibernate is a. Net-based object persistence class library for relational databases. NHibernate comes from a very good Java-based hibernate-relational persistence tool.
NHibernate from the bottom of the database to persist your. Net objects to the relational database. NHibernate handles this for you, much more than you have to write SQL to access objects from the database. Your code is only associated with objects, Nhibernat automatically generates SQL statements, and ensures that objects are submitted to the correct tables and fields.
Two, ORM Introduction
What is ORM? Object-Relational mapping (Object/relation Mapping, or ORM) is a result of the development of object-oriented software development methods. The object-oriented development method is the mainstream development method in the enterprise application development environment, and the relational database is the mainstream data storage system which is stored permanently in the enterprise-level application environment. object and relational data are two representations of business entities, and business entities behave as objects in memory and behave as relational data in the database. There are associations and inheritance relationships between objects in memory, and in a database, relational data cannot directly express many-to-many associations and inheritance relationships. Therefore, object-relational mapping (ORM) system usually exists in the form of middleware, which mainly realizes the mapping of program object to relational database data.
Object-oriented is developed from the basic Principles of software engineering (such as coupling, aggregation, encapsulation), and the relational database is developed from the mathematical theory, and the two sets of theories have significant differences. In order to solve this mismatch, object-relational mapping technology emerged.
Let's start with the O/R. The letter O originates from "object", and R is derived from "relationship" (relational). In almost all programs, there are objects and relational databases. In the business logic layer and the user interface layer, we are object-oriented. When the object information changes, we need to keep the object's information in the relational database.
If you open your recent program (for example, PetShop4.0) and look at the DAL (Database access Layer) code, you will definitely see a lot of approximate generic patterns. As an example of how to save an object, you pass in an object, add SqlParameter to the SqlCommand object, correspond all the properties and objects, set the CommandText property of the SqlCommand to the stored procedure, and then run SqlCommand. Write the code repeatedly for each object. Besides, is there a better way? There, introduce a O/R Mapping. Essentially, an O/R mapping will generate a DAL for you. Instead of writing the DAL code yourself, use the O/R Mapping. You use O/R mapping to save, delete, read objects, O/R mapping is responsible for generating SQL, you just need to care about the object just fine.
Third, the advantages and disadvantages of Nhiberante
3.1 Advantages
(1). Object-oriented: The use of nhiberante only need to manipulate objects, so that the development of more object, abandoned the idea of the database center, a complete object-oriented thinking.
(2). Transparent persistence: A single-threaded object with a persisted state with a business function that has a short lifetime. These objects may be ordinary Poco, 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 left out of the persistence state, which can be used freely by any layer of the application. (for example, as a data transfer object that deals with the presentation layer.) )
(3). It is not intrusive, that is, the so-called lightweight framework. Because it has the advantage of transparent persistence, it is not intrusive, it is a lightweight framework. A constant frame is heavyweight, or lightweight, and is based on its intrusive nature. And NHibernate is a lightweight ORM framework.
(4). Better portability: supports multiple databases and facilitates the migration of databases.
(5). Caching mechanism: provides secondary cache and query cache.
(6). Development efficiency: It is well known that the use of nhibernate can simplify the development of the program, so as to achieve rapid development. As a software company, the key to project management is to control development costs. Because the amount of code written after using NHibernate has been reduced, the cost is reduced relative to the previous project cycle with the "SqlHelper, DAL, BLL" development program.
3.2 Disadvantages
(1). Memory consumption: Direct use of "SqlHelper, DAL, BLL" is undoubtedly the most memory-saving. With NHibernate, there is no doubt that the memory overhead is quite large.
(2). Batch database processing: Because NHibernate is an object-oriented ORM framework, the way the database is processed is for a single object. The increase, deletion and modification of the database are positive for a record. For batch modification, delete data, not suitable for nhiberante. This is also the weakness of all or frameworks, for reasons that I think are in conflict with the caching mechanism.
(3). When using database features more, it is not appropriate to use Nhiberante. SQL statements such as a large number of stored procedures, triggers, and features in a database.
(4). Nhiberante is also not appropriate when table relationships are confusing. NHibernate is only appropriate in environments where tables and tables have a relatively clear relationship. If the foreign key should be established, the foreign key is not established. At this point, the use of Nhiberante not only reduces workload, but increases the workload.
(5). Learning cost: Compared to nhibernate, the use of "SqlHelper, DAL, BLL" Operational database, learning cost is low, and quick to get started. Using NHibernate requires the foundation of a certain OOP (object-oriented programming) and Ood (object-oriented design), which requires a certain amount of input from process-oriented programming to object-oriented programming for a weak-base programmer. In general, it takes about 1 months to learn to go deep into the nhiberante.
Welcome reprint, but need to retain copyright.
NHibernate Initial Introduction Basic configuration (i)