有没有一种方法可以禁用nhibernate会话/活动记录范围的Castle Active Record验证 [英] Is there a way to disable Castle Active Record validation for an nhibernate session / active record scope

查看:106
本文介绍了有没有一种方法可以禁用nhibernate会话/活动记录范围的Castle Active Record验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有办法为nhibernate会话/活动记录范围禁用活动记录验证?

Is there a way to disable Active Record validation for an nhibernate session / active record scope?

我有一个场景,其中我们正在执行大量项目的删除操作-在某些情况下,客户的数据库中的数据将无法通过验证(该数据是在引入新的验证规则之前或由于手动操作而捕获的)数据库操作等)

I have a scenario whereby we are performing deletion of a large number of items - and in some cases customers have data in their database that will not pass validation (it was captured prior to new validation rules being introduced, or due to manual manipulation of the database etc.)

在删除时,由于构造数据库的方式,会对现有实体进行一些验证检查,但会失败,但会发生异常-阻止删除这些实体.

When deleting, due the way the database is constructed, some validation checks on existing entities occur, and fail with an exception - preventing the deletion of those entities.

对于删除方案,我们希望禁用所有与实体在其中删除的事务/会话/作用域相关联的验证,这可能吗?

For the deletion scenario we would like to disable all validation from occurring associated with the transaction/session/scope the entities are being deleted within, is this possible?

更新23/01/2011

实现了用于禁用验证的简单验证活动记录基类:

Implemented a simple validation active record base class for disabling validation:

public class DisabledValidationProvider : IValidationProvider
{
    public bool IsValid()
    {
        return true;
    }

    public bool IsValid(RunWhen runWhen)
    {
        return true;
    }

    public string[] ValidationErrorMessages
    {
        get { return null; }
    }

    public IDictionary PropertiesValidationErrorMessages
    {
        get { return null; }
    }
}

public class DisableValidationScope : IDisposable
{
    public DisableValidationScope()
    {
        Local.Data["DisableValidationScope"] = true;
    }

    public static bool IsValidationDisabled
    {
        get { return Local.Data["DisableValidationScope"] != null; }
    }

    public void Dispose()
    {
        Local.Data["DisableValidationScope"] = null;
    }
}

public abstract class ScopeAwareValidationBase : ActiveRecordHooksValidationBase
{
    static readonly IValidationProvider DisabledProvider = new DisabledValidationProvider();

    protected override IValidationProvider ActualValidator
    {
        get
        {
            if (DisableValidationScope.IsValidationDisabled)
            {
                return DisabledProvider;
            }
            return base.ActualValidator;
        }
    }
}

我的ActiveRecord模型是从ScopeAwareValidationBase继承的,然后我可以在交易代码周围使用using语句,就可以了.

My ActiveRecord models inherit from ScopeAwareValidationBase and then I can just emplot a using statement around my transaction code, works a treat.

using (new DisableValidationScope()) 
{
    // do transactional thing...
}

推荐答案

我将研究覆盖ActualValidator属性,在那里您可以:

I'd look into overriding the ActualValidator property, there you could either:

  • 提供设置器,让您的代码决定是否基于每个实例应用验证,或者
  • (更好)查找与当前SessionScope相关的某些上下文,该上下文决定是否应用验证.

要禁用验证,您将返回一个始终返回IsValid()等为true的虚拟IValidationProvider.

To disable validation, you'd return a dummy IValidationProvider that always returned true to IsValid(), etc.

这篇关于有没有一种方法可以禁用nhibernate会话/活动记录范围的Castle Active Record验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆