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 345213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:03:47+00:00 2026-05-12T11:03:47+00:00

Using Castle ActiveRecord / NHibernate: Is there a way you can force an ICriterion

  • 0

Using Castle ActiveRecord / NHibernate: Is there a way you can force an ICriterion on all queries on a table?

For example, a good amount of of my tables have a “UserId” column. I might want to ensure that I am always selecting rows for the logged in user. I can easily create an ICriterion object, but I am forced to supply it for different methods: FindAll(), FindFirst(), FindLast() etc.

Is there a way to force a WHERE clause on all queries to a Castle ActiveRecord?

  • 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-12T11:03:47+00:00Added an answer on May 12, 2026 at 11:03 am

    I finally found a great solution (using filters). Since Castle AR does not have any native API for mapping to NHibernate filters, this part was pretty much undocumented. So here goes.

    This example filter, will make sure you will never get news more than a year old, no matter what kind of query you use on the ActiveRecord. You can probably think of more practical applications for this.

    First, create an ActiveRecord “News”.

    Use the following code before you initialize ActiveRecordStarter.

    ActiveRecordStarter.MappingRegisteredInConfiguration += MappingRegisteredInConfiguration;
    Castle.ActiveRecord.Framework.InterceptorFactory.Create = () => { return new EnableFiltersInterceptor(); };
    

    Then, add the missing function and class:

    void MappingRegisteredInConfiguration(Castle.ActiveRecord.Framework.ISessionFactoryHolder holder)
    {
        var cfg = holder.GetConfiguration(typeof (ActiveRecordBase));
    
        var typeParameters = new Dictionary<string, IType>
                                      {
                                        {"AsOfDate", NHibernateUtil.DateTime}
                                      };
    
        cfg.AddFilterDefinition(new FilterDefinition("Latest", "", typeParameters));
    
        var mappings = cfg.CreateMappings(Dialect.GetDialect(cfg.Properties));
    
        var newsMapping = cfg.GetClassMapping(typeof (News));
        newsMapping.AddFilter("Latest", ":AsOfDate <= Date");
    }
    
    
    public class EnableFiltersInterceptor : EmptyInterceptor
    {
        public override void SetSession(ISession session)
        {
            session.EnableFilter("Latest").SetParameter("AsOfDate", DateTime.Now.AddYears(-1));
        }
    }
    

    And voila! Queries on News, e.g. FindAll(), DeleteAll(), FindOne(), Exists(), etc. will never touch entries more than a year old.

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

Sidebar

Related Questions

I'm using ASP.Net MVC (still 1.0 for now) with Castle ActiveRecord and NHibernate.Linq. All
Using Castle.Windsor in ASP.NET MVC (3.0) is there any way I can appropriately handle
I'm using Castle ActiveRecord, but this question applies to NHibernate, too, since a solution
Is there a way in Castle Active Record/NHibernate to join across two databases? I
I'm using Castle ActiveRecord which uses NHibernate underneath and I've added a property with
I'm going to refactor a growing project from using Castle Activerecord to pure NHibernate
Assume you are using some libraries like NHibernate or Castle ActiveRecord that use log4net
I decided to make a system for a client using Castle ActiveRecord , everything
for my current project I am using Castle's ActiveRecord in C#. For one of
I am trying to solve an issue when using a Castle ActiveRecord TransactionScope which

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.