0

I'm new in C# and I need some hints to solve this problem: I start to develop an application in C# an Windows Form Application (until this summer, I work 20 years in Visual Fox) The UI is generic (has an grid and controls for show details for current record), and is build dynamic from definitions I stored in an Xml File.

I want to create a class for each table I use for create methods for getting default values, validate fields, and record, etc). I use for this classes a namespace: BmiSqlTables.

Now I need in UI on adding record to get default values, validate fields, etc. The problem i have is to replace switch case statements which can became a very large one (for 50 tables) with something using substitution like in Foxpro

private void GetDefaultValue ( string TableName, string fieldName)
{
switch (tableName)
{
case "person":
  BmiSqlTables.Person.GetDefaulValue( fieldName, valueType);
  break;
case "client":
  BmiSqlTables.Client.GetDefaulValue( fieldName, valueType);
  break;
default:
break
}
}
namespace BmiSqlTables
{
  public static class Person
 {
  public static dynamic GetDefaultValue( string fieldName, string valueType)
  {
  dynamic defaultValue = null;
  switch (fieldName.toLower())
  {
  case "field1":
  case "field2":
  default:
   switch (valueType.ToLower())
    {
    case "string":
    case "varchar":
    case "char:"
      return "";
    case "int":
      return 0;
    default:
      return  null;
    }
  .......
  }
  }
  }
}

In Foxpro the GetDefaultValue can be something like this:

Function GetDefaultValue( tableName, fieldName, valueType)
return BmiSqlTable.&tableName..GetDefaultValue( fieldName, valueType)

Any advice to this problem (and project) will be apreciate. Hope you understand my english, and what I need to do. Thanks in advance.

4

2 回答 2

0

我知道在 FoxPro 中,您会将值存储在表中并使用“替换(或其他任何含义)”将它们取回。在 .NET 中执行相同的操作。将值存储在某个外部源(DB 或 xml 文件或其他)中,并在方法实现中读取它们。

请记住,虽然 FoxPro 也是一个数据库引擎,并且您在 .NET 中可以免费获得数据,但您需要选择要用于数据存储的技术。

对于您的特定示例,保存名称/值对的简单 xml 文件应该可以。查找 System.Xml.XDocument 以了解与 XML 文档交互的多种方式之一。

编辑:如果您仍想将数据保存在代码中而不是某些外部源,请考虑使用一个/多个静态字典 (System.Collections.Generic.Dictionary) 代替巨大的开关盒。

于 2013-10-14T09:23:56.263 回答
0

看一下策略模式:https ://www.google.be/search?q=strategy+pattern

于 2013-10-14T08:13:10.500 回答