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

Source: Internet
Author: User
Tags automap
Use fluent nhib for automatic ing

To use fluent nhib for automatic ing, you must first put the ing object in a namespace, which makes it easier to notify automapper of the objects and value objects included in the ing. We recommend that you create a domain folder in your project to put the objects and value objects to be mapped in this folder.

Because objects and value objects are implicitly mapped, conventions and exceptions must be defined for automapping. We implement a configuration class inherited from defaultautomappingconfiguration.

Suppose we have put all the objects and value objects in the same namespace. This namespace only contains the domain class, we can do the following:CodeDefine the configuration class:

 
Public classOrderingsystemconfiguration:Defaultautomappingconfiguration{Public override boolShouldmap (TypeType ){ReturnType. namespace =Typeof(Customer). Namespace ;}}

The code above tells automapping to map only the classes with the same namespace as the customer object.

Before automapper works, we need to tell it more things. We must tell it who is a value object in the system. The iscomponent method can be rewritten in the configuration class, as shown in the following code:

 
Public override boolIscomponent (TypeType ){VaRComponenttypes =New[] {Typeof(Name),Typeof(Address)};ReturnComponenttypes. Contains (type );}

The Configuration System uses automatic ing. We use the following code:

VaR CFG =NewOrderingsystemconfiguration(); Var configuration =Fluently. Configure (). Database (/* Database Config */). Mappings (M => M. automappings. Add (AutoMap. Assemblyof <Employee> (CFG). buildconfiguration ();
Actual time-use automatic ing

In this example, we want to define a basic domain model, map our model using the automatic ing provided by FLUENT nhib.pdf, and then let nhib.pdf generate a database architecture for US based on the ing:

1. Create a console application in VS with the name automappingsample.

2. Add nhib.pdf. dll and fluentnhib.pdf.ProgramSet to the project.

3. Create a new domain folder in the project.

4. Add the following classes to the domain folder respectively:

Customer class

 
NamespaceAutomappingsample. Domain {Public classCustomer{Public Virtual intId {Get;Set;}Public Virtual stringCustomername {Get;Set;}}}

Lineitem class

NamespaceAutomappingsample. Domain {Public classLineitem{Public Virtual intId {Get;Set;}Public Virtual intQuantity {Get;Set;}Public Virtual decimalUnitprice {Get;Set;}Public Virtual stringProductcode {Get;Set;}}}

Order class

  namespace  automappingsample. domain { public class   order  { Public Virtual int  ID { Get ;  set ;}< span style =" color: blue; "> Public Virtual   datetime  orderdate { Get ;  set ;}< span style =" color: blue; "> Public Virtual   customer  customer { Get ;  set ;}< span style =" color: blue; "> Public Virtual   ilist   lineitem  lineitems { Get ;  set ;}}

5. Add an orderingsystemconfiguration class to the project and inherit from defaultautomappingconfiguration.

6. Override the shouldmap method, as shown in the following code:

 
NamespaceAutomappingsample {Public classOrderingsystemconfiguration:Defaultautomappingconfiguration{Public override boolShouldmap (TypeType ){ReturnType. namespace =Typeof(Customer). Namespace ;}}}

7. Add the following using statement to the program class:

UsingFluentnhib.pdf. cfg;UsingFluentnhib.pdf. cfg. dB;UsingFluentnhibing. automapping;UsingAutomappingsample. domain;UsingNhib.pdf. tool. hbm2ddl;

8. Add the following code to the main method of the program class to create the Nhibernate configuration. The configuration object is created using the configuration API of fluent nhib.pdf for automatic ing.

VaRCFG =NewOrderingsystemconfiguration();VaRConfiguration =Fluently. Configure (). Database (Mssqlconfiguration. Mssql2008). mappings (M => M. automappings. Add (AutoMap. Assemblyof <Customer> (CFG). buildconfiguration ();

Note that it is unnecessary to define the database string for this operation, because we do not access the database, just tell nhib.pdf which type of database.

9. Add the following code to create and display the SQL script on the console:

VaRExporter =NewSchemaexport(Configuration); exporter. Execute (True,False,False);

10. Add the following code to prevent the program from exiting without confirmation:

 
Console. Writeline ("Press enter to exit :");Console. Readline ();

11. Run the program. The console output is as follows:

Automatic coning using conform

Conform is part of nhib?contributions and can be downloaded from http://code.google.com/p/codeconform/downloads/list.Make sure that the downloaded version is compatible with your Nhibernate version.

Like the automapper of fluent Nhibernate, conform implicitly defines the ing of Domain Models Using conventions. These conventions can be rewritten or defined.

Actual Time-Use the conform ing model

In this example, we create a simple domain model and use conform to create an implicit ing for us. Then we use nhihei's schema exporter to create an SQL script to create a database architecture compatible with our model.

1. Open Vs and create a console application project named conformsample.

2. Add a reference to the nhib.pdf. dll and conform. dll assembly to the project. My nhib.pdf version is 3.1, so conform is compatible with conform 1.0.1.5 of 3.1.

3. Create a domain folder in the project.

4. Add the customer, order, and lineitem classes in the previous example to the domain folder.

Customer class

NamespaceConformsample. Domain {Public classCustomer{Public Virtual intId {Get;Set;}Public Virtual stringCustomername {Get;Set;}}}

Order class

  namespace  conformsample. domain { public class   order  { Public Virtual int  ID { Get ;  set ;}< span style =" color: blue; "> Public Virtual   datetime  orderdate { Get ;  set ;}< span style =" color: blue; "> Public Virtual   customer  customer { Get ;  set ;}< span style =" color: blue; "> Public Virtual   ilist   lineitem  lineitems { Get ;  set ;}}

Lineitem class

 
NamespaceConformsample. Domain {Public classLineitem{Public Virtual intId {Get;Set;}Public Virtual intQuantity {Get;Set;}Public Virtual decimalUnitprice {Get;Set;}Public Virtual stringProductcode {Get;Set;}}}

5. Add the following using statement to the program class:

UsingConformsample. domain;UsingConform;UsingConform. NH;UsingNhib.pdf. cfg;UsingNhib.pdf. cfg. loquacious;UsingNhib.pdf. dialect;UsingNhib.pdf. driver;UsingNhib.pdf. tool. hbm2ddl;

6. In the main method of the program class, use the fluent configuration API provided by nhib.pdf to create a Nhibernate configuration object:

 
VaRConfiguration =NewConfiguration(); Configuration. databaseintegration (DB => {dB. dialect <Mssql2008dialect> (); DB. Driver <Sqlclientdriver> ();});

7. Add the following code to determine the ing type:

VaRTypes =Typeof(Customer). Assembly. gettypes (). Where (t => T. namespace =Typeof(Customer). Namespace );

8. Create an instance of the objectrelationalmapper type, as shown in the following code:

 
VaROrm =NewObjectrelationalmapper();

9. Tell the er that a table should be created for each type, as shown below:

 
Orm. tableperclass (types );

10. Create mappings and add them to the configuration object, as shown in the following code:

VaRMapper =NewMapper(ORM );VaRHbmmappings = Mapper. compilemappingfor (types); configuration. adddeserializedmapping (hbmmappings,"Mydomain");

11. Create a schema generation script, as shown in the following code:

 
VaRExporter =NewSchemaexport(Configuration); exporter. Execute (True,False,False);

12. Similarly, add the following code to prevent the program from exiting without confirmation:

 
Console. Write ("Press enter to exit :");Console. Readline ();

13. Run the program, as shown in:

Without coding and a small amount of configuration, the system creates an implicit ing for our domain model and exports a script that can create a database architecture. The generated script is based on the conventions defined in conform. All these conventions can be rewritten, or you can add your own conventions as needed.

The next section describes XML ing.

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.