我正在使用ERP公司提供的Web服务(Infor的SyteLine 8 Web服务)。通过传递带有DataRow的DataTable,Web服务可以让您将产品传送到ERP系统,DataRow包含进入Item的值的字符串。传递的许多价值来自限制列表。
我最初尝试通过对这些值使用Enums来解决受限列表的问题。不幸的是,我需要使用Enums的许多东西都不符合Enums类型,因为它们的名称类似于“KANBAN / JIT”或“040”,这些名称在Enum中不起作用。
我尝试使用Enums的原因是因为我在我的代码中创建了一个POCO产品对象,我将其传递给Web服务,然后通过将其平移到DataTable中的字符串DataRow来传递给Web服务。这样就可以很容易地确保我根据product.productionType = productionTypeEnum.KANBANJIT;
。
除了使用Enums之外,我还有哪些其他选择,以便我不会遇到问题?
答案 0 :(得分:1)
您可以使用枚举,只需要一些方法将它们转换为字符串,然后再将它们添加到DataRow中。例如,转换器可以实现为扩展方法:
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
public class Product
{
public enum ProductionType
{
Unknown,
KanbanJit,
ZeroForty
}
private ProductionType type;
public ProductionType Type
{
get { return this.type; }
set { this.type = value; }
}
// ... other members of Product class ...
}
public static class ExtensionMethods
{
public static string ToString(this Product.ProductionType productionType)
{
switch (productionType)
{
case Product.ProductionType.KanbanJit: return "KANBAN/JIT";
case Product.ProductionType.ZeroForty: return "040";
default: return string.Empty;
}
}
}
public class Program
{
public static void Main()
{
// Create products, set their production type, and add them to a list
var products = new List<Product>();
products.Add(new Product() { Type = Product.ProductionType.KanbanJit });
products.Add(new Product() { Type = Product.ProductionType.ZeroForty });
// Convert the production types to string and add them to DataRow
foreach (var product in products)
AddProductionTypeToDataRow(product.Type.ToString());
}
static void AddProductionTypeToDataRow(string productionType)
{
// ... implementation comes here ...
Console.WriteLine(productionType);
}
}
==更新==
这是另一种类型安全的解决方案,没有扩展方法:
using System;
using System.Collections.Generic;
public class Product
{
public sealed class ProductionType
{
private string name;
private ProductionType(string name = null) { this.name = name; }
public static implicit operator string(ProductionType type) { return type.name; }
public static readonly ProductionType KanbanJit = new ProductionType("KANBAN/JIT");
public static readonly ProductionType ZeroForty = new ProductionType("040");
// ... other constants ...
public static readonly ProductionType Unknown = new ProductionType();
}
private ProductionType type;
public ProductionType Type
{
get { return this.type; }
set { this.type = value; }
}
// ... other members of Product ...
}
public class Program
{
public static void Main()
{
// Create products, set their production type, and add them to a list
var products = new List<Product>();
products.Add(new Product() { Type = Product.ProductionType.KanbanJit });
products.Add(new Product() { Type = Product.ProductionType.ZeroForty });
// Convert the production types to string and add them to DataRow
foreach (var product in products)
AddProductionTypeToDataRow(product.Type);
}
static void AddProductionTypeToDataRow(string productionType)
{
// ... implementation comes here ...
Console.WriteLine(productionType);
}
}