views:

45

answers:

1

What would be the best way to implement fluent-nhibernate regarding project architecture?

I have two projects at the moment one for the domain layer and the other is the persistance layer. My problem is that when trying to configure nhibernate I get a circular reference.

The domain references the persistance layer but how do I get the configuration to work without having to reference the domain in the persistance layer i.e. the product class in this line AddFromAssemblyOf()?

Currently my configuration is like this.

return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c =>         c.FromConnectionStringWithKey("DisillStoreConnectionString")))
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Product>())) 
                .BuildSessionFactory();
+1  A: 

You'll want your Fluent config class in your persistence project with your ClassMap classes, not your domain models.

Using your code sample, you'd want to AddFromAssemblyOf on your Map class and not your Model class. See:

return Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008.ConnectionString(
                 c =>
                   c.FromConnectionStringWithKey("DisillStoreConnectionString")))
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<ProductMap>())) 
                .BuildSessionFactory();

This way, your domain project doesn't need to reference your persistence project.

JMP
JMP, thanks for the reply. Would you not have the same problem as the ProductMap referencing Product (Model)?public class ProductMap : ClassMap<Product>How can you populate the domain object without referencing the persistance layer from your domain layer?
Calum Bett
Your domain objects will need to be marked public in their own assembly. Also, your domain objects will not populate themselves, nHibernate will populate them based on information it gets from your configuration class. The domain project need not reference your persistence project at all.
JMP
JMP, Thanks that clears thiings up.
Calum Bett