WebService:在将对象发送到客户端之前对其进行编辑

发布于 2024-12-10 20:14:33 字数 445 浏览 0 评论 0原文

我正在开发一个使用 Web 服务来搜索数据、在数据库中插入记录等的应用程序。

我有一个问题:我在对象中有一些字符串属性,这些属性在末尾填充了空格,或者只填充了空格。这真的很烦人:/

我有一个想法:在将对象发送到客户端之前修剪 Web 服务上的所有字符串属性。这可以解决我的问题:D

这可以使用反射或其他东西吗?

//WebMethod
public MillenniumCoreCustomer LoadCustomer()
{
     MyObject returnObj = new MyObject();
     returnObj = GetDataFromSourceNotControlatedByMe();

     // I want to trim all string properties here
     return returnObj;
}

I'm developing an application that uses webservices to search data, insert records in database, etc.

I have a problem: I have some string properties in objects that are filled with spaces at the end, or with just spaces. This is really annoying :/

I had an idea: trim all string properties on the webservice before send the objects to the client. This would solve my problem :D

Is this possible using reflection or some other thing?

//WebMethod
public MillenniumCoreCustomer LoadCustomer()
{
     MyObject returnObj = new MyObject();
     returnObj = GetDataFromSourceNotControlatedByMe();

     // I want to trim all string properties here
     return returnObj;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执笔绘流年 2024-12-17 20:14:33

你可以,但这是一个糟糕的主意。

想象一下您发送了 10,000 个对象。您将在这 10,000 行上使用反射来修剪字符串属性。更糟糕的是,您将不得不“搜索”每个对象的属性列表,直到找到字符串。

只需对字符串属性调用 Trim() 即可。你已经知道它们是哪些了。

编辑
在您的代码中,您已经引用了 MyObject,它已经公开了哪些属性是字符串,哪些不是字符串。只需对这些属性调用 Trim() 即可,但由于字符串是不可变的,因此请确保执行类似的操作

myObject.StringProperty=myObject.StringProperty.Trim();

You can, but it's a terrible idea.

Imagine you send 10,000 objects. You'll be using reflection on those 10,000 rows just to trim string properties. Worse, you'll have "to search" the property list of each object until you find the ones that are strings.

Just call Trim() on the string properties. You already know which ones they are.

EDIT
On your code you already have a reference to MyObject which already exposes which properties are strings and which aren't. Just call Trim() on those properties but since strings are immutable, make sure you do something like

myObject.StringProperty=myObject.StringProperty.Trim();
十年不长 2024-12-17 20:14:33

这是一个非常糟糕的主意。

您的服务旨在查询数据库并返回数据。这就是它应该做的。

如果您不需要尾随空格,那么一开始就不要将尾随空格放入数据库中。

This is a very bad idea.

Your service is meant to query the database and return data. That's what it should do.

If you don't want trailing spaces, then don't put the trailing spaces into the database to begin with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文