Nhibernate envers,使用依赖注入将用户添加到修订实体中
Nhibernate envers, use dependency injection to add user into revision entity
阅读 this and this 后,我接近解决方案但还没有结束,因为我想念如何应用到我的实施中。
我有这个自定义修订实体和修订监听器:
public class _NHRevEntity : DefaultRevisionEntity {
public virtual int IdUtente { get; set; }
}
public class EnversRevisionListener : IRevisionListener {
private int _userName = 0;
public EnversRevisionListener(IUserManagement um) : base() {
_userName = um.UtenteCorrente.Id;
}
public void NewRevision(object revisionEntity) {
var casted = revisionEntity as _NHRevEntity;
if (casted != null) {
casted.IdUtente = _userName;
}
}
}
这是我的 SessionFactory
public class SessionFactory : IDisposable {
private static ISessionFactory _sessionFactory;
private static NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
static readonly object factorylock = new object();
private static void InitializeSessionFactory() {
_sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(c => c.FromAppSetting("FluentNHibernateConnection"))
.ShowSql())
.Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly())
)
.ExposeConfiguration(SetupEnvers)
.ExposeConfiguration(config => new SchemaUpdate(config).Execute(false, true))
.BuildSessionFactory();
}
private static void SetupEnvers(NHibernate.Cfg.Configuration cfg) {
var enversConf = new NHibernate.Envers.Configuration.Fluent.FluentConfiguration();
enversConf.Audit<AutitedClass>();
IRevisionListener revListner = new EnversRevisionListener();
enversConf.SetRevisionEntity<_NHRevEntity>(e => e.Id, e => e.RevisionDate, revListner);
cfg.IntegrateWithEnvers(enversConf);
}
internal ISession OpenSession() {
lock (factorylock) {
if (_sessionFactory == null) {
InitializeSessionFactory();
}
}
NHibernate.ISession session = _sessionFactory.OpenSession();
return session;
}
}
核心问题是如何在revisionListner
中进行工作注入:
public EnversRevisionListener(IUserManagement um)
编辑:
继续工作,我看到了真正的任务:private static void InitializeSessionFactory
:因为它是静态的,所以无法在其中注入引用。必须另辟蹊径
解决了将依赖注入移植到 UnitOfWork Class:
public class 工作单元:IUnitOfWork
{
私有静态只读对象 _syncRoot = new object();
private ITransaction _transaction;
private SessionFactory _nhHelper;
internal ISession _session;
public UnitOfWork(SessionFactory sf)
{
//Contract.Ensures(_session.IsOpen);
_nhHelper = sf;
InizializzaSessione();
}
public void InizializzaSessione()
{
lock (_syncRoot)
{
if (_session == null || _session.IsOpen == false)
{
_session = _nhHelper.OpenSession();
}
}
}
然后是会话工厂:
public class SessionFactory : IDisposable
{
private static ISessionFactory _sessionFactory;
private static NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
static readonly object factorylock = new object();
IUserManagement um;
public SessionFactory(IUserManagement ium)
{
um = ium;
}
private static void InitializeSessionFactory(IUserManagement um, bool genereteDB = false)
{
_sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(c => c.FromAppSetting("FluentNHibernateConnection"))
.ShowSql())
.Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly())
.Conventions.AddFromAssemblyOf<SessionFactory>()
)
.ExposeConfiguration(config => SetupEnvers(config, um))
.ExposeConfiguration(config => new SchemaUpdate(config).Execute(false, true))
.BuildSessionFactory();
}
private static void SetupEnvers(NHibernate.Cfg.Configuration cfg, IUserManagement um)
{
try
{
var enversConf = new NHibernate.Envers.Configuration.Fluent.FluentConfiguration();
IRevisionListener revListner = new EnversRevisionListener(um);
enversConf.SetRevisionEntity<RevisionEntity>(e => e.Id, e => e.RevisionDate, revListner);
}
catch (Exception ex)
{
throw ex;
}
}
internal ISession OpenSession()
{
lock (factorylock)
{
if (_sessionFactory == null)
{
InitializeSessionFactory(um);
}
}
NHibernate.ISession session = _sessionFactory.OpenSession();
return session;
}
}
阅读 this and this 后,我接近解决方案但还没有结束,因为我想念如何应用到我的实施中。
我有这个自定义修订实体和修订监听器:
public class _NHRevEntity : DefaultRevisionEntity {
public virtual int IdUtente { get; set; }
}
public class EnversRevisionListener : IRevisionListener {
private int _userName = 0;
public EnversRevisionListener(IUserManagement um) : base() {
_userName = um.UtenteCorrente.Id;
}
public void NewRevision(object revisionEntity) {
var casted = revisionEntity as _NHRevEntity;
if (casted != null) {
casted.IdUtente = _userName;
}
}
}
这是我的 SessionFactory
public class SessionFactory : IDisposable {
private static ISessionFactory _sessionFactory;
private static NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
static readonly object factorylock = new object();
private static void InitializeSessionFactory() {
_sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(c => c.FromAppSetting("FluentNHibernateConnection"))
.ShowSql())
.Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly())
)
.ExposeConfiguration(SetupEnvers)
.ExposeConfiguration(config => new SchemaUpdate(config).Execute(false, true))
.BuildSessionFactory();
}
private static void SetupEnvers(NHibernate.Cfg.Configuration cfg) {
var enversConf = new NHibernate.Envers.Configuration.Fluent.FluentConfiguration();
enversConf.Audit<AutitedClass>();
IRevisionListener revListner = new EnversRevisionListener();
enversConf.SetRevisionEntity<_NHRevEntity>(e => e.Id, e => e.RevisionDate, revListner);
cfg.IntegrateWithEnvers(enversConf);
}
internal ISession OpenSession() {
lock (factorylock) {
if (_sessionFactory == null) {
InitializeSessionFactory();
}
}
NHibernate.ISession session = _sessionFactory.OpenSession();
return session;
}
}
核心问题是如何在revisionListner
中进行工作注入:
public EnversRevisionListener(IUserManagement um)
编辑:
继续工作,我看到了真正的任务:private static void InitializeSessionFactory
:因为它是静态的,所以无法在其中注入引用。必须另辟蹊径
解决了将依赖注入移植到 UnitOfWork Class:
public class 工作单元:IUnitOfWork { 私有静态只读对象 _syncRoot = new object();
private ITransaction _transaction;
private SessionFactory _nhHelper;
internal ISession _session;
public UnitOfWork(SessionFactory sf)
{
//Contract.Ensures(_session.IsOpen);
_nhHelper = sf;
InizializzaSessione();
}
public void InizializzaSessione()
{
lock (_syncRoot)
{
if (_session == null || _session.IsOpen == false)
{
_session = _nhHelper.OpenSession();
}
}
}
然后是会话工厂:
public class SessionFactory : IDisposable
{
private static ISessionFactory _sessionFactory;
private static NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
static readonly object factorylock = new object();
IUserManagement um;
public SessionFactory(IUserManagement ium)
{
um = ium;
}
private static void InitializeSessionFactory(IUserManagement um, bool genereteDB = false)
{
_sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(c => c.FromAppSetting("FluentNHibernateConnection"))
.ShowSql())
.Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly())
.Conventions.AddFromAssemblyOf<SessionFactory>()
)
.ExposeConfiguration(config => SetupEnvers(config, um))
.ExposeConfiguration(config => new SchemaUpdate(config).Execute(false, true))
.BuildSessionFactory();
}
private static void SetupEnvers(NHibernate.Cfg.Configuration cfg, IUserManagement um)
{
try
{
var enversConf = new NHibernate.Envers.Configuration.Fluent.FluentConfiguration();
IRevisionListener revListner = new EnversRevisionListener(um);
enversConf.SetRevisionEntity<RevisionEntity>(e => e.Id, e => e.RevisionDate, revListner);
}
catch (Exception ex)
{
throw ex;
}
}
internal ISession OpenSession()
{
lock (factorylock)
{
if (_sessionFactory == null)
{
InitializeSessionFactory(um);
}
}
NHibernate.ISession session = _sessionFactory.OpenSession();
return session;
}
}