Beruflich Dokumente
Kultur Dokumente
Version: 2.5
Table of Contents
1. ....................................................................................................... .......................................................................5 NCONSTRUCT LITE .......................................................................................................5
1.1. WHO NEEDS NCONSTRUCT LITE..................................................................................................5 1.2. INPUT AND OUTPUT .....................................................................................................................5 1.3. BASIC TERMS ...............................................................................................................................5 1.3.1. Entities...............................................................................................................................5 1.3.2. Views .................................................................................................................................6 1.3.3. Namespaces........................................................................................................................6
2. 3.
.............................................................................................7 ............................................................. SYSTEM REQUIREMENTS .............................................................................................7 START.................................................................................................................8 ................................................................................................................. QUICK START .................................................................................................................8
3.1. CODE GENERATION OUTPUT .....................................................................................................13
4.
2/36
Table of Figures
Figure 1 Quick start: NConstruct Lite .....................................................................................................8 Figure 2 Quick start: Database type selection .........................................................................................8 Figure 3 Quick start: Application settings...............................................................................................9 Figure 4 Quick start: Database server not found.....................................................................................9 Figure 5 Quick start: Namespace definition..........................................................................................10 Figure 6 Quick start: Object creation and management.......................................................................11 Figure 7 Quick start: O/R mapping creation .........................................................................................12 Figure 8 Quick start: Final report ..........................................................................................................12 Figure 9 - NConstruct Lite Toolbar ..........................................................................................................14 Figure 10 Wizard: Welcome page .........................................................................................................15 Figure 11 Wizard: Database type selection page...................................................................................16 Figure 12- Wizard: Application settings ..................................................................................................17 Figure 13 Wizard: General settings section ..........................................................................................17 Figure 14 Wizard: Connection settings page for Microsoft SQL Server 2000, 2005, and 2008 ..........19 Figure 15 Wizard: Connection settings page for Oracle 9i and 10g.....................................................19 Figure 16 - Wizard: Connection settings page for MS Access.................................................................21 Figure 17 Wizard: Namespaces definition page ....................................................................................22 Figure 18 Wizard: Add namespace/group form ....................................................................................22 Figure 19 Wizard: Namespaces definition page ....................................................................................23 Figure 20 Wizard: Objects Management...............................................................................................23 Figure 21 Wizard: Setting up an entity .................................................................................................24 Figure 22 Wizard: Simple propery settings...........................................................................................25 Figure 23 Wizard: Unique ID propery settings.....................................................................................26 Figure 24 Wizard: One-to-many property settings ..............................................................................27 Figure 25 Wizard: NHibernate Component Creation Wizard.............................................................29 Figure 26 Wizard: Formula Field Properties.........................................................................................29 Figure 27 Wizard: Entity class preview.................................................................................................30 Figure 28 Wizard: HBM Preview ..........................................................................................................30 Figure 29 - Wizard: Entity equality..........................................................................................................31 Figure 30 - Wizard: Inheritance - Table per class hierarchy ..................................................................31 Figure 31 Wizard: Base class creation Wizard ......................................................................................32 Figure 32 Wizard: Setting up a View ....................................................................................................32 Figure 33 Wizard: Views and primary keys..........................................................................................33 Figure 34 Wizard: Generated Directory Structure ...............................................................................34 Figure 35 Wizard: Final report ..............................................................................................................35 Figure 36 Wizard: Generated Directory Structure ...............................................................................35
3/36
Table of Tables
Table 1 Wizard: Application Settings....................................................................................................18 Table 2 Wizard: Entitiy Section Settings...............................................................................................25 Table 3 - Wizard: Entitys Simple Propert Settings .................................................................................26 Table 4 - Wizard: Entitys Unique Id Propert Settings............................................................................27 Table 5 Wizard: Entitys One to Many Propert Settings ......................................................................28 Table 6 Wizard: View Section Settings .................................................................................................33 Table 7 Wizard: Views Properties Section Settings .............................................................................33
4/36
1. NConstruct Lite
NConstruct Lite is a powerful wizard-like tool for creating application's data layer source
code for NHibernate object/relational persistence and query framework. NConstruct Lite examines database's metadata and creates NHibernate HBM files, entities' source code, Microsoft Visual Studio project file, and some optional helper files.
1.3.1. Entities
Entities are basically objects mapped from database. They usually represent specific record from a table or a view. They are closely related to NHibernate, which recognizes their properties through specific mapping files (HBM files), generated by NConstruct Lite.
5/36
NHibernate persists these entities to the database through save and load operations.
Properties of these entities (you could also call them entity objects) usually represent column in a database table. Entities can of course be equipped with additional properties.
NConstruct Lite generates these entities during NConstruct Lite code generation process.
Developer can later add additional functionality to them. However, in order to maintain
NHibernate and NConstruct functionality for such entities, their certain properties must be
maintained (as described later in this book). It is also important to note that entities can only be generated for tables and views having primary key.
1.3.2. Views
Views are handled very similarly to tables. In fact Entities based on views do not differ from
the ones based on tables. As it is the case with tables, Entities can also be created only from views having primary key defined. If they do not have primary key defined, it can be defined during code generation process. Whatever is stated in this guide about entities applies also to views, unless noted otherwise.
1.3.3. Namespaces
Entities can be divided into several namespaces. This improves code manageability in case of
great number of tables. The division into namespaces is also performed by developer during code generation process. Entities are usually grouped according to common areas of interest they represent within enterprise application.
6/36
2. System Requirements
In order to start using NConstruct Lite, the following system requirements must be met: Windows 98/NT/2000/XP/Vista/Windows 7 SQL Server 2000, SQL Server 2005, SQL Server 2008, Oracle 9i/10g, or Access 20002003 database .NET Framework 2.0 At least 512M of memory 50 MB of hard disk space
7/36
3. Quick Start
This chapter is intended to give you the first taste of NConstruct Lite, by giving a step-bystep example for creating application using the well known Northwind database. All you need to follow the steps of this chapter is to have Northwind database, which comes shipped with Microsoft SQL Server 2000 or 2005.
After starting the NConstruct Lite, the Welcome page appears (Figure 1). Click Next to proceed to Database type selection page (Figure 2).
8/36
Currently, NConstruct Lite supports Microsoft SQL Server 2000, Microsoft SQL Server 2005, Microsoft SQL Server 2008, Oracle 9i and 10g, and MS Access 2000-2003 databases. Select the type of database server you have installed on your machine and which contains
Northwind example (Microsoft SQL Server 2000, Microsoft SQL Server 2005 or Microsoft SQL Server 2008) and click Next. Code Generation Wizard searches for all database servers of selected type and displays the Aplication settings page (Figure 3).
On the Application settings page select appropriate SQL Server instance (in case you have several instances installed) that contains Northwind example. It might happen the first server which is selected by default in SQL server drop down menu is not accessible. In such case you get the following warning (Figure 4):
If you get such warning, simply press OK button and select a different SQL server instance. In the case you get the message for a server that contains Northwind database, verify if the server is started. From the Database name list select Northwind database. Leave Trusted connection checkbox checked and Naming convention set to Standard and click Next button.
9/36
All generated classes representing tables in database are by default put into namespace named by the database in our case the Northwind namespace. Leave all tables and views in Northwind namespace and click Next to proceed to Object management page (Figure 6).
10/ 10/36
This page enables you to adjust some NHibernate settings. At this point let us not go into details of these settings, instead we will take advantage of the fact that by default NConstruct Lite uses table names and table field names for class names and names of properties within classes.
For a simple quick-start sample we don't need to make any changes in this window, so just simply press Next button to proceed to O-R mapping creation page (Figure 7).
11/ 11/36
O-R mapping creation page enables you to specify where generated files are going to be
created. If you do not agree that generated files are created in a default folder, change that folder.
Click Finish to start generating Northwind project and corresponding source files. This should take only a minute or less on an average machine. Upon successful generation Final report page appears (Figure 8), which informs you where the files are created. Final report page has also some useful shortcuts. To open the folder where the files were generated, click on the first link:
C:\NConstructProjects\Northwind
To open the project file with Microsoft Visual Studio (or other IDE associated with csproj extension), click on the second link:
C:\NConstructProjects\Northwind\Src\Northwind\Northwind.Server.Data\ Northwind.Server.Data.csproj
12/ 12/36
Exceptions folder contains BusinessException.cs file which can handle custom exceptions if someone choose this type of exception handling in the Application settings step.
13/ 13/36
You can restore saved settings by clicking Open button in NConstruct Lite toolbar and selecting file, which contains stored settings.
14/ 14/36
Simply press Next button to move to the Database type selection page.
15/ 15/36
Microsoft SQL Server 2000 (including Microsoft SQL Server Desktop Engine MSDE) Microsoft SQL Server 2005 (including Express Edition) Microsoft SQL Server 2008 (including Express Edition) Oracle 9i and 10g (including Express Edition) MS Access 2000-2003
By selecting database server type, Code Generation Wizard knows where to look for existing databases after you have moved to the next step.
16/ 16/36
Select the appropriate database type and click Next to proceed to the Application settings page.
For every database type there is an Application settings group where you can set the following properties:
17/ 17/36
Editable Yes
Description Name of your application. This name is used in default namespace name, and names of generated projects. You can choose how to handle exceptions for database values which do not allow null values, when null value is to be inserted. Generated one-to-many associations can use a Generic Collections .NET feature. NConstruct can try to auto detect the inheritance relationships in your database. If your database table contains a foreign key that is also a primary key, the entities will be treated as joined subclasses. Some applications don't need views for presenting or even updating/adding/deleting data. If you don't need database views in application's object model check this checkbox. Remove the prefix, if your table name starts with some prefix string you do not want to include it in your entity name. Remove the prefix, if your column name starts with a prefix string you do not want to include in your field and property name. By default, NConstruct will add "Entity" suffix for all entity names (e.g. if the table name is "Orders" the entity class will be named "OrdersEntity"). If you don't want to use this suffix check this check-box.
Yes
Yes
Yes
Exclude database views from OR mapping? Entity prefix to remove Fields/Properties prefix to remove
Yes
Yes
Yes
Yes
18/ 18/36
2000, 2008 Figure 14 Wizard: Connection settings page for Microsoft SQL Server 2000, 2005, and 2008
You can select appropriate Microsoft SQL Server instance from the SQL Server drop down list. Search button enables you to repeat the search for existing instances of Microsoft SQL Server database if another instance was started after the last search. All databases from a selected SQL Server instance are listed in a Database name dropdown list. Select the desired database from this drop down list. You can specify that either trusted windows connection is to be used to connect to selected database by checking out Trusted connection checkbox, or you can specify that Username and Password are to be used. If you chose trusted windows connection, make sure that user running NConstruct Lite has appropriate permissions on selected database. Set Naming convention to Standard. Other options are currently not supported. If you want to check the connection string based on your settings and which will
NConstruct use to connect to your database, click on the Generate button. The connection string is displayed in the Generated connection string text-box.
Enter Server/Database instance name into Server/Database instance name field. If you are using Oracle 10g Express then enter Xe (default) into Server/Database instance name field.
19/ 19/36
Enter valid username and password. Oracle user "hr" with password "hr" is offered by default. Set Naming convention to ORACLE. There are also other settings grouped in the Other settings group. You can define sequence prefix in the Sequence prefix field or sequence suffix in the Sequence suffix field. In Oracle, a sequence is an object that is used to generate a number sequence. This can be useful when you need to create a unique number to act as a primary key. You can create an auto number field by using sequences.
Oracle Data Access Components (ODAC) is among NConstruct system requirements. However, if you don't have ODAC installed you can uncheck ODAC 10.2.0.2.21 installed check-box and NConstruct will try to establish a connection using an alternative approach.
For more detailed explanation about setting up an Oracle connection, please refer to an online article at: http://www.nconstruct.com/Oracle-And-NConstruct.aspx
20/ 20/36
When using Microsoft Access database, you first have to specify mdb file containing the database. Click Search button and browse to your mdb file. Enter username and password, if your database is protected by password, otherwise leave the username and password fields blank. Set Naming convention to Standard or ORACLE, depending on your convention preferences.
Value of the<ApplicationName> part of the namespace is obtained from Application name field on Connection settings page, which is by default set to database name (see 4.1.4 Application Settings).
1
21/ 21/36
You can release tables and/or views from namespace by checking the checkboxes in front of their names in Namespaces in the application section and then clicking Release DB objects from namespace button. You can add tables and/or views to the currently selected namespace by checking checkboxes in front of their names in Available database objects section and then clicking Move DB objects to namespace button. NConstruct Lite will, along with selected tables and/or views, move also all tables and views that reference selected tables and/or views. You can create new namespaces by clicking Create namespace button. An Add namespace/group form will open enabling you to add one or more namespaces. For each namespace you must enter Namespace name and Caption.
The actual name of a namespace will be composed by adding a prefix "<applicationname>.Server.Data." to the name you have entered. You can add multiple namespaces by clicking Add and Continue button. You can also rename a namespace by selecting it in the Namespaces in the application section and then clicking the Rename button. And finally, you can delete a namespace by selecting it in Namespaces section and then clicking Remove namespace button. Only namespaces having no tables and views can be deleted.
22/ 22/36
After having successfully set up namespaces, click Next button to proceed to Objects management page.
23/ 23/36
This fine-tuning enables you to set up properties of generated views and entities, which represent the data from a database, to change some NHibernate settings, and even enables you to add new calculated properties (formula properties) to any entity. The following topics describe in detail how to fine-tune your enterprise application.
Entities Entities
To set up properties and attributes of an entity2, select the desired entity in the objects tree (Figure 21).
All settings already have assigned default values, which are obtained by NConstruct Lite from database properties of tables to which entities refer. There are two sections of settings Entity section and Properties section.
24/ 24/36
In Entity section you can set up settings referring to the entire class representing an entity. The following settings can be applied:
Setting Class name Editable Yes Description The name of a class representing an entity mapped to a table.
NHibernate Optional3:
Select before update? Yes Should hibernate perform update only if object is actually modified? Should insert contain only not null columns? Immutable classes may not be updated or deleted by the application. This allows NHibernate to make some minor performance optimizations. Enable or disable lazy fetching. Should update contain only changed columns? Batch size for fetching instances of this class by identifier. SQL condition to be used when retrieving objects of this class. Table 2 Wizard: Entitiy Section Settings
Yes Yes
In the Properties section you can set up settings referring to properties of the selected entity. You can edit settings of the individual property through the Edit property form by doubleclicking on the property, or by selecting the property and then clicking the Edit button.
Edit property form can be used for editing simple properties (Figure 22), unique ID
properties (Figure 23) and one-to-many properties (Figure 24).
25/ 25/36
Setting
Field name
Editable
No
Description
Name of a property. It is the same as name of a corresponding field in a database. Type of a property defined on the bases of database field type.
Type
No
Enumeration type
Yes
NHibernate optional4:
Generate property Yes Specifies that the value of this property is generated by the database. Default value is "Never". Default value when entity is inserted into database, which is used if property is not set. Does a field represent a foreign key? Is property used during update? All mapped properties are. If true, property's value must not be null before saving to a database. Appropriate validation with selected exception type (set in general settings page) will be generated. Is property used during insert? All mapped properties are.
Default value
No
No No Yes
Insert?
No
ID properties have different properties which can be seen or altered through similar edit form.
26/ 26/36
Setting
Field name
Editable
No
Description
Name of a property. It is the same as name of a corresponding field in a database. Type of a property defined on the bases of database field type. Does a field represent a primary key? Editable only if primary key is not defined for underlying table. If editable, a composite key is created, if you select several fields. Does a field represent a foreign key?
Type
No
Primary key
Yes/No
Foreign key
No
One-to-many or many-to-many properties have different properties which can be seen or altered through similar edit form.
Setting
Field name
Editable
No
Description
Name of a property. It is the same as name of a corresponding field in a database. Type of a property defined on the bases of database field type. NConstruct auto detects if set's entity extends another class and displays it in this combo-box. You can select base class as the set's entity type.
Type
No
Yes
27/ 27/36
Move to base/subclass
Yes
If class A, which has this 1-M set, extends class B you can move set definition to the class B declaration in the HBM file by selecting class B in this combo-box. NConstruct tries to select the right type of an association during the database examination process. If you want to choose different type of an association, change the value in this check-box. A bidirectional association allows navigation from both "ends" of the association. NHibernate supports two kinds of bidirectional association: one-to-many and many-to-many. Check this check-box if you want to use bidirectional association.
Is bidirectional association?
Static fetch strategy: Fetch Batch size Yes Yes Fetch strategy to be used for fetching data from database. Batch size which refers to the number of collections.
NHibernate optional5:
Where SQL condition Yes SQL condition to be used when retrieving objects of this class. Cascade action applicable for sets.
Yes
If you wish to use NHibernate managed properties like Money or Date interval on the bases of existing properties, click Manage component property button in Properties section.
28/ 28/36
NHibernate Component Creation Wizard guides you through component property creation
(Figure 25).
You can also create NHibernate formula property by clicking Manage Formula Field Property button (Figure 26).
In the Formula Field Properties form there is very useful button, Execute test. Pressing this button, you can run quick SQL syntax check to test your NHibernate formula.
29/ 29/36
After you have set all entitys settings and its properties settings, you can preview how an entity class will be generated by clicking Class preview button (Figure 27).
You can also preview how a HBM mapping file will be generated by clicking HBM preview button.
30/ 30/36
You can set the equality condition for an entity by clicking Equality button and then selecting fields to be compared when determining equality of two entities (Figure 29).
Another feature supported by NConstruct Lite is support for creation of base classes. You can create base classes by the help of Table per class hierarchy wizard which leads you through base class creation.
When base class is created it usually consist of primary key fields and non nullable fields. Besides base class also subclass appears, which consists of remaining (nullable and non nullable) columns.
31/ 31/36
Views
Views are very similar to entities. The main difference is that views do not contain foreign keys and that they cannot always be updated and/or saved.
There are again two sections in view settings View section and Properties section.
32/ 32/36
In the View section you can set the following settings of a view:
Setting
Class name
Editable
No
Description
The name of a class representing a view mapped to a database view. Table 6 Wizard: View Section Settings
In the Properties section of view settings is similar you can set the settings for individual properties of a view:
Setting
Field name
Editable
No
Description
Name of a property. It is the same as name of a corresponding field in a database. Type of a property defined on the basis of database field type. Default value when view is created (when class representing a view is instantiated).
No Yes
33/ 33/36
After specifying location of generated files, click Finish button to start the code generation process.
34/ 34/36
To open the project file with Microsoft Visual Studio (or other IDE associated with sln extension), click on the second link:
C:\NConstructProjects\Northwind\Src\Northwind\NorthwindServer.sln
Directory structure of generated code is formed in the way to best suite usual development environment and can easily be put under source control (e.g. Subversion). Figure 36 shows generated directory structure.
35/ 35/36
NConstruct versions which generate Windows and Web n-tier applications from your
databases with just a little more effort.
36/ 36/36