ServiceStack.OrmLite 笔记8 -还是有用的姿势
复杂点的使用2
InsertAll, UpdateAll and DeleteAll 的参数要是IEnumerables
Each关键字 返回 IEnumerable 并且是延迟加载的
全局设置 当字符串为null时候,设置一个默认值
OrmLiteConfig.OnDbNullFilter = fieldDef = >
fieldDef.FieldType == typeof(string) ? "NULL" : null;
执行sql的时候过滤
public class MockStoredProcExecFilter : OrmLiteExecFilter
{
public override T Exec
{
try
{
return base.Exec(dbConn, filter);
}
catch (Exception ex)
{
if (dbConn.GetLastSql() == "exec sp_name @firstName, @age")
return (T)(object)new Person { FirstName = "Mocked" };
throw;
}
}
}
OrmLiteConfig.ExecFilter = new MockStoredProcExecFilter();
using (var db = OpenDbConnection())
{
var person = db.SqlScalar
new { firstName = "aName", age = 1 });
person.FirstName.Print(); //Mocked
}
CaptureSqlFilter
跟踪 可以在调试窗口看到了
using (var captured = new CaptureSqlFilter())
using (var db = OpenDbConnection())
{
db.Where
captured.SqlStatements[0].PrintDump();
}
同上 重复执行
public class ReplayOrmLiteExecFilter : OrmLiteExecFilter
{
public int ReplayTimes { get; set; }
public override T Exec<T>(IDbConnection dbConn, Func<IDbCommand, T> filter)
{
var holdProvider = OrmLiteConfig.DialectProvider;
var dbCmd = CreateCommand(dbConn);
try
{
var ret = default(T);
for (var i = 0; i < ReplayTimes; i++)
{
ret = filter(dbCmd);
}
return ret;
}
finally
{
DisposeCommand(dbCmd);
OrmLiteConfig.DialectProvider = holdProvider;
}
}
}
OrmLiteConfig.ExecFilter = new ReplayOrmLiteExecFilter { ReplayTimes = 3 };
using (var db = OpenDbConnection())
{
db.DropAndCreateTable
db.Insert(new PocoTable { Name = "Multiplicity" });
var rowsInserted = db.Count<PocoTable>(q => q.Name == "Multiplicity"); //3
}
单元测试 模拟返回的数据
下面返回的数据都是假的,山寨的
using (new OrmLiteResultsFilter {
PrintSql = true,
SingleResult = new Person {
Id = 1, FirstName = "Mocked", LastName = "Person", Age = 100
},
})
{
db.Single
db.Single(db.From
db.Single
db.Single
}
一看就懂 字符串过滤 还有这玩意居然
OrmLiteConfig.StringFilter = s => s.TrimEnd();
db.Insert(new Poco { Name = "Value with trailing " });
db.Select
可插入 可拔出的 复杂类型序列化
据说ServiceStack's的json序列化效率最高
//ServiceStack's JSON and JSV Format
SqliteDialect.Provider.StringSerializer = new JsvStringSerializer();
PostgreSqlDialect.Provider.StringSerializer = new JsonStringSerializer();
//.NET's XML and JSON DataContract serializers
SqlServerDialect.Provider.StringSerializer = new DataContractSerializer();
MySqlDialect.Provider.StringSerializer = new JsonDataContractSerializer();
//.NET XmlSerializer
OracleDialect.Provider.StringSerializer = new XmlSerializableSerializer();
code first的时候会用到吧
public class PocoTable
{
public int Id { get; set; }
[CustomField("CHAR(20)")]
public string CharColumn { get; set; }
[CustomField("DECIMAL(18,4)")]
public decimal? DecimalColumn { get; set; }
}
db.CreateTable
结果:CREATE TABLE "PocoTable"
(
"Id" INTEGER PRIMARY KEY,
"CharColumn" CHAR(20) NULL,
"DecimalColumn" DECIMAL(18,4) NULL
);
可以这样调用存储过程的
List
List
"EXEC GetAnalyticsForWeek @weekNo", new { weekNo = 1 });
List
List
"EXEC GetTotalsForWeek @weekNo", new { weekNo = 1 });
int result = db.SqlScalar
搞个带参数的存储过程
string spSql = @"DROP PROCEDURE IF EXISTS spSearchLetters;
CREATE PROCEDURE spSearchLetters (IN pLetter varchar(10), OUT pTotal int)
BEGIN
SELECT COUNT(*) FROM LetterFrequency WHERE Letter = pLetter INTO pTotal;
SELECT * FROM LetterFrequency WHERE Letter = pLetter;
END";
db.ExecuteSql(spSql);
using (var cmd = db.SqlProc("spSearchLetters", new { pLetter = "C" }))
{
var pTotal = cmd.AddParam("pTotal", direction: ParameterDirection.Output);
var results = cmd.ConvertToList<LetterFrequency>();
var total = pTotal.Value;
}
换个姿势加参数
IDbDataParameter pTotal = null;
var results = db.SqlList
cmd.CommandType = CommandType.StoredProcedure;
cmd.AddParam("pLetter", "C");
pTotal = cmd.AddParam("pTotal", direction: ParameterDirection.Output);
});
var total = pTotal.Value;
设置字段的默认值 g点在这里OrmLiteVariables
public class Poco
{
[Default(OrmLiteVariables.SystemUtc)] //= {SYSTEM_UTC}
public DateTime CreatedTimeUtc { get; set; }
}
作者:过错
出处:http://www.cnblogs.com/wang2650/
关于作者:net开发做的久而已。十余年时光虚度!
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。如有问题,可以邮件:wang2650@163.com
联系我,非常感谢。