Unable to get filters to work with Fluent Configuration + Fluent Mappings

50 views
Skip to first unread message

Alejandro Calbazana

unread,
Jul 9, 2016, 11:05:15 AM7/9/16
to nhusers
Hello,

Not sure if this is the right place, but I seem to be having a rough time with setting up filters using a fluent config.

Here is my configuration is setup something as follows:

var sf = Fluently.Configure()
    .ExposeConfiguration(AddFilters)
    .Database(MsSqlConfiguration.MsSql2012.ConnectionString(_dbConnString).ShowSql())
    .Mappings(m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))     
    ....
    .BuildSessionFactory();


I did not see a way to add filters other than exposing the config and using a method to add them it.  I have a method that establishes a filter definition as follows:

var filterDef = new NHibernate.Engine.FilterDefinition("tenantFilter", null, new Dictionary<string, IType> {{"effectiveTenant", NHibernateUtil.String}}, true);

So far, so good...

When I try to apply the filter in my ClassMap, I get the following:

"An item with the same key has already been added"

My ClassMap looks something like this:

...
Table("Objects");
Id(x => x.Id).Column("ObjectID").Access.LowerCaseField().GeneratedBy.Guid();
ApplyFilter("tenantFilter", ":effectiveTenant = Tenant");
Map(x => x.ExpirationDate);
...

I didn't expect this since I just really want to make my mapping aware of the filter.  Should I be configuring filters some other way?

If I leave off the ApplyFilter from my mapping the filter never kicks in when I enable it and run a query.  However, if I spin through the mappings and call AddFilter directly on the mapping, things seem to work.  Here is an example:

...
            foreach (var mapping in cfg.ClassMappings)
            {
                if (typeof (IMultiTenantAware).IsAssignableFrom(mapping.MappedClass))
                {
                    mapping.AddFilter("tenantFilter", ":effectiveTenant = Tenant");
                }
            }
...

I don't like this approach because it forces a marker interface.  I'd like to add the filter on the mapping if at all possible.

Any insight is appreciated.

Thanks!

Alejandro

Reply all
Reply to author
Forward
0 new messages