0
votes

Does anyone have an example of the PersistenceFacility using non-fluent NHibernate (xml mappings in NHibernate) with Castle Windsor and code as configuration (no XML for Castle Windsor)? (ASP.NET MVC)

Running through the tutorial, they are using fluent-NHibernate whereas I can't use fluent (I will configure the NHibernate classes via *.hbm.XML).

Tutorial> http://docs.castleproject.org/Windsor.Windsor-Tutorial-Part-Six-Persistence-Layer.ashx

Specific example with Fluent> https://github.com/kkozmic/ToBeSeen/blob/master/src/ToBeSeen/Plumbing/PersistenceFacility.cs

public class PersistenceFacility : AbstractFacility
{
protected virtual void ConfigurePersistence(Configuration config)
{
    SchemaMetadataUpdater.QuoteTableAndColumns(config);
}

protected virtual AutoPersistenceModel CreateMappingModel()
{
    var m = AutoMap.Assembly(typeof(EntityBase).Assembly)
        .Where(IsDomainEntity)
        .OverrideAll(ShouldIgnoreProperty)
        .IgnoreBase<EntityBase>();

    return m;
}

protected override void Init()
{
    var config = BuildDatabaseConfiguration();

    Kernel.Register(
        Component.For<ISessionFactory>()
        .UsingFactoryMethod(_ => config.BuildSessionFactory()),
        Component.For<ISession>()
        .UsingFactoryMethod(k => k.Resolve<ISessionFactory>().OpenSession())
        .LifestylePerWebRequest()
    );
}

protected virtual bool IsDomainEntity(Type t)
{
    return typeof(EntityBase).IsAssignableFrom(t);
}

protected virtual IPersistenceConfigurer SetupDatabase()
{
    return MsSqlConfiguration.MsSql2008
        .UseOuterJoin()
        .ConnectionString(x => x.FromConnectionStringWithKey("ApplicationServices"))
        .ShowSql();
}

private Configuration BuildDatabaseConfiguration()
{
    return Fluently.Configure()
        .Database(SetupDatabase)
        .Mappings(m => m.AutoMappings.Add(CreateMappingModel()))
        .ExposeConfiguration(ConfigurePersistence)
        .BuildConfiguration();
}

private void ShouldIgnoreProperty(IPropertyIgnorer property)
{
    property.IgnoreProperties(p => p.MemberInfo.HasAttribute<DoNotMapAttribute>());
}
}

What I need is the PersistenceFacility configuration to setup NHibernate without Fluent. If someone can demonstrate the code to setup NHibernate etc. for non-fluent NHibernate, or point me to an example/tutorial/blog that would be great!

1

1 Answers

0
votes

It appears to work like this. I still haven't tried saving or retrieving data from the database, but it's error free initializing controllers etc. The rest of the code is like in the tutorial http://docs.castleproject.org/Windsor.Windsor-tutorial-part-one-getting-Windsor.ashx.

NHibernate configuration is in the config file (web.config or nhibernate.cfg.xml).

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
        ...
    </session-factory>
</hibernate-configuration>

Then the PersistenceFacility can simply be like this.

public class PersistenceFacility : AbstractFacility
{

    protected override void Init()
    {
        // NHibernate configures from the <hibernate-configuration> section of a config file
        var config = new Configuration().Configure();

        Kernel.Register(
            Castle.MicroKernel.Registration.Component.For<ISessionFactory>()
                .UsingFactoryMethod(_ => config.BuildSessionFactory()),
            Castle.MicroKernel.Registration.Component.For<ISession>()
                .UsingFactoryMethod(k => k.Resolve<ISessionFactory>().OpenSession())
                .LifestylePerWebRequest()
        );
    }
}

The difference between the tutorial is there is no need for the Fluent configuration and instead using NHibernates Configuration().Configure() works.