Example - HomeroThompson/firehawk GitHub Wiki

Example

The aim of this example is to show you how to configure Firehawk from scratch.

The Model

The model is the one shown in the following class diagram: The Model

The Mapping Files

In this case we've two mapping classes, one for the base entity class and one for the product entity. The last one is introduced in order to map the many-to-many relationship between the Product and Category entities.

public class EntityMap : ClassMapping<Entity>
{
  public EntityMap()
  {
    Id(e => e.ID, m => m.Generator(Generators.Identity));
    Version(e => e.Version, m =>
    {
      m.Generated(VersionGeneration.Never); 
      m.UnsavedValue(0);
      m.Insert(true);
    });
  }
}

public class ProductMap : ClassMapping<Product>
{
  public ProductMap()
  {
    Bag(x => x.Categories, m => m.Cascade(Cascade.None), m => m.ManyToMany());
  }
}
The Code

In the following code we configure NHibernate, then Firehawk and last we export the schema using SchemaExport NHibernate tool.

// NHibernate config
var config = new Configuration()
  .DataBaseIntegration(db =>
  {
    db.Dialect<MsSql2012Dialect>();
    db.Driver<SqlClientDriver>();
    db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
    db.IsolationLevel = IsolationLevel.ReadCommitted;
    db.SchemaAction = SchemaAutoAction.Create;
    db.ConnectionStringName = ConnectionStringName;
  });

// Firehawk config
Firehawk.Init()
  .Configure()
    .ConfigureEntities()
      .AddBaseEntity<Entity>()
      .SearchForEntitiesOnThisAssembly(Assembly.GetExecutingAssembly())
      .EndConfig()
    .ConfigureMappings()
      .SearchForMappingsOnThisAssembly(Assembly.GetExecutingAssembly())
      .EndConfig()
    .EndConfiguration()
  .BuildMappings(config);

// Export Schema
var schemaExport = new SchemaExport(config);
schemaExport.Execute(false, true, false);
The First Result

Until now, the example doesn’t add any naming convention and the database schema looks like the one shown in following picture: Database

The Naming Conventions

Next let’s add the following naming conventions:

Firehawk.Init()
 .Configure()
  .ConfigureEntities()
  .AddBaseEntity<Entity>()
  .SearchForEntitiesOnThisAssembly(Assembly.GetExecutingAssembly())
  .EndConfig()
 .ConfigureMappings()
  .SearchForMappingsOnThisAssembly(Assembly.GetExecutingAssembly())
  .EndConfig()
 .ConfigureNamingConventions()
(1).UseConventionForTableNames(TablesNamingConvention.Uppercase)
(2).UseConventionForComponentTableNames(ComponentsTableNamingConvention.EntityNameRelationshipName
(3).UseConventionForManyToManyTableNames(ManyToManyTableNamingConvention.FirstTableNameSecondTableName)
(4).UseConventionForColumnNames(ColumnsNamingConvention.Lowercase)
(5).UseConventionForPrimaryKeyColumnNames(PrimaryKeyColumnNamingConvention.EntityNameIdPropertyName)    (6).UseConventionForForeignKeyColumnNames(ForeignKeyColumnNamingConvention.PropertyNameIdPropertyName)
(7).UseConventionForComponentColumnNames(ComponentsColumnsNamingConvention.EntityPropertyName_ComponentPropertyName)
 .EndConfig()
.EndConfiguration()
.BuildMappings(config);

var schemaExport = new SchemaExport(config);
schemaExport.Execute(false, true, false);

Let’s see a brief description about the code shown above:

Tables

(1) The casing convention for table names is Uppercase.
(2) The names for those tables that store components are generated by appending the name of the entity and the name of the property that relates the entity and the component.
(3) Many-to-many tables names are generated by appending the name of both related tables.

Columns

(4) The casing convention for column names is lowercase. Also
(5) Primary key columns names are generated by appending the entity name to the id property name.
(6) Foreign key columns names are generated by appending the name of the foreign entity and the id property name.
(7) For those columns belonging to components that are mapped to the same table as the parent entity, their names are generated by appending the name of the parent property and the name of the component property.

The Final Result

This is the resulting database schema after conventions are applied Database Final

Summary

You can see the Documentation page in order to get more detail about the full set of naming conventions and their options.

⚠️ **GitHub.com Fallback** ⚠️