views:

1218

answers:

2

I want to implement NHibernate on my domain objects in my project, but I'm not sure how I should go about generating the mapping file, and the database.

I've found some questions that kind of touch on this here and here, but I'm starting with my classes already defined, and would like to start from them and work my way down, not the other way around.

Is there any way to do this?

I'm perfectly fine with a multi-stage process, I just want to know what other people have done that was successful for them.

FYI, I want to deploy the database on SQL Server 2005.

+1  A: 

I like Fluent-NHibernate. See example below for mapping a User class, of course you can use XML.

The x value in the lamda expression represents the domain class.

This is very much like RoR which I like very much

public sealed class UserMap : ClassMap<User>, IMapGenerator
    {
     public UserMap()
     { 
      Id(x => x.Id)
       .WithUnsavedValue(0);
      Map(x => x.Username).TheColumnNameIs("UserName");
      Map(x => x.Password).TheColumnNameIs("Password"); 
      Map(x => x.Salt).ReadOnly();

      Map(x => x.CreatedOn).ReadOnly();
      Map(x => x.CreatedBy).ReadOnly();
      Map(x => x.CreatedAt).ReadOnly();

      Map(x => x.ApprovalStatus)
       .TheColumnNameIs("ApprovalStatusId")
       .CustomTypeIs(typeof(ApprovalStatus));

      Map(x => x.DeletionStatus)
       .TheColumnNameIs("DeletionStatusId")
       .CustomTypeIs(typeof(DeletionStatus));

      References(x => x.Role).Not.Nullable();
      References(x => x.Contact);

     }

     #region IMapGenerator Members

     public System.Xml.XmlDocument Generate()
     {
      return CreateMapping(new MappingVisitor());
     }

     #endregion
    }
Gary
+1 for Fluent. I've read about this before. I'm wondering, however, if all the features of NHibernate are included in Fluent, or does it trail behind fairly significantly?
Joseph
Another question, given the use of Fluent, how would you go about generating your database? Fluent only resolves the mapping issue.
Joseph
I have had no issues with compatibility or feature set. Paco answered the generating question let me know if you have any other ones.
Gary
+1  A: 

About the mapping: You can create the mapping with the Fluent Mapping like Gary. When you have a very uncomplicated domain model, you can use Automapping, a convention based mapping feature of FluentNhibernate:

 var sessionFactory = Fluently.Configure()  
   .Database(MsSqlConfiguration.MsSql2005  
     .ConnectionString(c => c  
       .Is(ApplicationConnectionString)))  
   .Mappings(m =>  
     m.AutoMappings.Add(AutoPersistenceModel.MapEntitiesFromAssemblyOf<Product>())  
   )  
   .BuildSessionFactory();

And that's all you need.

You can build your database with schemaexport:

var schemaExport = new SchemaExport(configuration);
schemaExport.Create(false,true);
Paco
When you say "uncomplicated" domain model, do you have any metrics on what exactly that means? Say for instance, only 1 level deep relationships? No circular references?
Joseph
Thanks for the SchemaExport idea, that's exactly what I was looking for in terms of the Database building.
Joseph
You can have circular references, but you won't have support for legacy db schema's, usertypes, sql formula mapping and a lot of other options that are supported in "manual" fluent nhibernate. Automapping is an option to start with persistence fast in a greenfield application, but when your app grows or when you inherit an app from someone else, you might like to be more specific about your mapping and use "manual" fluent nhibernate, or even hbm.xml
Paco