使用自定义EFContextProvider进行服务器端验证 [英] Server side validation with custom EFContextProvider

查看:88
本文介绍了使用自定义EFContextProvider进行服务器端验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读了有关自定义EFContextProvider 并将其实现之后,我仍在尝试弄清楚找出执行服务器端验证的最佳方法是什么,以及如何在保存之前应用业务规则...也就是说,我的问题围绕着应该被覆盖的2种方法:

After reading about Custom EFContextProvider and implementing it, I am still trying to figure out what is the best way to perform server side validation and how to apply business rule before saving...Namely my questions are revolving around 2 methods that are supposed to be overridden:


  • 受保护的覆盖布尔值BeforeSaveEntity(EntityInfo entityInfo){//}

  • 受保护的覆盖Dictionary< Type,List< EntityInfo>> BeforeSaveEntities(Dictionary< Type,List< EntityInfo>> saveMap){//}

  • protected override bool BeforeSaveEntity(EntityInfo entityInfo) { //}
  • protected override Dictionary<Type, List<EntityInfo>> BeforeSaveEntities(Dictionary<Type, List<EntityInfo>> saveMap) { // }

我是请注意,文档指定一次 BeforeSaveEntity方法将在调用BeforeSaveEntities方法之前为每个实体。此外,我围绕的问题是围绕具有特定于域的关系的多个实体验证/应用业务规则,而不必验证单个实体的属性(为此,我相信自定义验证可以用作在此处解释

I am aware that docs specify that "BeforeSaveEntity method will be called for every entity before the BeforeSaveEntities method is called" once. Also, the questions I have revolve around validating/applying business rules on multiple entities that have domain specific relationships and not necessarily validating single entity's property (for that, I believe custom validation could work as explained here)

所以我的问题是:


  1. 如何从服务器返回验证错误?
    一旦我应用了业务规则,并且万一它们失败了,如何将验证错误添加到一个或多个实体?

  2. 如何传递某种验证上下文,以便服务器端代码知道要应用的业务规则?
    我的应用程序可以在几个不同的位置添加新客户,并且根据应用程序上下文,应该应用业务规则,或者应该是可选的。例如,如果有一个明确的添加新客户屏幕以及打印检查屏幕,该屏幕允许即时创建新客户(在这种情况下,必须检查更多规则)。这可能不是期望的设计,但这是必需的。在其他地方也可以创建客户。...但是在服务器端,我没有这个上下文来决定如何应用(以及按什么顺序)业务规则...此外,我不能使用逻辑来检查我在saveMap中具有哪些实体(如果使用BeforeSaveEntities)来确定上下文,因为这不是确定性的(saveMap在不同的应用程序上下文中可以具有相同的实体)

感谢
Z ...

Thanks Z...

推荐答案

1)通常从服务器向客户端返回自定义验证错误的最简单方法是在服务器上抛出错误。我没有尝试过但认为可能有用的方法是,如果您希望将该错误应用于特定实体,请创建一个包含属性的异常,该属性包含失败的实体/实体的EntityKey并抛出此异常。在客户端上,您应该能够在promise failure分支中检索此错误,并将验证错误自己应用于特定实体。 (顺便说一句,听起来像是对微风的合理请求,使此过程更容易。请发布在微风的用户声音,以便我们评估社区的兴趣。)

1) Typically the simplest way to return 'custom' validation errors from the server to the client is to simply throw an error on the server. What I have not tried but think might work is that if you want that error to be applied to a specific entity, create an exception that includes a property that contains the EntityKey's of the entity/entities that fail and throw this exception. On the client you should be able to retrieve this error, in the promise failure branch, and apply the validation error yourself to the specific entity. ( By the way, it does sound like a reasonable feature request for breeze to make this process easier. Please post it on the Breeze User Voice so we can gauge the community's interest.)

2)您有两种方法来应用上下文保存。最简单的方法是通过 SaveOptions.tag 属性。可以在客户端上设置此属性,并将在服务器上反序列化此属性,以通过SaveOptions属性在ContextProvider中使用。 (类似这样):

2) You have two methods to apply a 'context' to a save. The simplest is via the SaveOptions.tag property. This property can be set on the client and will be deserialized on the server for use within your ContextProvider via the SaveOptions property. (something like this):

 public class NorthwindContextProvider : EFContextProvider<NorthwindIBContext_EDMX_2012> {

    protected override bool BeforeSaveEntity(EntityInfo entityInfo) {
       if ((string)SaveOptions.Tag == "myCustomSaveSetting") {

       }
    }

另一种方法是为保存的每个版本创建一个完全独立的终结点。您可以通过 SaveOptions 实例的 resourceName属性来执行此操作。

The other approach is to create a completely separate endpoint for each 'version' of your save. You can do this via the 'resourceName' property of the SaveOptions instance.

 var so = new SaveOptions({ resourceName: "MyCustomSave" });
 return myEntityManager.saveChanges(null, so);

将转到 MyCustomSave控制器方法,而不是标准的 SaveChanges方法。即

will go to the 'MyCustomSave' controller method instead of the standard "SaveChanges" method. i.e.

public class NorthwindIBModelController : ApiController {

    [HttpPost]
    public SaveResult MyCustomSave(JObject saveBundle) {
        ContextProvider.BeforeSaveEntitiesDelegate = MyCustomBeforeSaveEntities;
        return ContextProvider.SaveChanges(saveBundle);
    }

    private Dictionary<Type, List<EntityInfo>> MyCustomBeforeSaveEntities(Dictionary<Type, List<EntityInfo>> saveMap) {     
        // your code...
    }
 }

这篇关于使用自定义EFContextProvider进行服务器端验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆