i trying test simple nhibernate-based auditing mechanism stores 1 row per changed property changelog table. does, perform actual insert statement expected , perform audit logging twice.
so, do:
string connectionstring = @"data source=.\sqlexpress;initial catalog=audittest;integrated security=sspi;"; fluentconfiguration config = fluently.configure().database(mssqlconfiguration.mssql2008 .connectionstring(c => c.is(connectionstring)).showsql()) .mappings(x => x.fluentmappings.add<class1classmap>()) .mappings(x => x.fluentmappings.add<changelogmap>()) .exposeconfiguration(cfg => { nhibernateauditlistener listener = new nhibernateauditlistener(); cfg.appendlisteners(listenertype.postinsert, new[] { listener }); }); isessionfactory sf = config.buildsessionfactory(); isession session = sf.opensession(); using (itransaction tr = session.begintransaction()) { session.save(new class1() { firstname="peter", lastname="pan", id=100 }); tr.commit(); }
edit:
altered logging code simple see failure:
public void onpostinsert(postinsertevent @event) { if (@event.entity iauditable) { console.writeline("----write audit----"); (int index = 0; index < @event.state.length; index++) console.writeline("----store changes of property {0}----", @event.persister.propertynames[index]); } }
this generates following output:
nhibernate: insert "class1" (firstname, lastname, id) values (@p0, @p1, @p2); @p0 = 'peter' [type: string (0)], @p1 = 'pan' [type: string (0)], @p2 = 1 [type: int64 (0)] ----write audit---- ----store changes of property firstname---- ----store changes of property lastname---- ----write audit---- ----store changes of property firstname---- ----store changes of property lastname----
as see, it's not eventhandler code that's erroneous, framework calling behaves unexpectedly (calling onpostinsert method twice). ideas why happening?
okay everybody, problem exists in detail of handling within programm. building fluentconfiguration-instance on fly creates the basic nhibernate configuration.
this done on call of these 2 lines (variable config of type fluentconfiguration
):
new schemaexport(config.buildconfiguration()).create(true, true);
and
isessionfactory sf = config.buildsessionfactory();
the fluentconfiguration caches first created instance , reuses creating new instance isessionfactory
-instance. on both calls exposeconfiguration
of fluentconfiguration
instance called. there 2 instances of nhibernateauditlistener
within session persisting data.
try this:
string connectionstring = @"data source=.\sqlexpress;initial catalog=audittest;integrated security=sspi;"; var config = fluently.configure().database(mssqlconfiguration.mssql2008 .connectionstring(c => c.is(connectionstring)).showsql()) .mappings(x => x.fluentmappings.add<class1classmap>()) .mappings(x => x.fluentmappings.add<changelogmap>()) .exposeconfiguration(cfg => { nhibernateauditlistener listener = new nhibernateauditlistener(); cfg.appendlisteners(listenertype.postinsert, new[] { listener }); }) .buildconfiguration(); new schemaexport(config).create(true, true); console.writeline("----------------------------------------------"); isessionfactory sf = config.buildsessionfactory(); isession session = sf.opensession(); using (itransaction tr = session.begintransaction()) { session.save(new class1() { firstname="peter", lastname="pan", id=100 }); tr.commit(); }
within config
have real nhibernate configuration instance, 1 listener registered.
got it?!
Comments
Post a Comment