DOCX

NHibernate I got easier

By Billy Ruiz,2015-05-04 15:43
15 views 0
NHibernate I got easier

    preface

    This article from the I try to learn to use Nhiberbnate frustration.I found that if I got all the materials are not so blurred or too detailed introduced.What I need is a simple tutorial directly, can let me for I got familiar as soon as possible.I have never found.Fortunately, this article will meet these requirements.

    The article some long, but I encourage you to read in your way.I got is a complex procedure, it is a long and tortuous the learning process.This article will provide you flattened twists and turns, from a few days or weeks to a few hours.

    The problem

    I got the purpose is to solve the problem of a well-known, object persistent code in the bottleneck problem in the process of development.Many articles showed that a quarter to a third of the program code is about object persistence, reads data from a database, and write the data back to the database.The code is repetitive, time-consuming, and there are many trivial code to write.

    For this problem, there are a lot of solutions are available.Code generation can generate data access code in a few seconds.But if the business model changes, the code needs to be generated.Object relational mapping (ORMs) the use of a new way, as I got.They manage data access more transparent, provides many simple API, you can use a line of code to implement load and save the entire object.

    Introduction to got

    I got is a persistence engine framework.It is loaded from the database business object, and the change of these objects will update in the database.Can be seen from the article above,

    it can only use one or two lines of code to achieve the business object of the loading and saving.

    I got using a mapping file to guide to business objects from the database data conversion.Another way, you can use the features and properties of a class to replace the mapping file.In order to make things as simple as possible, in this article we will use a mapping file, instead of using the class features.In addition, the mapping file can clearly separate the business logic and persistence code.

    Ok, we just need to add a few lines of code in the program, and a persistent class to each mapping file, and I got to take care of all database operations.I don't know how many development using got will save us time.

    Remember that in the.net environment, I got is not the only ORM framework.There are many commercial and open source products can provide such service.I got is one of the most popular, mainly because of his genetic self-improvement large Hibernate, a very popular ORM framework in Java environment.In addition, Microsoft also provides the ADO.NET Entity Framework "", to provide the ORM services.However, this product has been delayed and has been a long time didn't released.

    Install got

    Use I got the first step is to download I got and Log4Net (an open-source logging application, I got to use it to record the error and warning), I got contains the latest version of the Log4Net, you can download the entire Log4Net install package, here is the download address:

    ; NHibernate

    ; Log4Net

    I got Log4Net is not, but its automatic logging during debugging function is very useful.

    now

    In this article, I'll use a simple example program, rather than explain how to use I got for data access.This is a console application, by eliminating the UI code make application easier.This program will create a number of business objects, I got to use to persist to them, then they read from the database.

    In order to program to run, you need to do several things:

    ; Add got to Chen and Log4Net assembly references

    ; To add database program

    ; Change the database connection string

    The example application reference I got and Log4Net.These applications should be your machine recognition, if you got and log4net install in the default directory.If these references are not recognized, you can use I got respectively. The DLL and Log4Net. DLL to replace the reference position.The DLL file can be found I got in the installation directory.

    This sample program is according to the SQL Server Express 2005 to configuration, the database file (NhibernateSimpleDemo. MDF and NhibernateSimpleDemo. LDF) have been packaged in the zip file.You can will be the SQL Server database on your machine.

    Finally, the database connection string configuration in the App. The config file, you are using a SQL Server database by default.You can according to their own versions of SQL Server to the machine, to change the database connection string.

    The business model

    Here there are two ways to use got to create the application.The first is "a data-centric" approach, it begins from data model and create a business object.The second is "object centered" method, from the business model and create a database to persist the model.This sample program using object centered approach.

    The business model in the example here: