Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 735887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:32:30+00:00 2026-05-14T07:32:30+00:00

In my quest for a version-wide database filter for an application, I have written

  • 0

In my quest for a version-wide database filter for an application, I have written the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Automapping;
using FluentNHibernate.Automapping.Alterations;
using FluentNHibernate.Mapping;
using MvcExtensions.Model;
using NHibernate;

namespace MvcExtensions.Services.Impl.FluentNHibernate
{
    public interface IVersionAware
    {
        string Version { get; set; }
    }

    public class VersionFilter : FilterDefinition
    {
        const string FILTERNAME = "MyVersionFilter";
        const string COLUMNNAME = "Version";

        public VersionFilter()
        {
            this.WithName(FILTERNAME)
                .WithCondition("Version = :"+COLUMNNAME)
                .AddParameter(COLUMNNAME, NHibernateUtil.String );
        }

        public static void EnableVersionFilter(ISession session,string version)
        {
            session.EnableFilter(FILTERNAME).SetParameter(COLUMNNAME, version);
        }

        public static void DisableVersionFilter(ISession session)
        {
            session.DisableFilter(FILTERNAME);
        }
    }

    public class VersionAwareOverride : IAutoMappingOverride<IVersionAware>
    {
        #region IAutoMappingOverride<IVersionAware> Members

        public void Override(AutoMapping<IVersionAware> mapping)
        {
            mapping.ApplyFilter<VersionFilter>();
        }
        #endregion
    }

}

But, since overrides do not work on interfaces, I am looking for a way to implement this.
Currently I’m using this (rather cumbersome) way for each class that implements the interface :

public class SomeVersionedEntity : IModelId, IVersionAware
{
    public virtual int Id { get; set; }
    public virtual string Version { get; set; }
}

public class SomeVersionedEntityOverride : IAutoMappingOverride<SomeVersionedEntity>
{
    #region IAutoMappingOverride<SomeVersionedEntity> Members

    public void Override(AutoMapping<SomeVersionedEntity> mapping)
    {
        mapping.ApplyFilter<VersionFilter>();
    }

    #endregion
}

I have been looking at IClassmap interfaces etc, but they do not seem to provide a way to access the ApplyFilter method, so I have not got a clue here…

Since I am probably not the first one who has this problem, I am quite sure that it should be possible; I am just not quite sure how this works..

EDIT :
I have gotten a bit closer to a generic solution:

This is the way I tried to solve it :

Using a generic class to implement alterations to classes implementing an interface :

public abstract class AutomappingInterfaceAlteration<I> : IAutoMappingAlteration
{
    public void Alter(AutoPersistenceModel model)
    {
        model.OverrideAll(map =>
        {
            var recordType = map.GetType().GetGenericArguments().Single();
            if (typeof(I).IsAssignableFrom(recordType))
            {
                this.GetType().GetMethod("overrideStuff").MakeGenericMethod(recordType).Invoke(this, new object[] { model });
            }
        });
    }

    public void overrideStuff<T>(AutoPersistenceModel pm) where T : I
    {
        pm.Override<T>( a => Override(a));
    }

    public abstract void Override<T>(AutoMapping<T> am) where T:I;
}

And a specific implementation :

public class VersionAwareAlteration : AutomappingInterfaceAlteration<IVersionAware>
{
    public override void Override<T>(AutoMapping<T> am)
    {
        am.Map(x => x.Version).Column("VersionTest");
        am.ApplyFilter<VersionFilter>();
    }
}

Unfortunately I get the following error now :

[InvalidOperationException: Collection was modified; enumeration operation may not execute.]
   System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) +51
   System.Collections.Generic.Enumerator.MoveNextRare() +7661017
   System.Collections.Generic.Enumerator.MoveNext() +61
   System.Linq.WhereListIterator`1.MoveNext() +156
   FluentNHibernate.Utils.CollectionExtensions.Each(IEnumerable`1 enumerable, Action`1 each) +239
   FluentNHibernate.Automapping.AutoMapper.ApplyOverrides(Type classType, IList`1 mappedProperties, ClassMappingBase mapping) +345
   FluentNHibernate.Automapping.AutoMapper.MergeMap(Type classType, ClassMappingBase mapping, IList`1 mappedProperties) +43
   FluentNHibernate.Automapping.AutoMapper.Map(Type classType, List`1 types) +566
   FluentNHibernate.Automapping.AutoPersistenceModel.AddMapping(Type type) +85
   FluentNHibernate.Automapping.AutoPersistenceModel.CompileMappings() +746

EDIT 2 : I managed to get a bit further; I now invoke “Override” using reflection for each class that implements the interface :

public abstract class PersistenceOverride<I> 
{

    public void DoOverrides(AutoPersistenceModel model,IEnumerable<Type> Mytypes)
    {
        foreach(var t in Mytypes.Where(x=>typeof(I).IsAssignableFrom(x)))
            ManualOverride(t,model);
    }

    private void ManualOverride(Type recordType,AutoPersistenceModel model)
    {
        var t_amt = typeof(AutoMapping<>).MakeGenericType(recordType);
        var t_act = typeof(Action<>).MakeGenericType(t_amt);
        var m = typeof(PersistenceOverride<I>)
                .GetMethod("MyOverride")
                .MakeGenericMethod(recordType)
                .Invoke(this, null);
        model.GetType().GetMethod("Override").MakeGenericMethod(recordType).Invoke(model, new object[] { m });
    }

    public abstract Action<AutoMapping<T>> MyOverride<T>() where T:I;
}

public class VersionAwareOverride : PersistenceOverride<IVersionAware>
{
    public override Action<AutoMapping<T>> MyOverride<T>()
    {
        return am =>
        {
            am.Map(x => x.Version).Column(VersionFilter.COLUMNNAME);
            am.ApplyFilter<VersionFilter>();
        };
    }
}

However, for one reason or another my generated hbm files do not contain any “filter” fields….
Maybe somebody could help me a bit further now ??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:32:30+00:00Added an answer on May 14, 2026 at 7:32 am

    Apparently, there was a bug in the current version of fluent-nhibernate. The filters did not get copied when you use Automapper.Override<T> .

    I forked the source, fixed the bug, tested, and I have now sent a pull request to the fluentnhib guys over at github.

    For now, you can download the fixed sourcecode at http://github.com/ToJans/fluent-nhibernate/commit/29058de9b2bc3af85bc433aa6f71549f7b5d8e04

    There is now also a complete blog post on how to do this :
    http://www.corebvba.be/blog/post/How-to-override-interface-mappings-and-creata-a-generic-entity-version-filter-in-fluent-nhibernate.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 376k
  • Answers 376k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Searchlogic can be combined with existing named scopes and must… May 14, 2026 at 8:42 pm
  • Editorial Team
    Editorial Team added an answer Since this answer was somehow accepted and thus will appear… May 14, 2026 at 8:42 pm
  • Editorial Team
    Editorial Team added an answer You should be able to use the instructions in Publishing… May 14, 2026 at 8:42 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.