Nhib.pdf beginner's Guide (5): method 1 for ing models to databases

Source: Internet
Author: User
Tags in domain
Ing type

When we use nhib.pdf as our ORM framework, there are four main ing types:

    1. XML-based ing.
    2. Feature-based ing.
    3. Fluent ing.
    4. The agreed-based ing is also called automatic ing.

In the next three articlesArticleTo remove the ing types other than "Property ing.

Fluent ing

All the operations in this article need to be done in the nhib.pdf beginner's Guide (3): create a modelCodeClick here to download the code.

To map our model to the underlying database using FLUENT, we need to add two references: Fluent nhib.pdf and nhib.pdf.ProgramSet can be found in the nhib.pdf beginner's Guide (2): The LIB file created in a complete example.

Ing Class

To map entities to database tables, you must add a class that inherits from classmap (defined in fluentnhibernate. dll) to our project. For example, to map the product object, we should define a ing Class As shown in the following code:

Public classProductmap:Classmap<Product> {PublicProductmap (){// Define the ing information here}}

The ing information must be defined in the default constructor of the ing Class. Note that classmap <t> requires the ing class as a generic parameter. In this example, it is the product class.

Object level settings

To make the database understand the domain model, we need to specify the minimum details. However, we have the ability to specify more details for extra flexibility. As an example, if the ing table name is different from the object class name, we can define the table name. You can use the following code to define:

 
PublicProductmap (){// Define the ing information hereTable ("Tbl_product");}

If a specific table resides in anotherSchema)You can also use the following code to specify the corresponding architecture:

PublicProductmap (){// Define the ing information here // table ("tbl_product ");Schema ("Orderingsystem");}

By default, the fluent nhib.pdf configuration object isLazy loaded). If you do not like this, you can use the following code to change the default behavior:

 
PublicProductmap (){// Define the ing information // table ("tbl_product"); // Schema ("orderingsystem ");Not. lazyload ();}

More information can be specified, but not in the scope of this introduction.

ID column

Usually we need to map the Object ID first. This requires the ID method. This method has many options and uses the simplest form, as shown in the following code:

 
PublicProductmap () {ID (x => X. ID). generatedby. HiLo ("1000");}

Using the above Code, we will tell Nhibernate that the ID attribute of the product object should be used as the primary key ing, and the new ID will be automatically generated by the Hilo generator of Nhibernate. As long as our field in the database is ID, everything works properly. However, what if the name is ID or product_id? The answer is that it won't be processed at all. In this case, you must add an optional column parameter to specify the column Name:

 
ID (x => X. ID,"Product_id"). Generatedby. HiLo ("1000");

In addition, you can also write the following form:

 
ID (x => X. ID). Column ("Product_id"). Generatedby. HiLo ("1000");

Id also has an optional attribute that is frequently used: unsavedvalue. It specifies the value to be returned before a new object is persisted to the database. The following code:

 
ID (x => X. ID,"Product_id"). Generatedby. HiLo ("1000"). Unsavedvalue (-1 );

Note that the specified ID type is not displayed. Fluent nhib.pdf can obtain its type through reflection. In short, we should avoid specifying redundant information, because there is no need to make your code messy.

Attribute

Map simple value Attributes. We use the map method. A simple value attribute is an attribute that contains one of the basic. Net types, such as int, float, double, decimal, bool, string, or datetime. Such attributes are mapped to database columns. For example, the name attribute of the product object uses the simplest form, as shown in the following code:

 
Map (x => X. Name );

The code above tells nhib.pdf to map the name attribute to columns with the same name in the database. In SQL Server, the database column type is nvarchar, and the maximum length of the database column is 255. In addition, the column can be empty.

If the maximum length of a column is 50, it is not empty, as shown in the following code:

 
Map (x => X. Name). Length (50). Not. nullable ();

If the database column name and attribute name are different, you can use the following code to specify them, which is the same as the ID:

 
Map (x => X. Name,"Product_name"). Length (50). Not. nullable ();

Sometimes, Nhibernate does not automatically understand how we map a property. We must specify our intent. For example, we want to map an attribute of the enumeration type. Assume that the object product has a productype property of the producttypes type, and producttypes is an enumeration, as shown below:

 
Public EnumProducttypes{Producttypea, producttypeb}

Then we can add a customtype method to map this property, as shown below:

 
Map (x => X. producttype). customtype <Producttypes> ();

In this case, because the type behind Enum is INTEGER (INT by default), for SQL Server, database fields will be int type. Nhib.pdf automatically converts the enum value of the domain model to the int type number in the database.

Another commonly used method is to map bool attributes to Char (1) columns in the database. True indicates 'y', and false indicates 'n '. Nhib defines a special ing called yesno. For example, we can use the following code to map the discontinued attribute of the product entity:

 
Map (x => X. discontinued). customtype ("Yesno");

Fluent nhib.pdf is very complete and provides great flexibility for us to map simple value attributes. In this section, we only discuss the most important and common settings.

Reference

Ing references the attributes of another object, such as the category attribute of the product object, you can use the following code to easily map this relationship:

 
References (x => X. Category );

This type of ing represents the "many-to-one" relationship, which is often used in Domain Models.

In most cases, you can use the following code to forcibly define this reference:

References (x => X. Category). Not. nullable ();

If this parameter is not specified, the foreign key name from the product table to the category table is category_id. By default, the attribute name and ID are combined with underscores. Of course, you can use the following code to modify the foreign key name:

 
References (x => X. Category). Not. nullable (). foreignkey ("Productid");

The last setting is to specify the reference as unique. The following code:

 
References (x => X. Category). Not. nullable (). Unique ();
Set

Let's talk about the one-to-many relationship. Let's take a look at an example of a set of orders containing line item objects. The line item set ing code is as follows:

 
PublicOrdermap () {hasmany (x => X. lineitems );}

Generally, the hasmany ing is defined as inverse. In the one-to-multiple relationship between order and line items, if the set is not marked as inverse, Nhibernate performs an additional update operation. When it is marked as inverse, nhibse will first persist the entity that owns the Set (Order), and then persist the entity (line items) in the set ). This avoids additional update operations. The following code:

Hasmany (x => X. lineitems). Inverse ();

Another important configuration is the cascade operation of nhib.pdf. In most cases, this configuration is used, as shown in the following code:

 
Haswon (x => X. lineitems). Inverse (). Cascade. alldeleteorphan ();
Map multiple-to-multiple relationships

We can also map many-to-many relationships. In the domain model, the book object contains a set of author sub-objects, and the author object also contains a set of book sub-objects. The following code defines the ing:

 
Hasmany (x => X. Author );

The following code specifies the name of the intermediate table:

 
Haswon (x => X. Author). Table ("Bookauthor");
Ing Value Object

In nhib.pdf beginner's Guide (3): create a model, we learned that value objects are a very important concept of a domain model. Therefore, we need to know how to map attributes that contain value objects as types. Take the name attribute of the customer object as an example. First, we may want to map the value object itself. We define a namemap class that inherits from componentmap <Name>, as shown in the following code:

Public classNamemap:Componentmap<Name> {...}

Then, add the detailed information of the ing to the default constructor of this class, as shown in the following code:

 
PublicNamemap () {map (x => X. lastname ). not. nullable (). length (50); Map (x => X. middlename ). length (50); Map (x => X. firstname ). not. nullable (). length (50 );}

Once the value object is mapped, we can define the customer ing of the name attribute of the customer object. Add the following code to the mermap constructor:

 
Component (x => X. customername );

After finishing the basic knowledge required for fluent ing, We will map the model below.

Practical Time-map our model

1. Download the source code and add references as required at the beginning of this article.

2. in nhib.pdf beginner's Guide (3): create a model using a guid ID. In this article, we want to use an int ID to open the entity <t> class, modify the property ID:

Public intId {Get;Private set;}

3. Replace guid. empty in the base class of entity <t> with 0 (zero.

4. Create a new folder mappings in the project.

5. Map the employee object and create a new employeemap class inherited from classmap <employee>. The Code is as follows:

 
Public classEmployeemap:Classmap<Employee> {PublicEmployeemap () {ID (x => X. ID). generatedby. HiLo ("100"); Component (x => X. Name );}}

6. Map the name value object, create a namemap class, and inherit from componentmap <Name>. The Code is as follows:

Public classNamemap:Componentmap<Name> {PublicNamemap () {map (x => X. lastname ). not. nullable (). length (100); Map (x => X. middlename ). length (100); Map (x => X. firstname ). not. nullable (). length (100 );}}

7. Map the customer object and create a customermap class inherited from classmap <customer>. The Code is as follows:

Public classCustomermap:Classmap<Customer> {PublicCustomermap () {ID (x => X. ID). generatedby. HiLo ("100"); Component (x => X. customername); Map (x => X. customeridentifier ). not. nullable (). length (50); component (x => X. address); hasmany (x => X. orders ). access. camelcasefield (). inverse (). cascade. alldeleteorphan ();}}

Here, we need to explain that access. camelcasefield () is used to allow nhibders to directly access the private Field orders of the customer class (). Cascade. alldeleteorphan () tells Nhibernate to automatically cascade all insert, update, or delete operations from the customer to its orders.

8. The ing value object address creates an addressmap class inherited from componentmap <address>. The Code is as follows:

Public classAddressmap:Componentmap<Address> {PublicAddressmap () {map (x => X. line1 ). not. nullable (). length (50); Map (x => X. line2 ). length (50); Map (x => X. zipcode ). not. nullable (). length (10); Map (x => X. city ). not. nullable (). length (50); Map (x => X. state ). not. nullable (). length (50 );}}

9. Map the order object and create an ordermap class inherited from classmap <order>. The Code is as follows:

Public classOrdermap:Classmap<Order> {PublicOrdermap () {ID (x => X. ID). generatedby. HiLo ("100"); Map (x => X. orderdate ). not. nullable (); Map (x => X. ordertotal ). not. nullable (); References (x => X. customer ). not. nullable (); References (x => X. employee ). not. nullable (); haslab( x => X. lineitems ). inverse (). cascade. alldeleteorphan ();}}

10. Map the lineitem object and create a new lineitem class inherited from classmap <lineitem>. The Code is as follows:

Public classLineitemmap:Classmap<Lineitem> {PublicLineitemmap () {ID (x => X. ID). generatedby. HiLo ("100"); References (x => X. order ). not. nullable (); References (x => X. product ). not. nullable (); Map (x => X. quantity ). not. nullable (); Map (x => X. unitprice ). not. nullable (); Map (x => X. discount ). not. nullable ();}}

11. Finally, map the product entity and create a productmap class inherited from classmap <product>. The Code is as follows:

Public classProductmap:Classmap<Product> {PublicProductmap () {ID (x => X. ID). generatedby. HiLo ("100"); Map (x => X. name ). not. nullable (). length (50); Map (x => X. description ). length (4000); Map (x => X. unitprice ). not. nullable (); Map (x => X. reorderlevel ). not. nullable (); Map (x => X. discontinued );}}

So far, we have completed model ing. Now nhib.pdf can understand how to organize domain data in the database.

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.