我可以在保存更改之前验证实体吗?
我有一个非常简单的 WCF 数据服务应用程序,并且正在执行一些基本的 CRUD 操作。我在正在更改的实体集上有一个 ChangeInterceptor,但 ChangeInterceptor 中的对象是数据库中的当前状态,而不是 HTTP PUT 中发送的内容。 有没有办法在保存对象之前验证其属性?
这是我的 ChangeInterceptor:
[ChangeInterceptor("People")]
public void OnChangePerson(Person personChanging, UpdateOperations updateOperations) {
switch (updateOperations) {
case UpdateOperations.Change:
// personChanging is the database version here, not the changed version.
break;
default:
break;
}
}
这是我的客户端代码 (jQuery):
var data = {
FirstName: "NewFN",
LastName: "NewLN"
};
$.ajax({
type: "PUT",
url: serviceUrl + "/People(" + personID + ")",
contentType: "application/json",
dataType: "json",
data: JSON.stringify(data),
success: function (data) {
alert("Success!");
},
error: function (error) {
alert("An error occured");
}
});
这是发送到服务器的 JSON:
这是收到消息时的 ChangeInterceptor:
我已在此处上传了该项目的代码:http://andyjmay.com/test/2921612/ODataTest.zip
I have a very simple WCF Data Services application and I am doing some basic CRUD operations. I have a ChangeInterceptor on the entity set that is changing, but the object in the ChangeInterceptor is the current state in the database, not what is sent in the HTTP PUT. Is there a way to validate the properties of the object before saving it?
Here is my ChangeInterceptor:
[ChangeInterceptor("People")]
public void OnChangePerson(Person personChanging, UpdateOperations updateOperations) {
switch (updateOperations) {
case UpdateOperations.Change:
// personChanging is the database version here, not the changed version.
break;
default:
break;
}
}
Here is my client-side code (jQuery):
var data = {
FirstName: "NewFN",
LastName: "NewLN"
};
$.ajax({
type: "PUT",
url: serviceUrl + "/People(" + personID + ")",
contentType: "application/json",
dataType: "json",
data: JSON.stringify(data),
success: function (data) {
alert("Success!");
},
error: function (error) {
alert("An error occured");
}
});
Here is the JSON being sent to the server:
Here is the ChangeInterceptor when the message is received:
I have uploaded the code for this project here: http://andyjmay.com/test/2921612/ODataTest.zip
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我下载了您的示例,重现了您的问题,并且目前能够使用此解决方法查看最新更新的值。
当我在内部调查这个问题时,您可以更改代码以使用 Merge 动词而不是 PUT 吗?
通过此更改,当您通过 jQuery 客户端更新值时,您现在应该能够看到传递到 ChangeInterceptors 的最新实体值。
<代码>$.ajax({
beforeSend: 函数 (xhrObj) {
xhrObj.setRequestHeader("X-Http-Method", "MERGE");
},
类型:“POST”,
url: serviceUrl + "/People(" + personID + ")",
内容类型:“application/json”,
数据类型:“json”,
数据:JSON.stringify(data),
成功:函数(数据){
GetAllPeople();
},
错误:函数(错误){
警报(错误);
}
});
I downloaded your sample , reproed your issue and was able to see the latest updated value using this work-around for now.
While I investigate this internally,Can you change your code to use a Merge verb instead of a PUT ?
With this change, you should now be able to see the latest entity values being passed in to the ChangeInterceptors when you update the values via the jQuery client.
$.ajax({
beforeSend: function (xhrObj) {
xhrObj.setRequestHeader("X-Http-Method", "MERGE");
},
type: "POST",
url: serviceUrl + "/People(" + personID + ")",
contentType: "application/json",
dataType: "json",
data: JSON.stringify(data),
success: function (data) {
GetAllPeople();
},
error: function (error) {
alert(error);
}
});
WCF 有一些很好的扩展,您可以编写它们,例如 MessageInspector 和 ParameterInspector。
我确信其中之一可以帮助您在服务器开始处理请求之前验证内容。
WCF got some nice extension you can write like MessageInspector and ParameterInspector.
I'm sure one of them can help you validate stuff before the server even starts to work with tthe request.
如果服务是基于 EF 的并且请求是 PUT,那么将提供旧值(这与 EF 提供程序的实现方式有关,并且可能是一个错误,我们将进一步研究)。您可以通过发送 MERGE 请求来解决此问题。我验证了,在这种情况下它会按预期工作(您会得到新值)。 MERGE 有一点不同的语义,但它可能对你有用。 PUT 会覆盖实体,因此如果您没有发送给定属性的值,它将重置为其默认值。 MERGE 仅使用有效负载中的值修改现有实体,因此如果某些属性不在有效负载中,则其值将保持不变。
If the service is EF based and the request is PUT, then the old value will be provided (this has to do with the way the EF provider is implemented and might be a bug, we will look into that some more). You can workaround this by sending a MERGE request instead. I verified, that in that case it works as expected (you get the new values). MERGE has a little different semantics, but it might work for you. PUT overwrites the entity, so if you didn't send a value for a given property it will be reset to its default value. MERGE only modifies the existing entity with the values from the payload, so if some property is not in the payload its value will be left untouched.
嗯...你说personChanging是数据库版本,那肯定应该是更新后的版本。
我的测试(以及产品团队的人员)告诉我这应该是通过网络发布的版本。会不会还有其他问题?
例如,您的属性可以是 Firstname 而不是 FirstName 吗?
Hmm... you say personChanging is the database version, it should definitely be the updated version.
My tests (and people on the product team) tell me it should be the version that came over the wire. Could something else be going wrong?
For example could your property be Firstname instead of FirstName?