@@ -176,6 +176,106 @@ ObjectExtensionManager.Instance
176
176
177
177
` options ` 有一个名为 ` Configuration ` 的字典,该字典存储对象扩展定义甚至可以扩展. EF Core使用它来将其他属性映射到数据库中的表字段. 请参阅[ 扩展实体文档] ( Customizing-Application-Modules-Extending-Entities.md ) .
178
178
179
+ #### CheckPairDefinitionOnMapping
180
+
181
+ 控制在映射两个可扩展对象时如何检查属性定义. 请参阅* 对象到对象映射* 部分,了解 ` CheckPairDefinitionOnMapping ` 选项.
182
+
183
+ ## Validation
184
+
185
+ 你可能要为你定义的额外属性添加一些 ** 验证规则** . ` AddOrUpdateProperty ` 方法选项允许进行验证的方法有两种:
186
+
187
+ 1 . 你可以为属性添加 ** 数据注解 attributes** .
188
+ 2 . 你可以给定一个action(代码块)执行 ** 自定义验证** .
189
+
190
+ 当你在** 自动验证** 的方法(例如:控制器操作,页面处理程序方法,应用程序服务方法...)中使用对象时,验证会工作. 因此,每当扩展对象被验证时,所有额外的属性都会被验证.
191
+
192
+ ### 数据注解 Attributes
193
+
194
+ 所有标准的数据注解Attributes对于额外属性都是有效的. 例:
195
+
196
+ ```` csharp
197
+ ObjectExtensionManager .Instance
198
+ .AddOrUpdateProperty <IdentityUserCreateDto , string >(
199
+ " SocialSecurityNumber" ,
200
+ options =>
201
+ {
202
+ options .Attributes .Add (new RequiredAttribute ());
203
+ options .Attributes .Add (
204
+ new StringLengthAttribute (32 ) {
205
+ MinimumLength = 6
206
+ }
207
+ );
208
+ });
209
+ ````
210
+
211
+ 使用以上配置,如果没有提供有效的 ` SocialSecurityNumber ` 值, ` IdentityUserCreateDto ` 对象将是无效的.
212
+
213
+ ### 自定义验证
214
+
215
+ 如果需要,可以添加一个自定义action验证额外属性. 例:
216
+
217
+ ```` csharp
218
+ ObjectExtensionManager .Instance
219
+ .AddOrUpdateProperty <IdentityUserCreateDto , string >(
220
+ " SocialSecurityNumber" ,
221
+ options =>
222
+ {
223
+ options .Validators .Add (context =>
224
+ {
225
+ var socialSecurityNumber = context .Value as string ;
226
+
227
+ if (socialSecurityNumber == null ||
228
+ socialSecurityNumber .StartsWith (" X" ))
229
+ {
230
+ context .ValidationErrors .Add (
231
+ new ValidationResult (
232
+ " Invalid social security number: " + socialSecurityNumber ,
233
+ new [] { " SocialSecurityNumber" }
234
+ )
235
+ );
236
+ }
237
+ });
238
+ });
239
+ ````
240
+
241
+ ` context.ServiceProvider ` 可以解析服务.
242
+
243
+ 除了为单个属性添加自定义验证逻辑外,还可以添加在对象级执行的自定义验证逻辑. 例:
244
+
245
+ ```` csharp
246
+ ObjectExtensionManager .Instance
247
+ .AddOrUpdate <IdentityUserCreateDto >(objConfig =>
248
+ {
249
+ // Define two properties with their own validation rules
250
+
251
+ objConfig .AddOrUpdateProperty <string >(" Password" , propertyConfig =>
252
+ {
253
+ propertyConfig .Attributes .Add (new RequiredAttribute ());
254
+ });
255
+
256
+ objConfig .AddOrUpdateProperty <string >(" PasswordRepeat" , propertyConfig =>
257
+ {
258
+ propertyConfig .Attributes .Add (new RequiredAttribute ());
259
+ });
260
+
261
+ // Write a common validation logic works on multiple properties
262
+
263
+ objConfig .Validators .Add (context =>
264
+ {
265
+ if (context .ValidatingObject .GetProperty <string >(" Password" ) !=
266
+ context .ValidatingObject .GetProperty <string >(" PasswordRepeat" ))
267
+ {
268
+ context .ValidationErrors .Add (
269
+ new ValidationResult (
270
+ " Please repeat the same password!" ,
271
+ new [] { " Password" , " PasswordRepeat" }
272
+ )
273
+ );
274
+ }
275
+ });
276
+ });
277
+ ````
278
+
179
279
## 对象到对象映射
180
280
181
281
假设你已向可扩展的实体对象添加了额外的属性并使用了自动[ 对象到对象的映射] ( Object-To-Object-Mapping.md ) 将该实体映射到可扩展的DTO类. 在这种情况下你需要格外小心,因为额外属性可能包含** 敏感数据** ,这些数据对于客户端不可用.
0 commit comments