【发布时间】:2017-01-16 03:20:59
【问题描述】:
我正在尝试使用 NHibernate 事件列表器进行简单的构造函数注入,这是一个示例:
public class FormGeneratorUpdate : IPostUpdateEventListener
{
private readonly IHostingEnvironment _env;
public FormGeneratorUpdate(IHostingEnvironment env)
{
_env = env;
}
public void OnPostUpdate(PostUpdateEvent @event)
{
string typeName = @event.Entity.GetType().Name;
dynamic entity = @event.Entity;
string filePath =
$"{_env.ContentRootPath}\\App_Data\\FormGenerator\\{typeName}\\{entity.Id}.json";
File.Delete(filePath);
string json = JsonConvert.SerializeObject(entity);
using (FileStream fs = File.Create(filePath))
{
// Add some text to file
Byte[] content = new UTF8Encoding(true).GetBytes(json);
fs.Write(content, 0, content.Length);
}
}
}
我目前将 NHibernate bytecodeProvider 设置为 autofac 实现,如下所示:
NHibernate.Cfg.Environment.BytecodeProvider =
new AutofacBytecodeProvider(_container, new ProxyFactoryFactory(), new DefaultCollectionTypeFactory());
在构建会话工厂时这似乎工作得很好,但我的问题是如何在不首先实例化的情况下使用 NHibernate 配置注册事件侦听器?我可以注册它的每一种方式都需要我首先实例化对象,如下所示:
cfg.SetListener(ListenerType.Update, new FormGeneratorUpdate());
由于构造函数不是空的,因此会引发错误...我尝试仅使用 Autofac 注册事件侦听器,但这似乎也不起作用,我相信我也必须在配置中进行设置一些方法。
【问题讨论】:
标签: c# nhibernate autofac