轻量级ORM框架PetaPoco

1、简介

PetaPoco是一个小型、快速、单文件的微型ORM(Object Relational Mapper)框架,可在.NET和Mono环境运行。

https://github.com/CollaboratingPlatypus/PetaPoco

2、特点

PetaPoco 是一个用于 .NET 的小型且快速的微型 ORM

  • 与Dapper一样,它很快,因为它使用动态方法生成 (MSIL) 将列值分配给属性

  • 像Massive一样,它现在也支持动态 Expandos

  • 与ActiveRecord一样,它支持对象和数据库表之间的密切关系

  • 与SubSonic一样,它支持使用 T4 模板生成 poco 类(仅限 V5)

  • 与Massive一样,它以单个文件的形式提供,您可以轻松地将其添加到任何项目或编译。(仅限 V5)

3、功能

  • 很小,绝对没有依赖!

  • 异步或同步,选择权在您。(又名异步)(V6)

  • 与严格未修饰的 POCO 或归属的几乎 POCO 一起使用。

  • 易于配置,包括开箱即用的流畅配置。

  • 插入/删除/更新/保存和 IsNew 的辅助方法

  • 分页请求会自动计算出总记录数并获取特定页面。

  • 简单的交易支持。

  • 更好的参数替换支持,包括从对象属性中获取命名参数。

  • 通过使用 DynamicMethod 生成消除 Linq 和快速属性分配来获得出色的性能。

  • 查询语言是好的 ole SQL。

  • 包括一个低摩擦的 SQL 构建器类,使编写内联 SQL变得更加容易。

  • 包括 T4 模板以自动为您生成 POCO 类。(V5)

  • 用于记录异常、安装值转换器和将列映射到没有属性的属性的挂钩。

  • 适用于 SQL Server、SQL Server CE、MS Access、SQLite、MySQL、MariaDB、Firebird 和 PostgreSQL。(Oracle 支持但没有集成测试)。

  • 在 Net Standard 2.0、.NET 4.0/4.5+ 或 Mono 2.8 及更高版本下工作。

  • 有Xunit单元测试。

  • 已支持 DBs 集成测试。

  • 开源(MIT 许可证或 Apache 2.0)

4、简单例子

添加

[Fact]
    public void Insert()
    {
        // Create the person
        var person = new Person { Id = Guid.NewGuid(), Name = "PetaPoco", Dob = new DateTime(2011, 1, 1), Age = (DateTime.Now.Year - 2011), Height = 242 };


        // Tell PetaPoco to insert it
        var id = DB.Insert(person);


        // Obviously the ID returned will be the same as the one we set
        id.ShouldBe(person.Id);


        // Get a clone/copy from the DB
        var clone = DB.Single<Person>(id);


        // See, they're are the same
        clone.ShouldBe(person);


        // But, they're not not reference equals as PetaPoco doesn't cache because it's a MircoORM.
        person.Equals(clone).ShouldBeFalse();
    }

更新

// Create and insert the person
        var person = new Person { Id = Guid.NewGuid(), Name = "PetaPoco", Dob = new DateTime(2011, 1, 1), Age = (DateTime.Now.Year - 2011), Height = 242 };
        var id = DB.Insert(person);


        // Update a few properties of the person
        person.Age = 70;
        person.Name = "The PetaPoco";


        // Tell PetaPoco to update the DB
        DB.Update(person);


        // Get a clone/copy from the DB
        var clone = DB.Single<Person>(id);


        // See, the person has been updated
        clone.Id.ShouldBe(person.Id);
        clone.Dob.ShouldBe(person.Dob);
        clone.Height.ShouldBe(person.Height);
        clone.Age.ShouldBe(person.Age);
        clone.Name.ShouldBe(person.Name);

删除

// Create the person
        var person = new Person { Id = Guid.NewGuid(), Name = "PetaPoco", Dob = new DateTime(2011, 1, 1), Age = (DateTime.Now.Year - 2011), Height = 242 };


        // Tell PetaPoco to insert it
        DB.Insert(person);
        
        // Obviously, we find only 1 matching person in the db
        var count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [People] WHERE [Id] = @Id", new { person.Id });
        count.ShouldBe(1);
        
        // Tell PetaPoco to delete it
        DB.Delete(person);
        
        // Obviously, we should now have none in the db
        count = DB.ExecuteScalar<int>("SELECT COUNT([Id]) FROM [People] WHERE [Id] = @0", person.Id);
        count.ShouldBe(0);

查询

var inmemorylist = db.Fetch<article>("SELECT * FROM Articles") 
foreach (var a in inmemorylist)
{    Console.WriteLine($"{a.Id} - {a.Title}");
}Console.WriteLine($"Count: {inmemorylist.Count}");
foreach (var a in db.Query<article>("SELECT * FROM Articles"))
{    Console.WriteLine($"{a.Id} - {a.Title}");
}

更多方法见

https://github.com/CollaboratingPlatypus/PetaPoco/wiki/

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值