Nhibernate使用依赖注入将用户添加到修订实体中

时间:2016-11-08 09:36:02

标签: c# nhibernate ninject nhibernate-envers

在阅读thisthis之后,我接近解决方案但未到最后,因为我错过了如何申请我的实施。

我有这个custon修订实体和修订列表:

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:因为它是静态的,所以无法在其中注入引用。必须找到不同的方式

1 个答案:

答案 0 :(得分:1)

解决了将依赖注入移植到UnitOfWork类中的问题:

public class 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();
            }
        }
    }

然后会话工厂:

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