我真的无法让这个工作,所以我希望有人可以提供帮助:)。
这是我的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>
这是在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() };
}
可能比需要更多信息,但我想在这里完成。
这是我的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;
}
}
这是一个例子.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>
以下是一个示例实体:
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 - 我已经完成了所有工作,但有一些资源解释如何执行此操作。
这让我不知所措。
瓦特://
答案 0 :(得分:0)
请尝试使用<listeners/>
元素,例如:
<castle>
<facilities>
<facility id="nhibernatefacility" ...>
<factory id="...">
<settings>
...
</settings>
<assemblies>
</assemblies>
<listeners>
<listener type="MyNamespace.CustomSaveEventListener, MyAssembly" event="Save"/>
</listeners>
...
答案 1 :(得分:0)
使用fluent配置似乎解决了这个问题
答案 2 :(得分:0)
您可以通过以下方式一起使用Fluent NH和城堡设施:
设施配置说明自定义配置构建器:
<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>
接下来,创建自定义配置构建器,这是添加侦听器的位置:
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;
}
}
最后创建你的听众:
public class AuditUpdateEventListener : IPreUpdateEventListener
{
public bool OnPreUpdate(PreUpdateEvent @event)
{
. . . .
return false;
}
}
public class AuditInsertEventListener : IPreInsertEventListener
{
public bool OnPreInsert(PreInsertEvent @event)
{
. . . .
return false;
}
}