.Net框架中是否有一个内置类可以用来表示AnsiString?
对于 dapper,我需要构建对传入 AnsiString 参数的支持。
数据库同时具有 unicode 和非 unicode 字符串,选择正确的参数类型有时至关重要。
特定参数的 DbType.String
与 DbType.AnsiString
会严重影响性能。
在 dapper 中,我们动态传递参数,例如:
Query<User>("select * from Users where Name=@Name", new {Name = "name"});
我有一个内部映射,它表示如果我看到 typeof(String)
我知道将参数作为 DbType.String
传递>
但是,我希望我的用户能够指示该字符串应该是 AnsiString。匿名类不支持属性,因此我需要一个不同的类型。
显然我可以发明一个:
public class AnsiString
{
private readonly string str;
public AnsiString(string str)
{
this.str = str;
}
public String Value { get { return str; } }
}
这将为我提供干净的 API:
Query<User>("select * from Users where Name=@Name",
new {Name = new AnsiString("name")});
但是,如果 System.Data 或 BCL 中存在这样的类,为什么还要发明一个呢?
BCL 或 System.Data 中是否有某种类型可以用作 AnsiString
的容器,其语义与上面的示例类似?
For dapper I need to build support for passing in AnsiString params.
Databases have both unicode and non-unicode strings, picking the right parameter type is sometimes crucial.
DbType.String
vs DbType.AnsiString
for a particular param can heavily effect perf.
In dapper we pass in parameters dynamically, Eg:
Query<User>("select * from Users where Name=@Name", new {Name = "name"});
I have an internal map that says that if I see typeof(String)
I know to pass in the param as a DbType.String
However, I would like my users to be able to denote that the string should be an AnsiString. Attributes are not supported for anonymous classes, so I need a distinct type for this.
Clearly I can invent one:
public class AnsiString
{
private readonly string str;
public AnsiString(string str)
{
this.str = str;
}
public String Value { get { return str; } }
}
Which would give me the clean API:
Query<User>("select * from Users where Name=@Name",
new {Name = new AnsiString("name")});
However, why invent one if such a class exists in System.Data or the BCL.
Is there a type somewhere in the BCL or System.Data
I could use as a container for AnsiString
, with similar semantics to the sample above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
BCL 或 System.Data 中没有这样的类,您必须自己编写。
我们选择了自定义类型,以最终提供更细粒度的自定义;该测试显示了典型用法:
So;它支持:
类型可用dapper 内。
There is not such class in the BCL or System.Data, you will have to roll your own.
We went with a custom type to provide more fine-grained customn in the end; this test shows typical usage:
So; it supports:
The type is available inside dapper.
我想您可以将
string
用于DbType.String
,将char[]
用于DbType.AnsiString
。它看起来与您当前的代码非常相似:
或者如果您想使用 AnsiString,您可以创建扩展方法
.ToAnsiString()
:I guess you can use
string
forDbType.String
andchar[]
forDbType.AnsiString
.It will look very similar to your current code:
Or if you wan use your AnsiString, you can create extension method
.ToAnsiString()
: