Question

I really can't get this working so i'm hoping someone here can help :)

here is my castle.config:

<castle>
  <facilities>
    <facility id="nhibernatefaciltity"
              isWeb="true"
              type="Castle.Facilities.NHibernateIntegration.NHibernateFacility,  Castle.Facilities.NHibernateIntegration">
      <factory id="sessionFactory1">
        <settings>
          <item key="show_sql">true</item>
          <item key="connection.provider">NHibernate.Connection.DriverConnectionProvider</item>
          <item key="connection.driver_class">NHibernate.Driver.SqlClientDriver</item>
          <item key="connection.connection_string">Data Source=.;Initial Catalog=xxx;User Id=xxx;Password=xxx;Pooling=False</item>
          <item key="dialect">NHibernate.Dialect.MsSql2005Dialect</item>
          <item key="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</item>
        </settings>
        <assemblies>
          <assembly>Namespace.Model</assembly>
        </assemblies>
      </factory>
    </facility>
  </facilities>
</castle>

Here is the container set up in the global.asax:

Container = new WindsorContainer(new XmlInterpreter(new ConfigResource()))
            .AddFacility<WcfFacility>(f => f.Services.AspNetCompatibility =
                AspNetCompatibilityRequirementsMode.Required)

            .Register(
                Component.For<IServiceBehavior>().Instance(metadata),
                Component.For<IServiceBehavior>().Instance(debug),
                Component
                    .For<IAppServices>()
                    .ImplementedBy<AppServices>()
                    .Named("Namespace.WebServices.AppServices")
                    .LifeStyle.Transient
                    .ActAs(new DefaultServiceModel().Hosted()
                        .AddEndpoints(
                            WcfEndpoint.BoundTo(new BasicHttpBinding()),
                            WcfEndpoint.BoundTo(new WSHttpBinding(SecurityMode.None)).At("ws")
                            ))

                );
var cfgs = Container.ResolveAll<NHibernate.Cfg.Configuration>();
foreach (var cfg in cfgs)
{
    cfg.EventListeners.SaveEventListeners =
        new ISaveOrUpdateEventListener[] { new CustomSaveEventListener() };
}

Probably more info than needed but i want to be complete here.

Here is my CustomSaveEventListener:

public class CustomSaveEventListener : DefaultSaveEventListener
{
    protected override object PerformSaveOrUpdate(SaveOrUpdateEvent evt)
    {
        IHaveAuditInformation entity = evt.Entity as IHaveAuditInformation;
        if (entity != null)
            ProcessEntityBeforeInsert(entity);

        return base.PerformSaveOrUpdate(evt);
    }

    internal virtual void ProcessEntityBeforeInsert(IHaveAuditInformation entity)
    {
        entity.DateAdded = DateTime.Now;
        entity.DateUpdated = DateTime.Now;
    }
}

Here is an example .hbm:

 <?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="GlastoStar.Model" namespace="GlastoStar.Model">
  <class name="User" table="AppUser">
    <id name="Id" column="Id" type="Int64">
      <generator class="hilo"/>
    </id>
    <property name="FirstName"></property>
    <property name="LastName"></property>
    <property name="Password"></property>
    <property name="UserName"></property>
    <property name="Email"></property>
    <property name="DateAdded"></property>
    <property name="DateUpdated"></property>
    <property name="Deleted"></property>
  </class>
</hibernate-mapping>

Here is an example entity:

public class User : IHaveAuditInformation
{
    public virtual Int64 Id { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string Password { get; set; }
    public virtual string UserName { get; set; }
    public virtual string Email { get; set; }
    public virtual DateTime DateAdded { get; set; }
    public virtual DateTime DateUpdated { get; set; }
    public virtual bool Deleted { get; set; }
}

AFAIK - I have done everything but there a skant resources explaining how to do this.

It's bugging the hell out of me.

w://

Was it helpful?

Solution 2

using the fluent config seemed to fix this

OTHER TIPS

Try using the <listeners/> element instead, e.g.:

<castle>
    <facilities>
        <facility id="nhibernatefacility" ...>
            <factory id="...">
                <settings>
                ...
                </settings>
                <assemblies>
                </assemblies>
                <listeners>
                    <listener type="MyNamespace.CustomSaveEventListener, MyAssembly" event="Save"/>
                </listeners>
                ...

You can use Fluent NH and the castle facility together by doing the following:

Facility config stating custom config builder:

<castle>
    <facilities>
        <facility id="nhibernatefacility" 
                  isWeb="true" 
                  type="Castle.Facilities.NHibernateIntegration.NHibernateFacility, Castle.Facilities.NHibernateIntegration" 
                  configurationBuilder="SD.Core.Data.FluentNHibernateConfigurationBuilder, SD.Core.Data">
            <factory id="nhibernate.factory">
                <settings>
                    <item key="show_sql">true</item>
                    <item key="connection.provider">NHibernate.Connection.DriverConnectionProvider</item>
                    <item key="connection.driver_class">NHibernate.Driver.OracleDataClientDriver</item>
                    <item key="dialect">NHibernate.Dialect.Oracle9iDialect</item>
                    <item key="connection.connection_string">. . . </item>
                    <item key="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</item>
                </settings>
            </factory>
        </facility>
    </facilities>
</castle>

Next create your custom config builder, this is where you add the listeners:

public class FluentNHibernateConfigurationBuilder : IConfigurationBuilder
{
    public Configuration GetConfiguration(IConfiguration facilityConfiguration)
    {
        var defaultConfigurationBuilder = new DefaultConfigurationBuilder();
        var configuration = defaultConfigurationBuilder.GetConfiguration(facilityConfiguration);
        //configuration.Configure();
        configuration.AddMappingsFromAssembly(typeof(DogMap).Assembly);
        var auditUpdateEventListener = new AuditUpdateEventListener();
        var auditInsertEventListener = new AuditInsertEventListener();
        configuration.AppendListeners(ListenerType.PreInsert, new[] {auditInsertEventListener});
        configuration.AppendListeners(ListenerType.PreUpdate, new[] {auditUpdateEventListener});
        return configuration;
    }
}

Lastly create your listeners:

public class AuditUpdateEventListener : IPreUpdateEventListener
{
    public bool OnPreUpdate(PreUpdateEvent @event)
    {
        . . . . 
        return false;
    }
}

public class AuditInsertEventListener : IPreInsertEventListener
{
    public bool OnPreInsert(PreInsertEvent @event)
    {
        . . . . 
        return false;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top