Related Resources: List of O/R Mappers
Fluent NHibernate provides an AutoPersistenceModel that can greatly ease configuring NHibernate if you stick to various conventions when mapping your NHibernate Entities to tables in your database. This is perfect for new applications when you have control over the database schema and can establish the conventions. AutoPersistenceModel may not be so ideal when you have inherited a database and may end up having to override a number of the default conventions expected by Fluent Nhibernate.
Fluent NHibernate and AutoPersistenceModel
Let's take two domain entities, Product and Category, where there is a 1:m relationship between Category and Product:
public class Category
{
public Category()
{
Products = new List<Product>();
}
public int Id { get; set; }
public string Name { get; set; }
public IList<Product> Products { get; set; }
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public Category Category { get; set; }
}
To stay with conventions I have named the tables in the database the same as the entity names. Normally I might pluralize the table names, but this isn't necessary and the AutoPersistenceModel by default expects the entity and the table name associated with it to be the same. Here is a snapshot of the Product Table in the Database:
One convention I did not follow for the sake of this tutorial is the name of the foreign key, "CategoryId". AutoPersistenceModel expects the foreign key name to be "Category_id" so I will have to overwrite that convention with my own.
With all of this in place I can configue NHibernate with Fluent NHibernate as such:
var config = new Configuration();
var configuration = MsSqlConfiguration.MsSql2005.ShowSql().ConnectionString.Is(
"Data Source=.;Initial Catalog=Temp;Integrated Security=True").ConfigureProperties(config);
var persistentModel = AutoPersistenceModel.MapEntitiesFromAssemblyOf<Product>()
.Where(t => t.Namespace == "ConsoleApplication11.Entities")
.WithConvention(c => c.GetForeignKeyName = p => p.Name + "Id");
persistentModel.Configure(config);
You can see above where I overwrote the convention with regards to the foreign key so that it followed my convention of Name + "Id". I also specified a specific Namespace for which I wanted Fluent NHibernate to look for domain entities that map with the Database Tables. Not doing this may cause Fluent NHibernate to pick up non-entity types and attempt to map them.
One you configure NHibernate you can use it as such:
using (ISession session = config.BuildSessionFactory().OpenSession())
using (var transaction = session.BeginTransaction())
{
var category = new Category
{
Name = "Green Tea"
};
var product = new Product
{
Name = "Gyokuro",
Category = category
};
session.Save(category);
session.Save(product);
transaction.Commit();
}
Conclusion
If you are looking for a simple way to configure NHibernate without XML, you have the option of using Fluent NHibernate and the AutoPersistenceModel.
NHibernate Tutorials