gpt4 book ai didi

c# - Nhibernate envers,使用依赖注入(inject)将用户添加到修订实体中

转载 作者:太空宇宙 更新时间:2023-11-03 12:37:11 24 4
gpt4 key购买 nike

看完thisthis我接近解决方案但还没有结束,因为我想念如何应用到我的实现中。

我有这个自定义修订实体和修订监听器:

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;
}
}
}

这是我的 session 工厂

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中进行工作注入(inject):

public EnversRevisionListener(IUserManagement um)

编辑:

继续工作,我看到了真正的任务:private static void InitializeSessionFactory:因为它是静态的,所以无法在其中注入(inject)引用。必须找到不同的方式

最佳答案

解决了将依赖项注入(inject)到 UnitOfWork 类中的问题:

公共(public)类 UnitOfWork : IUnitOfWork { private static readonly object _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();
}
}
}

然后是 session 工厂:

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;
}
}

关于c# - Nhibernate envers,使用依赖注入(inject)将用户添加到修订实体中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40483412/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com