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(IDbConnection dbConn, Func<IDbCommand, T> filter)
{
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("exec sp_name @firstName, @age",
new { firstName = "aName", age = 1 });
person.FirstName.Print(); //Mocked
}
CaptureSqlFilter
跟踪 可以在调试窗口看到了
using (var captured = new CaptureSqlFilter())
using (var db = OpenDbConnection())
{
db.Where(new { Age = 27 });
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(x => x.Age == 42).FirstName // Mocked
db.Single(db.From().Where(x => x.Age == 42)).FirstName // Mocked
db.Single(new { Age = 42 }).FirstName // Mocked
db.Single("Age = @age", new { age = 42 }).FirstName // Mocked
}
一看就懂 字符串过滤 还有这玩意居然
OrmLiteConfig.StringFilter = s => s.TrimEnd();
db.Insert(new Poco { Name = "Value with trailing " });
db.Select().First().Name // "Value with trailing"
可插入 可拔出的 复杂类型序列化
据说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 results = db.SqlList("EXEC GetAnalyticsForWeek 1");
List results = db.SqlList(
"EXEC GetAnalyticsForWeek @weekNo", new { weekNo = 1 });
List results = db.SqlList("EXEC GetTotalsForWeek 1");
List results = db.SqlList(
"EXEC GetTotalsForWeek @weekNo", new { weekNo = 1 });
int result = db.SqlScalar("SELECT 10");
搞个带参数的存储过程
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("spSearchLetters", cmd => {
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; }
}
ServiceStack.OrmLite 笔记8 -还是有用的姿势的更多相关文章
- ServiceStack.OrmLite 笔记2 -增
ServiceStack.OrmLite 笔记2 这篇主要介绍 增加 db.Insert(new Employee { Id = 1, Name = "Employee 1" }) ...
- ServiceStack.OrmLite 笔记
ServiceStack.OrmLite 笔记1 ServiceStack.OrmLite 这个东东就是个orm框架,可以实现类似ef的效果.具体的就不这里班门弄斧了. 支持 SqlServerDia ...
- ServiceStack.OrmLite 笔记9 -code first 必须的代码优先
复杂点的使用3 code first的使用,支持复杂类型 public enum PhoneType { Home, Work, Mobile, } public enum AddressType { ...
- ServiceStack.OrmLite 笔记4 删
删除 db.DeleteAll(); //各种姿势 db.Delete(p => p.Age == 27);// db.Delete(q => q.Where(p => p.Age ...
- ServiceStack.OrmLite 笔记10-group having 分页orderby等
group having 分页等 var ev = OrmLiteConfig.DialectProvider.SqlExpression(); group的使用 同sql一样,注意group分组的字 ...
- ServiceStack.OrmLite 笔记5 改
修改 db.Update(new Person { Id = 1, FirstName = "Jimi", LastName = "Hendrix", Age ...
- ServiceStack.OrmLite 学习笔记7-复杂点的使用1
复杂点的使用1 先看看这2个类 class Customer { public int Id { get; set; } ... } class CustomerAddress { public in ...
- ServiceStack.OrmLite
ServiceStack.OrmLite 谈谈我的入门级实体框架Loogn.OrmLite 每次看到有新的ORM的时候,我总会留意一下,因为自己也写过一个这样的框架,人总是有比较之心的.我可能会d ...
- ServiceStack.OrmLite中的一些"陷阱"(2)
注:此系列不是说ServiceStack.OrmLite的多个陷阱,这仅仅个人认为是某一个陷阱(毕竟我踩坑了)而引发的思考. 前文说到了项目需要使用两种不同的数据库语言,虽说前文问题已基本解决了,但是 ...
随机推荐
- Nginx+Keepalived实现 转载
一.Keepalived简介 keepalived是一个类似于layer3, 4 & 5交换机制的软件,也就是我们平时说的第3层.第4层和第5层交换.Keepalived的作用是检测web服务 ...
- javaWeb 使用jsp开发 foreach 标签
1.jsp代码 测试数据 <% List<String> list = new ArrayList<String>(); list.add("aaa" ...
- Postgres-XL介绍
一.什么是Postgres-XL XL的意思是:eXtensible Lattice,可以扩展的格子,即将PostgreSQL应用在多机器上的分布式数据库的形象化表达. Postgres-XL 是一个 ...
- 搭建LAMP
RPM包和源码包存放位置 /usr/local/src 源码包编译安装位置 /usr/local/apache /usr/local/mysql /usr/local/php 默认MySQL 数据库位 ...
- Javascript页面之间参数传递 (前端)
一.来源:tongfang [系统管理员] --[系统管理] 的"SysLeftNavView.ascx.cs 用户插件 usercontrol 左侧菜单导航: <li>< ...
- 自己封装的OKhttp请求
package com.create.qdocumentimtest.rxjavatest; import com.squareup.okhttp.Callback; import com.squar ...
- idea项目无法自动导入maven库
解决:idea版本由15.0.*(需要Java8)降为14.1.4(Java7即可),搞定......
- ACM题目————二叉树的遍历
一.二叉树的后序遍历: 题目描述 给定一颗二叉树,要求输出二叉树的深度以及后序遍历二叉树得到的序列.本题假设二叉树的结点数不超过1000 输入 输 入数据分为多组,第一行是测试数据的组数n,下面的n行 ...
- 64位系统安装ODBC驱动的方法
为了更充分的利用硬件资源,我想很多人都开使用64位操作系统了,同时你可以也发现了在64位操作系统上ODBC的驱动找不到了,所以ODBC的东西都没法用了. 因为2007以前版本的Office只有32位版 ...
- 【转】SVN提示:由于目标机器积极拒绝,无法连接 的解决方法
转载地址:http://wxiaolei.blog.163.com/blog/static/1038760120133108180596/ 安装完TSVN之后,checkout时报错,并且后来在cmd ...