nhibernatecastle-windsorwindsor-nhfacilityfacilities

Getting Nhibernate Facility and Update/Save event listeners working


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://


Solution

  • using the fluent config seemed to fix this