2010-06-07 52 views
1

我正在嘗試Castle ActiveRecord。我想要使​​用驗證功能和LINQ功能。城堡活動記錄(Linq和驗證)問題

爲了to use LINQ,您可以:

  1. 我的選擇:讓你的實體從ActiveRecordLinqBase<T>繼承,然後詢問:

    VAR博客=(從b在Blog.Queryable選擇B) .ToList();

  2. 使用ActiveRecordLinq.AsQueryable<T>,如:

    VAR博客=(從b在ActiveRecordLinq.AsQueryable()選擇B).ToList()

現在,使用驗證功能,你有使您的實體繼承自ActiveRecordValidationBase<T>

多重繼承是不支持的話,這是我的選擇:

  1. 使用#2上面,同時使我的實體從ActiveRecordValidationBase<T>繼承。缺點:LINQ語句更長,更醜。
  2. 創建一個繼承自ActiveRecordLinqBase<T>的類並複製ActiveRecordValidationBase<T>中的代碼。缺點:代碼重複,必須使用未來的ActiveRecord版本進行更新。這裏是該類:

編輯:3.(未測試)Simulate multiple inheritance.缺點:必須保持屬性和方法定義與更新同步。

 

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; 
      } 
     } 
    } 
} 

有沒有更好的辦法?

回答

3

要使用驗證功能,您必須讓您的實體從ActiveRecordValidationBase繼承。

不一定。驗證是一個單獨的Castle項目,與ActiveRecord並不緊密結合。你可以run the validation manually。通過手動我的意思是包裝在你自己的倉庫/ DAO類。

由於它鬆散耦合,你甚至可以選擇任何其他驗證框架。

就個人而言,我不認爲ActiveRecordLinq<Blog>.AsQueryable()要長得多醜還是比Blog.Queryable所以我用這個選項去。