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

  • SEARCH
  • Home
  • 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 862657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:10:42+00:00 2026-05-15T09:10:42+00:00

I am trying out Castle ActiveRecord. I want to use the Validation features AND

  • 0

I am trying out Castle ActiveRecord. I want to use the Validation features AND the LINQ features.

In order to use LINQ, you can either:

  1. My preference: Make your entities inherit from ActiveRecordLinqBase<T>, then to query:

    var blogs = (from b in Blog.Queryable select b).ToList();

  2. Use ActiveRecordLinq.AsQueryable<T>, e.g.:

    var blogs = (from b in ActiveRecordLinq.AsQueryable() select b).ToList()

Now, to use the validation features, you have to make your entities inherit from ActiveRecordValidationBase<T>.

Multiple inheritance isn’t supported so, here’s my options:

  1. Use #2 from above while making my entities inherit from ActiveRecordValidationBase<T>. Disadvantage: LINQ statements are longer and uglier.
  2. Create a class that inherits from ActiveRecordLinqBase<T> and duplicates the code found in ActiveRecordValidationBase<T>. Disadvantage: Duplicate code, which must be updated with future ActiveRecord releases. Here’s the class:

Edit: 3. (Not Tested) Simulate multiple inheritance. Disadvantage: Have to keep Property and Method Definitions in sync with updates.

 

using System;
using System.Collections;
using System.Xml.Serialization;
using Castle.ActiveRecord.Framework;
using Castle.Components.Validator;
using NHibernate.Type;

namespace Castle.ActiveRecord.Linq
{
    [Serializable]
    public abstract class ActiveRecordValidationLinqBase<T> : ActiveRecordLinqBase<T>, IValidationProvider where T : class
    {
        // Fields
        [NonSerialized]
        private IValidationProvider _actualValidator;

        // Methods
        protected ActiveRecordValidationLinqBase() { }

        protected override bool BeforeSave(IDictionary state)
        {
            if (!this.IsValid(RunWhen.Insert))
            {
                this.OnNotValid();
            }
            return base.BeforeSave(state);
        }

        public virtual bool IsValid()
        {
            return this.ActualValidator.IsValid();
        }

        public virtual bool IsValid(RunWhen runWhen)
        {
            return this.ActualValidator.IsValid(runWhen);
        }

        protected override bool OnFlushDirty(object id, IDictionary previousState, IDictionary currentState, IType[] types)
        {
            if (!this.IsValid(RunWhen.Update))
            {
                this.OnNotValid();
            }
            return base.OnFlushDirty(id, previousState, currentState, types);
        }

        protected virtual void OnNotValid()
        {
            ActiveRecordValidator.ThrowNotValidException(this.ValidationErrorMessages, this.PropertiesValidationErrorMessages);
        }

        // Properties
        [XmlIgnore]
        protected virtual IValidationProvider ActualValidator
        {
            get
            {
                if (this._actualValidator == null)
                {
                    this._actualValidator = new ActiveRecordValidator(this);
                }
                return this._actualValidator;
            }
        }

        [XmlIgnore]
        public virtual IDictionary PropertiesValidationErrorMessages
        {
            get
            {
                return this.ActualValidator.PropertiesValidationErrorMessages;
            }
        }

        public virtual string[] ValidationErrorMessages
        {
            get
            {
                return this.ActualValidator.ValidationErrorMessages;
            }
        }
    }
}

Is there a better way?

  • 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-15T09:10:42+00:00Added an answer on May 15, 2026 at 9:10 am

    to use the validation features, you have to make your entities inherit from ActiveRecordValidationBase.

    Not necessarily. Validation is a separate Castle project, it’s not very tightly coupled to ActiveRecord. You can run the validation manually. By manually I mean wrapping this in your own repository/DAO class.

    And since it’s loosely coupled you could even choose any other validation framework.

    Personally, I don’t consider ActiveRecordLinq<Blog>.AsQueryable() to be much longer or uglier than Blog.Queryable so I’d go with that option.

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

Sidebar

Related Questions

I see with the Castle validators I can use a length validation attribute. [ValidateLength(6,
I'm trying-out qUnit for the first time but can't get any tests to run.
Trying out Linq for NHibernate and first noticed this: var list1 = (from ut
I'm trying to get Castle ActiveRecord to show me the SQL it generates. The
I'm trying out threading and relatively new to it. What I want to do
I'm trying to figure out how to have Castle Windsor resolve dependancies for objects
I'm new-ish to SQL and am trying to figure out how to use the
Trying out VS 2010, but still want to target .NET 3.5. Have all my
I'm trying to use the Castle NHibernate Facility with the AutoTx Facility. As a
I'm trying out jsRender. What I want to do: JS template: <script id=theaterTemplate type=text/x-jquery-tmpl>

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.