search
Homephp教程php手册XAF实现运行时填加验证规则并保存到数据库中

XAF实现运行时填加验证规则并保存到数据库中

Jul 06, 2016 pm 01:30 PM
xafkeepseveral kindsaccomplishdatabaseruleRuntimeverify

有 几种 方法 可以 用来 声明 一个 验证 规则 。 最常用的方法是使用对应的Attribute来定义。详见这里。 验证 模块还 允许 您 通过 在 业务 类 实现 IRuleSource 接口 定义 自定义 的 验证 规则的来 源 。 IRuleSource 接口 公开 两个 成员 。 名称 属性 应

 

几种方法可以用来声明一个验证规则最常用的方法是使用对应的Attribute来定义。详见这里。验证模块还允许通过业务实现 IRuleSource 接口定义自定义验证规则的来 IRuleSource 接口公开两个成员名称属性返回自定义验证规则唯一名称CreateRules 方法实例化自定义验证规则 一个场景可能需要实现自定义验证规则来执行验证规则存储数据库可以使用种方法需要频繁地自定义验证规则已部署应用程序中不能重新部署应用程序自定义应用程序模型 下面示例阐释了方案  

示例的 RuleRequiredFieldPersistent 一个普通业务实现 IRuleSource 接口用于创建存储数据库的 RuleRequiredField 验证规则 CreateRules 方法实例化一个 RuleRequiredField 验证规则基于 RuleRequiredFieldPersistent 公共属性RuleRequiredFieldPersistent 标记 DefaultClassOptionsAttribute以便最终用户可以手动创建验证规则通过相应列表视图

 

<span style="color: #000000;">[DefaultClassOptions]
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> RuleRequiredFieldPersistent : BaSEObject, 
    DevExPRess.Persistent.Validation.IRuleSource {
    </span><span style="color: #0000ff;">public</span> RuleRequiredFieldPersistent(session session) : <span style="color: #0000ff;">base</span><span style="color: #000000;">(session) { }
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span><span style="color: #000000;"> RuleName {
        </span><span style="color: #0000ff;">get</span> { <span style="color: #0000ff;">return</span> GetPropertyValuestring>(<span style="color: #800000;">"</span><span style="color: #800000;">RuleName</span><span style="color: #800000;">"</span><span style="color: #000000;">); }
        </span><span style="color: #0000ff;">set</span> { SetPropertyValue(<span style="color: #800000;">"</span><span style="color: #800000;">RuleName</span><span style="color: #800000;">"</span><span style="color: #000000;">, value); }
    }
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span><span style="color: #000000;"> CustomMessageTemplate {
        </span><span style="color: #0000ff;">get</span> { <span style="color: #0000ff;">return</span> GetPropertyValuestring>(<span style="color: #800000;">"</span><span style="color: #800000;">CustomMessageTemplate</span><span style="color: #800000;">"</span><span style="color: #000000;">); }
        </span><span style="color: #0000ff;">set</span> { SetPropertyValue(<span style="color: #800000;">"</span><span style="color: #800000;">CustomMessageTemplate</span><span style="color: #800000;">"</span><span style="color: #000000;">, value); }
    }
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">bool</span><span style="color: #000000;"> SkipNullOrEmptyValues {
        </span><span style="color: #0000ff;">get</span> { <span style="color: #0000ff;">return</span> GetPropertyValuebool>(<span style="color: #800000;">"</span><span style="color: #800000;">SkipNullOrEmptyValues</span><span style="color: #800000;">"</span><span style="color: #000000;">); }
        </span><span style="color: #0000ff;">set</span> { SetPropertyValue(<span style="color: #800000;">"</span><span style="color: #800000;">SkipNullOrEmptyValues</span><span style="color: #800000;">"</span><span style="color: #000000;">, value); }
    }
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span><span style="color: #000000;"> Id {
        </span><span style="color: #0000ff;">get</span> { <span style="color: #0000ff;">return</span> GetPropertyValuestring>(<span style="color: #800000;">"</span><span style="color: #800000;">Id</span><span style="color: #800000;">"</span><span style="color: #000000;">); }
        </span><span style="color: #0000ff;">set</span> { SetPropertyValue(<span style="color: #800000;">"</span><span style="color: #800000;">Id</span><span style="color: #800000;">"</span><span style="color: #000000;">, value); }
    }
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">bool</span><span style="color: #000000;"> InvertResult {
        </span><span style="color: #0000ff;">get</span> { <span style="color: #0000ff;">return</span> GetPropertyValuebool>(<span style="color: #800000;">"</span><span style="color: #800000;">InvertResult</span><span style="color: #800000;">"</span><span style="color: #000000;">); }
        </span><span style="color: #0000ff;">set</span> { SetPropertyValue(<span style="color: #800000;">"</span><span style="color: #800000;">InvertResult</span><span style="color: #800000;">"</span><span style="color: #000000;">, value); }
    }
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span><span style="color: #000000;"> ContextIDs {
        </span><span style="color: #0000ff;">get</span> { <span style="color: #0000ff;">return</span> GetPropertyValuestring>(<span style="color: #800000;">"</span><span style="color: #800000;">ContextIDs</span><span style="color: #800000;">"</span><span style="color: #000000;">); }
        </span><span style="color: #0000ff;">set</span> { SetPropertyValue(<span style="color: #800000;">"</span><span style="color: #800000;">ContextIDs</span><span style="color: #800000;">"</span><span style="color: #000000;">, value); }
    }
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span><span style="color: #000000;"> Property {
        </span><span style="color: #0000ff;">get</span> { <span style="color: #0000ff;">return</span> GetPropertyValuestring>(<span style="color: #800000;">"</span><span style="color: #800000;">Property</span><span style="color: #800000;">"</span><span style="color: #000000;">); }
        </span><span style="color: #0000ff;">set</span> { SetPropertyValue(<span style="color: #800000;">"</span><span style="color: #800000;">Property</span><span style="color: #800000;">"</span><span style="color: #000000;">, value); }
    }
    [Persistent(</span><span style="color: #800000;">"</span><span style="color: #800000;">ObjectType</span><span style="color: #800000;">"</span><span style="color: #000000;">)]
    </span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">string</span><span style="color: #000000;"> ObjectType {
        </span><span style="color: #0000ff;">get</span><span style="color: #000000;"> {
            </span><span style="color: #0000ff;">if</span>(ObjectTypeCore != <span style="color: #0000ff;">null</span><span style="color: #000000;">) {
                </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> ObjectTypeCore.FullName;
            }
            </span><span style="color: #0000ff;">return</span> <span style="color: #800000;">""</span><span style="color: #000000;">;
        }
        </span><span style="color: #0000ff;">set</span> { ObjectTypeCore =<span style="color: #000000;"> ReflectionHelper.FindType(value); }
    }
    [NonPersistent]
    [TypeConverter(</span><span style="color: #0000ff;">typeof</span><span style="color: #000000;">(DevExpress.Persistent.Base.LocalizedClassInfoTypeConverter))]
    </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> Type ObjectTypeCore {
        </span><span style="color: #0000ff;">get</span> { <span style="color: #0000ff;">return</span> GetPropertyValue<type>(<span style="color: #800000;">"</span><span style="color: #800000;">ObjectTypeCore</span><span style="color: #800000;">"</span><span style="color: #000000;">); }
        </span><span style="color: #0000ff;">set</span> { SetPropertyValue(<span style="color: #800000;">"</span><span style="color: #800000;">ObjectTypeCore</span><span style="color: #800000;">"</span><span style="color: #000000;">, value); }
    }
    </span><span style="color: #0000ff;">#region</span> IRuleSource Members
    <span style="color: #0000ff;">public</span> System.Collections.Generic.ICollection<irule><span style="color: #000000;"> CreateRules() {
        System.Collections.Generic.List</span><irule> list = <span style="color: #0000ff;">new</span> System.Collections.Generic.List<irule><span style="color: #000000;">();
        RuleRequiredField rule </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> RuleRequiredField();
        rule.Properties.SkipNullOrEmptyValues </span>= <span style="color: #0000ff;">this</span><span style="color: #000000;">.SkipNullOrEmptyValues;
        rule.Properties.Id </span>= <span style="color: #0000ff;">this</span><span style="color: #000000;">.Id;
        rule.Properties.InvertResult </span>= <span style="color: #0000ff;">this</span><span style="color: #000000;">.InvertResult;
        rule.Properties.CustomMessageTemplate </span>= <span style="color: #0000ff;">this</span><span style="color: #000000;">.CustomMessageTemplate;
        rule.Properties.TargetContextIDs </span>= <span style="color: #0000ff;">new</span> ContextIdentifiers(<span style="color: #0000ff;">this</span><span style="color: #000000;">.ContextIDs);
        rule.Properties.TargetType </span>= <span style="color: #0000ff;">this</span><span style="color: #000000;">.ObjectTypeCore;
        </span><span style="color: #0000ff;">if</span>(rule.Properties.TargetType != <span style="color: #0000ff;">null</span><span style="color: #000000;">) {
            </span><span style="color: #0000ff;">foreach</span>(PropertyInfo pi <span style="color: #0000ff;">in</span><span style="color: #000000;"> rule.Properties.TargetType.GetProperties()) {
                </span><span style="color: #0000ff;">if</span>(pi.Name == <span style="color: #0000ff;">this</span><span style="color: #000000;">.Property) {
                    rule.Properties.TargetPropertyName </span>=<span style="color: #000000;"> pi.Name;
                }
            }
        }
        </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> i = Validator.RuleSet.RegisteredRules.Count - <span style="color: #800080;">1</span>; i >= <span style="color: #800080;">0</span>; i--<span style="color: #000000;">) {
            </span><span style="color: #0000ff;">if</span>(Validator.RuleSet.RegisteredRules[i].Id == <span style="color: #0000ff;">this</span><span style="color: #000000;">.Id) {
                Validator.RuleSet.RegisteredRules.RemoveAt(i);
            }
        }
        list.Add(rule);
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> list;
    }
    [Browsable(</span><span style="color: #0000ff;">false</span><span style="color: #000000;">)]
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span><span style="color: #000000;"> Name {
        </span><span style="color: #0000ff;">get</span> { <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">this</span><span style="color: #000000;">.RuleName; }
    }
    </span><span style="color: #0000ff;">#endregion</span><span style="color: #000000;">
}</span></irule></irule></irule></type>

可以看到,这个示例中,只返回了一个规则,而在实际项目中,可以使用BO定义一个子集合,集合中定义N种规则。

不要定义N个BO并都实现 IRuleSource 那样有点浪费。


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor