XAF实现运行时填加验证规则并保存到数据库中
有 几种 方法 可以 用来 声明 一个 验证 规则 。 最常用的方法是使用对应的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 那样有点浪费。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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 latest version

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
Powerful PHP integrated development environment

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
Easy-to-use and free code editor
