Discussion:
[nhusers] Pass parameter to envers from castle.windsor(DI)
Cihan İspiroğlu
2017-09-15 06:43:50 UTC
Permalink
I'm trying to extend Envers RevInfo class and add some properties for
audit. I have a web API project, xx.infrastructure.dependency,
xx.datalayer.nhibernate libraries and so on. Also i'm using owin for auth.

I' m registering and installing all container things in
xx.infrastructure.dependency library and call Installer static method on my
Web API project.


public static WebApiControllerActivator Installer()
{
createContainer();
return new WebApiControllerActivator(_container);
}

private static void createContainer()
{
_container = new WindsorContainer();

_container.Kernel.ComponentRegistered += Kernel_ComponentRegistered;

Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("userId", "10421");

_container.Install(new ServiceInstaller());
_container.Install(new RepositoryInstaller());
_container.Install(new NHibernateInstaller(parameters));
_container.Install(new ControllerInstaller());
_container.Install(new LoggerInstaller());
_container.Register(Component.For<UnitOfWorkInterceptor>().LifeStyle.PerWebRequest.LifestylePerWebRequest());
}


Nhibernate installers register sessionfactory...


container.Register(
Component.For<ISessionFactory>().UsingFactoryMethod((k, m, c) => NHibernateSessionFactory.CreateSessionFactory(_factoryParameters)).LifeStyle.Singleton,
Component.For<ISession>().UsingFactory<ISessionFactory, ISession>(factory => factory.OpenSession()).LifestylePerThread()
);


and also CreateSessionFactory method like below..


var enversConf = new EnversNHibernate.Configuration.Fluent.FluentConfiguration();

List<Type> domainEntities = AppDomain.CurrentDomain.GetAssemblies().
Where(assembly => assembly.FullName.StartsWith("XXXX")).
FirstOrDefault().GetTypes()
.Where(t => (typeof(Entity<int>).IsAssignableFrom(t) || typeof(Entity<Guid>).IsAssignableFrom(t) || typeof(Entity<ulong>).IsAssignableFrom(t) ||
typeof(Entity<long>).IsAssignableFrom(t)) && !t.IsGenericType)
.ToList();

foreach (Type type in domainEntities)
enversConf.Audit(type);

CoreNHibernate.Cfg.Configuration cfg = new CoreNHibernate.Cfg.Configuration();
cfg = config.BuildConfiguration();

cfg.BuildMappings();
cfg.SetInterceptor(new TrackingInterceptor(factoryParameters));

//Envers RevType Values
//0(ADD), 1(MOD) and 2(DEL)
ConfigurationKey.AuditTableSuffix.SetUserValue(cfg, "_LOG");
enversConf.SetRevisionEntity<CustomRevInfo>(e => e.Id, e => e.RevisionDate, new CustomRevInfoListener(factoryParameters));
cfg.IntegrateWithEnvers(enversConf);

config.ExposeConfiguration(exp => new SchemaUpdate(cfg).Execute(false, true));
return config.BuildSessionFactory();


CustomRevInfo listener below...


public class CustomRevInfoListener : IRevisionListener{
private ulong _userId = ulong.MinValue;

public CustomRevInfoListener(Dictionary<string, object> additionalParameters) : base()
{
_userId = ulong.Parse(additionalParameters["userId"].ToString());
}

public void NewRevision(object revisionEntity)
{
((CustomRevInfo)revisionEntity).UserId = _userId;
}}


now i'm trying pass parameters to CustomRevInfoListener for add some other
custom audit properties but i can't pass constructor parameter(because
session factory is singleton) add run time is that a way to do this or is
my logic is wrong?
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nhusers+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at https://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/d/optout.
Loading...