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

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

WBOY
Release: 2016-07-06 13:30:25
Original
1559 people have browsed it

有 几种 方法 可以 用来 声明 一个 验证 规则 。 最常用的方法是使用对应的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>
Copy after login

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

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


source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template