Professional Documents
Culture Documents
An Example of Using NHibernate With
An Example of Using NHibernate With
NET
Gemunu R. Wickremasinghe
Change Log Version 0.5 1.0 Date 16 Aug 2005 th 17 Aug 2005
th
Initials GW GW
Remarks Draft Added introduction section (as suggested by CM). Expanded n-Tier Architecture to include Data Access Layer. Included Notes under sections 2.1.4.1.2 and 2.1.4.1.4 to suggest alternate approaches to replace XML mapping documents General fresh up Addressing review concerns Added whitepaper content to introduction Finishing touch
th
GW GW GW GW
Table of Contents 1 Introduction ....................................................................................................................................................... 3 1.1 What is NHibernate?................................................................................................................................... 3 1.2 What problems does it solve?..................................................................................................................... 3 1.3 Other similar frameworks ............................................................................................................................ 3 1.4 History of NHibernate.................................................................................................................................. 3 Example Application......................................................................................................................................... 4 User Interface .................................................................................................................................................... 4 3.1 Main Window .............................................................................................................................................. 4 3.1.1 Actions ................................................................................................................................................ 4 3.2 Orders form................................................................................................................................................. 5 3.2.1 Field Definitions .................................................................................................................................. 5 3.2.2 Actions ................................................................................................................................................ 5 Architecture and design................................................................................................................................... 5 4.1 Application Architecture .............................................................................................................................. 6 4.1.1 Database............................................................................................................................................. 6 4.1.2 NHibernate .......................................................................................................................................... 7 4.1.3 log4net ................................................................................................................................................ 7 4.1.4 Data Access Layer .............................................................................................................................. 7 4.1.4.1 DataAccessLayer.Beans namespace............................................................................................. 7 4.1.4.1.1 Customer class......................................................................................................................... 7 4.1.4.1.2 The XML mapping for Customer class (Customer.hbm.xml) ................................................... 7 4.1.4.1.3 Order class ............................................................................................................................... 8 4.1.4.1.4 The XML mapping for Order class (Order.hbm.xml) ................................................................ 8 4.1.4.2 DataAccessLayer.Services namespace ......................................................................................... 9 4.1.4.2.1 DataService class..................................................................................................................... 9 4.1.4.3 BusinessLogicLayer.Services namespace ..................................................................................... 9 4.1.4.3.1 OrderService class ................................................................................................................... 9 4.1.5 UI layer................................................................................................................................................ 9
2 3
Page 2 of 9
Introduction
The need to deal with relational data is something that arises almost always with business applications. The implementation of data access layer has been drawing attention of everyone so often due to the above reason. It is not the mere implementation of the data access layer that cost a lot in a software project but the direct and side effects of wrong approaches when doing so. Finding the most cost effective approach for handling data access requirements have been the focus of many researchers including some open source groups. Object relational mapping (O/R mapping) is recognized as a concept that can solve most of the well known issues faced by developers related to data access. This concept links object oriented programming with relational data models. O/R mapping can establish a bidirectional mapping between the data in a relational database and Objects in code.
Page 3 of 9
Example Application
In order to help the growing popularity of the usage of NHibernate framework, I decided to go ahead and implement a simple real world application while trying to focus a lot on an n-Tier approach that plays a vital role in enterprise application developments and maintenance. This sample application is implemented to pick a customer from an available collection in a backend data table and then retrieves the orders placed by any selected customer in a child data table. This application also allows inserting new orders to the current collection under the same customer and editing any order through the UI. The application is not made extra complex in order to leave room for the easy grasp of the concepts covered. Following is how the NHibernate framework is used inside the application to get the job done. The usage of NHibernate framework is cleanly restricted to MiddleTiers.dll assembly that implements both Data access layer and the Business logic layer of the sample. The NHibernate framework initialization code is implemented as the sole representative of data access logic which is the DataService class. That logic is called only once for the application lifetime to initialize the open source frameworks as declared in application configuration (app.config) file under <nhibernate></nhibernate> and <log4net></ log4net > sections. The business logic layer simply uses this class and appropriate persistence classes available in DataAccessLayer.Beans sub namespace to support the requirements from the UI. The UI layer does not worry about the internal implementation details of any API and simply invokes the relevant methods of classes available in BusinessLogicLayer.Services namespace to get the expected results out. The rest of this document explains the UI, architecture and design of the sample in more details
User Interface
3.1.1 Actions
Action Elements Select customers combo box View Orders Cancel Action Click on the drop down button will show the list of customers available so that user can scroll down to select the desired customer. This will invoke the Orders window (modal) populated with orders corresponding to the selected customer (Refer section 2.2) 1 Click on the Cancel button will quit the application
Page 4 of 9
3.2.2 Actions
Action Elements Insert an Order Update Cancel Action This will append a dummy order record to the current list of orders from the customer and commit changes to the backend User can do changes to the grid cells and click on this button to commit changes to the backend. Close the Orders form allowing access to Main form
Page 5 of 9
Business Logic Layer OrderService (BusinessLogicLayer.Services) log4net Data Access Layer Persistent Objects (DataAccessLayer.Beans) DataService (DataAccessLayer.Services)
Database
4.1.1 Database
SQL 2000 server installation includes the installation of a pre-populated DB called Northwind. The following two tables from this DB is used as the data model for the application
Page 6 of 9
4.1.2 NHibernate
This is the Open source framework used for the Object Relational Mapping and focus of this sample application. The term object/relational mapping (ORM) refers to the technique of mapping a data representation from an object model to a relational data model with a SQL-based schema.
4.1.3 log4net
This is an Open source framework for extending the error logging implementations of applications to output log statements to a variety of output targets without adding substantial performance overheads. The official link for this framework is http://www.neoworks.com/products/free/log4net/ . N.B. Both this framework and NHibernate are to be configured through different sections in configuration file. Once these frameworks are embedded in the middle tiers, they should be able to be configured based on the requirements of the users of the middle tiers. through the app.config file of the application.
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" namespace="DataAccessLayer.Beans" assembly="MiddleTiers"> <class name="Customer" table="Customers"> <id name="CustomerID"> <generator class="assigned" /> </id> <property name="CompanyName"/> </class> </hibernate-mapping> N.B. If you feel like the creation and maintenance of the XML mapping document is a hassle, NHibernate.Mapping.Attributes available in NHibernateContrib has a very good alternate for getting rid of the
Page 7 of 9
above file altogether through attribute programming. The details and instructions for implementation is available at the link published by me at http://sourceforge.net/forum/message.php?msg_id=3233643 4.1.4.1.3 Order class Order private int orderId; private string customerId; private int employeeId; private DateTime orderDate; private DateTime requiredDate; private DateTime shippedDate; private int shipVia; private double freight; private string shipName, shipAddress, shipCity, shipRegion, shipPostalCode, shipCountry; public int OrderID public string CustomerID public int EmployeeID public DateTime OrderDate public DateTime RequiredDate public DateTime ShippedDate public int ShipVia public double Freight public string ShipName public string ShipAddress public string ShipCity public string ShipRegion public string ShipPostalCode public string ShipCountry Figure 6: Order persistent class 4.1.4.1.4 The XML mapping for Order class (Order.hbm.xml)
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" namespace="DataAccessLayer.Beans" assembly="MiddleTiers"> <class name="Order" table="Orders"> <id name="OrderID" > <generator class="identity" /></id> <property name="CustomerID" /> <property name="EmployeeID"/> <property name="OrderDate" /> <property name="RequiredDate"/> <property name="ShippedDate" /> <property name="ShipVia"/> <property name="Freight"/> <property name="ShipName"/> <property name="ShipAddress"/> <property name="ShipCity"/> <property name="ShipRegion"/> <property name="ShipPostalCode"/> <property name="ShipCountry"/> </class> </hibernate-mapping> N.B. XML mapping can be avoided in the same way as described under the Customer class description.
Page 8 of 9
4.1.5 UI layer
This layer implements only the UI forms in addition to app.config file that declares the NHibernate and log4net framework configurations. This assembly only refers to the middle tier and does not know usage details of NHibernate at all. The log4net implementation is also restricted to the middle tier for now. However the decision of extending it to UI layer to be decided based on the application requirements. The beauty of this approach could really be seen if there is a need to implement a Web based solution that provides the same features. Then it would only be the Web forms that need creation and binding to middle tiers. The entire middle tier could be used seamlessly to cater to the Web UI just like it did for Windows UI. This eliminates the need for rewriting a whole new application with lot of code duplication just to support a different UI scenario.
Page 9 of 9