c# - Nhibernate envers, use dependency injection to add user into revision entity -
after reading this , this come near solution not end since miss how apply implementation.
i have custon revision entity , revision listner:
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 _nhreventity; if (casted != null) { casted.idutente = _username; } } }
and 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; } }
core problem how make work injection in revisionlistner
:
public enversrevisionlistener(iusermanagement um)
edit:
going on working see real quest: private static void initializesessionfactory
: since it's static there no way inject reference in it. must find different way
solved porting dependency injection unitofwork class:
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(); } } }
and session factory:
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; } }
Comments
Post a Comment