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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

In C/C++, the pointer comparison rules are as follows: pointers pointing to the same object are equal. Pointers to different objects are not equal. Exception: Pointers to null addresses are equal.

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.
