Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 8. Configuration > Time for action Using Loquacious to configure NHiber...

Time for action Using Loquacious to configure NHibernate

As always, we do not want to talk too much about the theory, but immediately dive into an example. In this example, we use some of the concepts we discussed in earlier chapters, such as the usage of value objects and of the NHibernate Profiler tool.

  1. Open Visual Studio and create a new project of type Console Application. Name the project LoquaciousConfigurationSample.

  2. As we again want to use SQLite in this example, we have to overcome some of its limitations and adjust our project settings accordingly. Therefore, in the Solution Explorer, right-click on the project and click on Properties.

  3. On the Application tab, select .NET Framework 3.5 as Target framework.

  4. On the Build tab, make sure that Platform target is set to Any CPU.

  5. Add a solution folder named Schema to the solution.

  6. In the Solution Explorer, right-click on the Schema folder and select Add | Existing Item…. Browse to the lib folder, select the nhibernate-mapping.xsd file, and click on OK to add this file to the Schema folder.

  7. In the Solution Explorer, right-click on the References folder of the LoquatiousConfigurationSample project and select Add Reference….

  8. Navigate to the lib folder and select the following four files:

  9. NHibernate.dll

  10. NHibernate.ByteCode.Castle.dll

  11. HibernatingRhinos.Profiler.Appender.dll

  12. System.Data.SQLite.dll

  13. Again, in the Solution Explorer, right-click on the project and select Add | New Item…. Select Application Configuration File as the template and click on Add. A file called App.config is added to your project.

  14. Open the App.config file and add a definition for the connection string we are going to use in this example. SQLite in file mode will be our database. Thus, the content of the App.config file should look similar to the following code snippet:

    <?xml version="1.0"?>
    <configuration>
    <connectionStrings>
    <add name="Sample" connectionString="data source=loquaciousConfig.dbf; version=3;new=true;"/>
    </connectionStrings>
    </configuration>
    
    
    					  
  15. In this example, we want to define a Person entity which (among others) has a Name property which is a value type. Add a new class file called Person.cs to the project.

  16. Add the following code snippet to the file to define the entity:

    using System;
    namespace LoquatiousConfigurationSample
    {
    public class Person
    {
    public Guid Id { get; set; }
    public Name Name { get; set; }
    public string SSN { get; set; }
    public DateTime Birthdate { get; set; }
    }
    }
    
  17. Note that this time we use an Id of type Guid— which is our primary key— and not, as in preceding examples, of type int.

  18. Add another new class file, Name.cs, to the project.

  19. To define the Name value type, use the code shown in the following code snippet:

    namespace LoquatiousConfigurationSample
    {
    public class Name
    {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MiddleName { get; set; }
    }
    }
    
  20. A value type always needs to implement equality based on the content of all its properties, as discussed in Chapter 3. Thus, add the following code snippet to the Name class to define this equality by overriding Equals and GetHashCode:

    public bool Equals(Name other)
    {
    if (ReferenceEquals(null, other)) return false;
    if (ReferenceEquals(this, other)) return true;
    return Equals(other.FirstName, FirstName) && Equals(other.LastName, LastName) && Equals(other.MiddleName, MiddleName);
    }
    public override bool Equals(object obj)
    {
    if (obj.GetType() != typeof (Name)) return false;
    return Equals((Name) obj);
    }
    public override int GetHashCode()
    {
    unchecked
    {
    int result = (FirstName != null ? FirstName.GetHashCode() : 0);
    result = (result*397) ^ (LastName != null ? LastName.GetHashCode() : 0);
    result = (result*397) ^ (MiddleName != null ? MiddleName.GetHashCode() : 0);
    return result;
    }
    }
    
    
    					  
  21. To define the mapping, add a new file of type XML to the project. Call the file Person.hbm.xml.

  22. In the Solution Explorer, select the file and make sure that in the Properties window, the Build Action is set to Embedded Resource.

  23. Add the following XML code snippet to define the mapping of the Person entity:

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="LoquatiousConfigurationSample" namespace="LoquatiousConfigurationSample" >
    <class name="Person" lazy="false">
    <id name="Id">
    <generator class="guid.comb"/>
    </id>
    <component name="Name">
    <property name="FirstName"/>
    <property name="LastName"/>
    <property name="MiddleName"/>
    </component>
    <property name="SSN"/>
    <property name="Birthdate"/>
    </class>
    </hibernate-mapping>
    
    
    					  
  24. In the preceding XML definition, specifically note the usage of the ID generator class guid.comb which instructs NHibernate to generate Ids of type Guid that are optimized for the usage in relational databases.

  25. Furthermore, note the usage of the component tag to define the value type Name.

  26. We want to use the NHibernate Profiler application to monitor the database communication of our application through NHibernate. Thus, add code to our application to support the profiler. To the first line of the Program class in the Main method, add the following code snippet:

    HibernatingRhinos.Profiler.Appender.NHibernate .NHibernateProfiler.Initialize();
    
    
    					  
  27. Now, finally, we will use the fluent API located in the NHibernate.Loquacious namespace to configure NHibernate. Add a static method GetConfiguration to the Program class. The method contains the configuration code, as shown in the following code snippet:

    private static Configuration GetConfiguration()
    {
    var cfg = new Configuration();
    cfg.SessionFactory() .Proxy .Through<ProxyFactoryFactory>() .Mapping .UsingDefaultCatalog("sampleCatalog") .UsingDefaultSchema("dbo") .Integrate .LogSqlInConsole() .Using<SQLiteDialect>() .Connected .Through<DriverConnectionProvider>() .By<SQLite20Driver>() .ByAppConfing("Sample");
    cfg.AddAssembly(typeof(Person).Assembly);
    return cfg;
    }
    
    
    					  
  28. In the preceding code, we first create a new instance of type NHibernate.Cfg.Configuration. Then we use the extension method SessionFactory() to get access to the fluent configuration API. Next, we define what proxy factory factory NHibernate shall use. Then we define what the default catalog should be and the schema that NHibernate should use. With the Integrate keyword, we start the declaration of the database driver and dialect we are going to use, as well as the connection string we have defined in the App.config file.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint