当前位置: 首页 > news >正文

html5响应式网站psd物语系列短篇资源WordPress

html5响应式网站psd,物语系列短篇资源WordPress,四川餐饮网站建设,黑龙江建设网官网手机版PetaPoco是一款适用于.Net 和Mono的微小、快速、单文件的微型ORM。 PetaPoco有以下特色#xff1a; 微小#xff0c;没有依赖项……单个的C#文件可以方便的添加到任何项目中。工作于严格的没有装饰的Poco类#xff0c;和几乎全部加了特性的Poco类Insert/Delete/Update/Save …PetaPoco是一款适用于.Net 和Mono的微小、快速、单文件的微型ORM。 PetaPoco有以下特色 微小没有依赖项……单个的C#文件可以方便的添加到任何项目中。工作于严格的没有装饰的Poco类和几乎全部加了特性的Poco类Insert/Delete/Update/Save and IsNew 等帮助方法。分页支持自动得到总行数和数据支持简单的事务更好的支持参数替换包括从对象属性中抓取命名的参数。很好的性能剔除了Linq并通过Dynamic方法快速的为属性赋值T4模板自动生成Poco类查询语言是Sql……不支持别扭的fluent或Linq语法仁者见仁智者见智包含一个低耦合的Sql Builder类让内联的Sql更容易书写为异常信息记录、值转换器安装和数据映射提供钩子。Hooks for logging exceptions, installing value converters and mapping columns to properties without attributes.兼容SQL Server, SQL Server CE, MySQL, PostgreSQL and Oracle。可以在.NET 3.5 或Mono 2.6或更高版本上运行在.NET 4.0 和Mono 2.8下支持dynamicNUnit单元测试开源Apache License所有功能大约用了1500行代码  可以从这里获得PetaPoco NuGet - http://nuget.org/List/Packages/PetaPocoGitHub - https://github.com/toptensoftware/petapoco  代码展示 首先定义一个Poco类 // Represents a record in the articles table public class article {public long article_id { get; set; }public string title { get; set; }public DateTime date_created { get; set; }public bool draft { get; set; }public string content { get; set; } } 接下来创建一个PetaPoco.Database来执行查询 // Create a PetaPoco database object var dbnew PetaPoco.Database(connectionStringName);// Show all articles foreach (var a in db.Queryarticle(SELECT * FROM articles)) {Console.WriteLine({0} - {1}, a.article_id, a.title); } 得到一个scalar long countdb.ExecuteScalarlong(SELECT Count(*) FROM articles); 得到一行记录 var a db.SingleOrDefaultarticle(SELECT * FROM articles WHERE article_id0, 123));   获取分页数据 PetaPoco能够自动完成分页请求 var resultdb.Pagearticle(1, 20, // -- page number and items per pageSELECT * FROM articles WHERE category0 ORDER BY date_posted DESC, coolstuff); 你将会得到一个PagedFetch对象 public class PageT where T:new() {public long CurrentPage { get; set; }public long ItemsPerPage { get; set; }public long TotalPages { get; set; }public long TotalItems { get; set; }public ListT Items { get; set; } } PetaPoco在背后为我们做了一下处理 生成并执行一个查询得到匹配的数据行数修改原始的查询语句使其只得到所有匹配数据的一个子集你现在已经拥有了一个展示单页数据的一切东西和一个分页控制器他们被封装在一个现成的小对象中。You now have everything to display a page of data and a pager control all wrapped up in one handy little object!   Query vs Fetch Database 对象有两个获取数据的方法Query 和Fetch。这两个方法非常相似不同的是Fetch方法返回一个POCO类的List而Query使用 yield return 迭代所有数据这些数据并没有加载到内存中。   不带查询的命令 使用Execute 方法执行一个不带查询的命令 db.Execute(DELETE FROM articles WHERE draft0);   Inserts、Updates 和 Deletes PetaPoco提供了insert、update和delete操作的帮助。 在插入一条记录时你需要指定插入的表名和主键 // Create the article var anew article(); a.titleMy new article; a.contentPetaPoco was here; a.date_createdDateTime.UtcNow;// Insert it db.Insert(articles, article_id, a);// by now a.article_id will have the id of the new article 更新记录也一样 // Get a record var adb.SingleOrDefaultarticle(SELECT * FROM articles WHERE article_id0, 123);// Change it a.contentPetaPoco was here again;// Save it db.Update(articles, article_id, a); 或者你可以传一个匿名类来更新一部分字段。下面的代码只更新article的title字段 db.Update(articles, article_id, new { titleNew title }, 123); 删除 // Delete an article extracting the primary key from a record db.Delete(articles, article_id, a);// Or if you already have the ID elsewhere db.Delete(articles, article_id, null, 123);   修饰POCO类 在上面的例子中必须指明表名和主键是很烦人的你可以在你的Poco类中附加这些信息 // Represents a record in the articles table [PetaPoco.TableName(articles)] [PetaPoco.PrimaryKey(article_id)] public class article {public long article_id { get; set; }public string title { get; set; }public DateTime date_created { get; set; }public bool draft { get; set; }public string content { get; set; } } 简化后的insert、update、delete // Insert a record var anew article(); a.titleMy new article; a.contentPetaPoco was here; a.date_createdDateTime.UtcNow; db.Insert(a);// Update it a.contentBlah blah; db.Update(a);// Delete it db.Delete(a); delete和update的其它方式 // Delete an article db.Deletearticle(WHERE article_id0, 123);// Update an article db.Updatearticle(SET title0 WHERE article_id1, New Title, 123); 你还可以告诉POCO忽略某列 public class article {[PetaPoco.Ignore]public long SomeCalculatedFieldPerhaps{ get; set; } } 或许你喜欢一点更详细的描述。和自动映射所有列相比你可以通过使用类和列的属性来指明哪些列需要映射。 // Represents a record in the articles table [PetaPoco.TableName(articles)] [PetaPoco.PrimaryKey(article_id)] [PetaPoco.ExplicitColumns] public class article {[PetaPoco.Column]publiclong article_id { get; set;}[PetaPoco.Column]publicstring title { get; set;}[PetaPoco.Column]publicDateTime date_created { get; set;}[PetaPoco.Column]public bool draft { get; set;}[PetaPoco.Column]publicstring content { get; set;} }  它可以结合partial class 很好的工作把需要绑定的字段放在一个.cs文件中把计算得到的和别的有用的属性添加到分开的文件中而不用去考虑DAL。 Hey是不是已经有装饰POCO数据库的标准属性了呢 好吧PetaPoco仅支持少数几个因为我不想引起混乱。 Hey稍等……它们不是POCO对象了 当然它们打破了严格的POCO概念但使用它们可以让POCO更容易工作。 T4 模板 Writing all those POCO objects can soon get tedious and error prone... so PetaPoco includes a T4 template that can automatically write classes for all the tables in your your SQL Server, SQL Server CE, MySQL, PostgreSQL or Oracle database. Using the T4 template is pretty simple. The git repository includes three files (The NuGet package adds these to your project automatically in the folder \Models\Generated). PetaPoco.Core.ttinclude - includes all the helper routines for reading the DB schemaPetaPoco.Generator.ttinclude - the actual template that defines whats generatedDatabase.tt - the template itself that includes various settings and includes the two other ttinclude files.A typical Database.tt file looks like this: # include filePetaPoco.Core.ttinclude ##    // Settings    ConnectionStringNamejab;    NamespaceConnectionStringName;    DatabaseNameConnectionStringName;    stringRepoNameDatabaseNameDB;    bool GenerateOperationstrue;    // Load tables    var tables LoadTables();## include filePetaPoco.Generator.ttinclude # To use the template: Add the three files to you C# projectMake sure you have a connection string and provider name set in your app.config or web.config fileEdit ConnectionStringName property in Records.tt (ie: change it from jab to the name of your connection string)Save Database.tt.All going well Database.cs should be generated with POCO objects representing all the tables in your database. To get the project to build youll also need to add PetaPoco.cs to your project and ensure it is set to compile (NuGet does this for you) . The template is based on the SubSonic template. If youre familiar with its ActiveRecord templates youll find PetaPocos template very similar. 自动的Select语句 当使用PetaPoco时大多数查询都以SELECT * FROM table开头。鉴于我们现在可以从POCO对象的attribute中得到表名我们没有理由不自动生成Select语句。 如果你运行一个不以select开头的查询 PetaPoco会自动的将它加上 // Get a record var adb.SingleOrDefaultarticle(SELECT * FROM articles WHERE article_id0,123); 可以简写 // Get a recordvar adb.SingleOrDefaultarticle(WHERE article_id0,123); PetaPoco实际上并不生成SELECT *它更准确的得到要查询的列名。 IsNew 和Save 方法 有时你有一个POCO你想要知道数据库中是否已经存在。因为我们有主键我们所要做的检查是该属性被设置为别的值还是默认值。 检测是否为新增: // Is this a new record if(db.IsNew(a)){    // Yes it is...} 和它相关联还有一个Save方法它将根据判断的结果执行Insert或Update。 // Save a new or existing recorddb.Save(a); 事务 事务相当的简单: using (var scopedb.Transaction){    // Do transacted updates here    // Commit    scope.Complete();} 事务可以是嵌套的因此你可以调用其它包含事务的方法或者被包含在单个的事务中。当所有事务都执行完成了事务将会提交否则所有操作都将回滚。 注意为了使用事务所有操作都需要相同的PetaPoco Database对象实例。你很可能想到IOC容器中为在每一个http请求或每一个线程共享同一个实体我喜欢用StructureMap。 Linq从哪儿实现 没有任何支持。我在Subsonic中使用Linq很长时间我发现自己下降到使用CodingHorror 来做这些事情因为  不能用简单的Linq实现 在.NET 下工作但不支持Mono尤其是Mono 2.6低效。例如Subsonic 中activerecord.SingleOrDefault(xx.id123)的效率比CodingHorror 低20倍。  Now that Ive got CodingHorror all over the place it bugs me that half the code is Linq and half is SQL. Also, Ive realized that for me the most annoying thing about SQL directly in the code is not the fact that its SQL but that its nasty to format nicely and to build up those SQL strings. So... PetaPocos SQL Builder 目前已经有很多构建SQL的API以下是我的版本它确实很基础 我的目标是格式化SQL更简单并且通过适当的参数替换达到防止SQL注入的作用。这不能保证SQL语法的正确也不支持使用intellisense。 以下是非常基础的写法 var id123;var adb.Queryarticle(PetaPoco.Sql.Builder    .Append(SELECT * FROM articles)    .Append(WHERE article_id0, id)) 很管用吧参数索引器来通过调用.Append是多么酷啊【Big deal right? Well whats cool about this is that the parameter indicies are specific to each .Append call:】 var id123;var adb.Queryarticle(PetaPoco.Sql.Builder    .Append(SELECT * FROM articles)    .Append(WHERE article_id0, id)    .Append(AND date_created0,DateTime.UtcNow)) 你也可以根据条件构建SQL var id123;var sqlPetaPoco.Sql.Builder    .Append(SELECT * FROM articles)    .Append(WHERE article_id0, id);if(start_date.HasValue)    sql.Append(AND date_created0, start_date.Value);if(end_date.HasValue)    sql.Append(AND date_created0, end_date.Value);var adb.Queryarticle(sql) 注意到每个append调用都用到餐厨0了吗PetaPoco构建整个列表的参数将这些参数索引更新到内部。 你也可以使用命名参数然后他会在传递的参数中找到合适的属性名。 sql.Append(AND date_createdstart AND date_createdend,                 new                 {                     startDateTime.UtcNow.AddDays(-2),                     endDateTime.UtcNow                 }            ); 不管是数字的还是命名的参数如果任何一个参数不能被推断出来都会抛出一个异常。 这里还有几个创建SQL的公用方法 var sqlPetaPoco.Sql.Builder()            .Select(*)            .From(articles)            .Where(date_created 0,DateTime.UtcNow)            .OrderBy(date_created DESC); 跟踪Sql命令 有些时候能够看到执行的Sql语句会非常有用PetaPoco为此提供了三个属性 LastSQL - 非常明显不解释LastArgs - 传递的参数数组LastCommand - SQL语句和参数字符串在调试器中查看LastCommand属性能够简单的看到执行了那些操作 OnException Handler Routine PetaPoco所执行的Sql命令都封装在try/catch语句块中所有的异常信息都会传递给OnException虚方法。通过记录这些异常或在这个方法中设置断点你可以轻松的跟踪那些地方出现了问题。 More 上面的内容展示了最基本的PetaPoco使用方法想了解更多请查看这些博客内容。   原文地址http://www.toptensoftware.com/petapoco/  转载于:https://www.cnblogs.com/youring2/archive/2012/06/04/2532130.html
http://www.lebaoying.cn/news/57990.html

相关文章:

  • 商丘市网站建设推广个人如何做微信下单小程序
  • 淘宝网站建设费用网站建设与维护1997年
  • 做家电网站好新农村建设在哪个网站申请
  • 网站建设申请表做网站那里好
  • 如何提升网站速度用iis搭建网站
  • 手机网站你们php做网站很快嘛
  • 网站可以多个域名吗巴中哪里做网站
  • 做钢材的都用什么网站模板建站平台
  • 建设团购网站费用wordpress侧面小工具
  • 金顶街网站建设动态 静态 网站地图
  • 企业网站建设指导规范如何添加网站logo
  • 网站是哪个建站公司做的网站备案 核验单
  • 上传下载文件网站开发的php源码网站开发定位
  • 淘客免费交易网站建设手机wap网站特效
  • 宣城市建设监督管理局网站首页活动网站推广
  • 网站开发html php天津网站优化步骤
  • 做影视后期应该关注哪些网站怎么设计logo用什么软件
  • 兰州网站制作公司哪个好蓝天下摄影作品排列网页模板
  • 单机网页游戏网站免费网页游戏网站
  • 诸城公司做网站win7如何做网站服务器
  • 网站制作新手教程视频网站 站长统计代码
  • 如何做网站活动封面百度论坛
  • 网站重新备案需要多长时间幼教网站模板
  • 网站的查询系统怎么做php网站访问很慢
  • 专门培训seo的网站frontpage怎么制作网页
  • 女网友叫我一起做优惠券网站wordpress标签搜索
  • 平面设计公司网站会展门户网站源码
  • 建设部咨询资质网站怎样使用wordpress模板
  • 梦幻创意北京网站建设外贸网站seo推广教程
  • 网站付费模板查看网站是否做百度推广