有一个类通过每个表有1 string[]
变量来定义各种表中的主键。例如:这个重构有什么好处吗?
static string[] my_table_foo_TablePrimaryKeys = new string[] { "primary_key1", "primary_key2" }
static string[] my_table_bar_TablePrimaryKeys = new string[] { "user_id", "customer_number" }
我觉得这是一个有点乱,对我们以后添加第三个表的情况下不易伸长,我们要回到这个类来定义新的第三个表的主键。所以,我重构它看起来像这样:
static Dictionary<string, string[]> tablePrimaryKeys = new Dictionary<string, string[]>()
{
{"my_table_foo", new string[] { "primary_key1", "primary_key2" }},
{"my_table_bar", new string[] { "user_id", "customer_number" }}
};
你会认为这是一个体面的重构改变?为什么?
此外,它也是一个小干净,在我主要的钥匙被引用的愚见。例如:
在前一种情况:
DoStuffWithPrimaryKeys(my_table_foo_TablePrimaryKeys, "other stuff", 1000);
而在后一种情况下:
string[] keys = tablePrimaryKeys["my_table_foo"];
DoStuffWithPrimaryKeys(keys, "other stuff", 1000);
如果有人也想提什么的原则是“可接受的重构”,如何知道重构可以接受什么,什么不可以,那会很好,很有教育意义。
我正在使用C#和.NET 3.5。
如果'my_table_foo'只存在于数据库中,是不是数据库所关心的?如果'my_table_foo'有一个匹配的C#类,你有没有考虑过属性? –