C#强制使用字典键
我想知道什么是验证或强制用于Dictionary对象/参数的键的最佳实践 以这种方法为例:C#强制使用字典键,c#,dictionary,standards,C#,Dictionary,Standards,我想知道什么是验证或强制用于Dictionary对象/参数的键的最佳实践 以这种方法为例: public override SqlServerQueryBuilder From(Dictionary<string, string> tableReferenceDictionary) { this.SelectQuery += " FROM " + this.IdentifierStart + tableReferenceD
public override SqlServerQueryBuilder From(Dictionary<string, string> tableReferenceDictionary)
{
this.SelectQuery += " FROM " + this.IdentifierStart + tableReferenceDictionary["Database"] + this.IdentifierEnd
+ "." + this.IdentifierStart + tableReferenceDictionary["Schema"] + this.IdentifierEnd
+ "." + this.IdentifierStart + tableReferenceDictionary["Table"] + this.IdentifierEnd;
return this;
}
public覆盖来自(字典表引用字典)的SqlServerQueryBuilder
{
this.SelectQuery+=“FROM”+this.IdentifierStart+tableReferenceDictionary[“Database”]+this.IdentifierEnd
+“+this.IdentifierStart+tableReferenceDictionary[“Schema”]+this.IdentifierEnd
+““+this.IdentifierStart+tableReferenceDictionary[“Table”]+this.IdentifierEnd;
归还这个;
}
此方法采用Dictionary参数,并对使用的键进行假设。如果调用方传递了一个输入错误(“数据库”而不是“数据库”),那么这段代码将抛出一个异常。有没有更好的管理方法
可能是实现相同功能的不同数据类型
可能使用枚举并使类型为enum(
Dictionary
)的键?此处的自定义类更合适,因此您可以强制按预期填充所有“键”(将是新类中的属性)。是的,您是正确的。使用Dictionary
可能是最好的方法。是的,我认为自定义类比Dictionary更合适。一个具有3个字段的构造函数的类可以说我有一个SqlQuerybuilder类为应用程序的其余部分构建Sql。该类是否应该是SqlQueryBuilder中的嵌套类?与SqlQueryBuilder.TableReferenceIf类似,如果它仅对SqlQueryBuilder有用,则嵌套类在这种情况下有效。