服务器端验证瓦特/微风JS和定制EFContextProvider [英] Server side validation w/ Breeze JS and custom EFContextProvider

查看:162
本文介绍了服务器端验证瓦特/微风JS和定制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){//}

  • 保护覆盖字典<类型,列表与LT; EntityInfo>> BeforeSaveEntities(词典<类型,列表与LT; 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. 如何通过某种形式的验证环境,使服务器端code知道要应用的业务规则?
    我的应用程序可以在不同的地方添加新客户和基于应用程序的上下文一个业务规则应该应用或它应该是可选的。例如,如果有一个明确的添加新客户屏幕以及打印检查屏幕,允许创建对飞的新客户(在这种情况下,更多的规则必须被选中)。这可能是不希望的设计,但它是要求。还有就是客户可以创建,以及其他一些地方....在服务器端,虽然,我并不以决定如何应用(以及以何种顺序)业务规则得到这个背景......此外,我不能为了使用检查我有saveMap的实体的逻辑(在BeforeSaveEntities的情况下),以确定背景下,因为这是不确定性(saveMap可以为不同的应用环境相同的实体)

谢谢
Z ...

Thanks Z...

推荐答案

1)通常从服务器返回自定义验证错误给客户端是简单地扔在服务器上的错误的最简单方法。我还没有试过,但觉得可能工作是,如果你想被应用于特定实体的错误,创建一个包括包含实体/失败,并抛出此异常实体的EntityKey的属性异常。在客户端,你应该能够检索这个错误,在承诺失败分支,自己申请验证错误的具体实体。 (顺便说一句,这听起来像微风合理的功能要求,使这个过程变得更容易,请张贴在的微风用户音色的,所以我们可以了解社会的利益。)

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 的实例的资源名称属性做到这一点。

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...
    }
 }

这篇关于服务器端验证瓦特/微风JS和定制EFContextProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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