gpt4 book ai didi

c# - NetSuite 自定义记录搜索将结果转换为 .NET 对象列表

转载 作者:行者123 更新时间:2023-12-03 21:37:08 26 4
gpt4 key购买 nike

我能够做到的唯一方法是通过下面基于反射的代码。我不敢相信没有“更简单的 NetSuite 方法”可以做到这一点?我错过了一些基本的东西吗?

在对自定义对象执行搜索后,我返回一个数组 Record[] ,然后可以循环遍历并将每个项目转换为 CustomObject .

自定义对象的属性存储在 CustomRecord 中的 customFieldList但这些值不能立即访问,您必须将它们转换为真正的 NetSuite 类型(如 LongCustomFieldRefDoubleCustomFieldRefBooleanCustomFieldRefStringCustomFieldRef 等)。

为了不必费心处理这些乱七八糟的事情,以便在我身边获得漂亮干净的物体,我决定采用以下方法:

创建类的属性名称与 NetSuite 名称匹配(包括大小写)并继承自 NetSuiteBase(定义如下)

public class MyNetSuiteObject : NetSuiteBase //<-- Note base class
{
public string myProperty1 { get; set; }
public bool myProperty2 { get; set; }
public int myProperty3 { get; set; }

public static MyNetSuiteObject FromCustomSearchRecord(CustomRecord customRecord)
{
var ret = new MyNetSuiteObject();
ret.AssignProperties(customRecord);
return ret;
}
}

创建一个基类,它将检查 CustomRecords 并将属性值应用于 .NET 类
public class NetSuiteBase
{

public void AssignProperties(CustomRecord customRecord)
{
var classProps = this.GetType().GetProperties();
foreach (var prop in classProps)
{
var propName = prop.Name;
var propValue = prop.GetValue(this, null);

//get the matching CustomFieldRef out of the customFieldList for the CustomRecord which matches our current property name
var myCustomFieldRef = customRecord.customFieldList.Where(c => c.scriptId == propName).FirstOrDefault();

if (myCustomFieldRef == null) continue;

//we can't get the value out until we cast the CustomFieldRef to its "actual" type.
var custType = myCustomFieldRef.GetType().Name;
switch (custType)
{
case "LongCustomFieldRef":
TrySetProperty(prop, ((LongCustomFieldRef)myCustomFieldRef).value.ToString());
break;

case "DoubleCustomFieldRef":
TrySetProperty(prop, ((DoubleCustomFieldRef)myCustomFieldRef).value.ToString());
break;

case "BooleanCustomFieldRef":
TrySetProperty(prop, ((BooleanCustomFieldRef)myCustomFieldRef).value.ToString());
break;

case "StringCustomFieldRef":
TrySetProperty(prop, ((StringCustomFieldRef)myCustomFieldRef).value.ToString());
break;

case "DateCustomFieldRef":
TrySetProperty(prop, ((DateCustomFieldRef)myCustomFieldRef).value.ToString());
break;

case "SelectCustomFieldRef":
TrySetProperty(prop, ((SelectCustomFieldRef)myCustomFieldRef).value.name.ToString());
break;

case "MultiSelectCustomFieldRef":
TrySetProperty(prop, ((MultiSelectCustomFieldRef)myCustomFieldRef).value.ToString());
break;
default:
Console.WriteLine("Unknown type: " + myCustomFieldRef.internalId);
break;
}
}
}


//Some of the NetSuite properties are represented as strings (I'm looking at you BOOLs), so we pass all the values from above
//as strings and them process/attempt casts
private void TrySetProperty(PropertyInfo prop, string value)
{
value = value.ToLower().Trim();

if (prop.PropertyType == typeof(string))
{
prop.SetValue(this, value);
return;
}


if (prop.PropertyType == typeof(bool))
{
if (value == "yes") value = "true";
if (value == "no") value = "false";
if (value == "1") value = "true";
if (value == "0") value = "false";

bool test;
if (bool.TryParse(value, out test))
{
prop.SetValue(this, test);
return;
}
}

if (prop.PropertyType == typeof(int))
{
int test;
if (int.TryParse(value, out test))
{
prop.SetValue(this, test);
return;
}
}

if (prop.PropertyType == typeof(double))
{
double test;
if (double.TryParse(value, out test))
{
prop.SetValue(this, test);
return;
}
}

if (prop.PropertyType == typeof(decimal))
{
decimal test;
if (decimal.TryParse(value, out test))
{
prop.SetValue(this, test);
return;
}
}

}
}

对自定义对象执行 NetSuite 搜索后,遍历结果并使用上述类将 NetSuite 结果转换为 .NET 类
for (int i = 0, j = 0; i < records.Length; i++, j++)
{
customRecord = (CustomRecord)records[i];

var myNetSuiteObject = MyNetSuiteObject.FromCustomSearchRecord(customRecord);
}

是否有其他一些“NetSuite 方式”可以完成我上面的操作?

最佳答案

不,没有“NetSuite”方式。如果你问我,你所做的远远超出了职责范围,这太神奇了。 NetSuite/SuiteTalk WSDL 非常糟糕,在设计过程中做出了如此多的错误选择,以至于我很震惊地发现它按原样发布给开发人员而没有提出任何问题。下面是另一个例子。

Bad API construction

这是来自他们的 SuiteTalk 类(class)的文档,它表明在解析高级搜索的结果时,它们包含在其中的 SearchRowBasic 对象。在每个 SearchRowBasic 对象中都有多个字段。要访问这些字段的值,您必须获取 SearchColumnField 数组的第一个元素。你为什么要这样做?如果只有一个值(并且他们在他们的文档中声明是的,确实只会有一个值),那么为什么要让返回对象成为一个数组,而不是仅仅将返回对象的值传回给开发人员直接原始类型本身???这只是糟糕的 API 构造,迫使开发人员每次都使用 SearchColumnField[0].searchValue 而不仅仅是 SearchColumnField.searchValue!

关于c# - NetSuite 自定义记录搜索将结果转换为 .NET 对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34299430/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com