LiteRepository 是一个访问数据库的新类。它通过 LiteDatabase 实现,实际上只是一个不用 LiteCollection 类和 fluent query 而快速访问数据的封装层。

    1. using(var db = new LiteRepository(connectionString))
    2. {
    3. // 简单访问,Insert/Update/Upsert/Delete
    4. db.Insert(new Product { ProductName = "Table", Price = 100 });
    5. db.Delete<Product>(x => x.Price == 100);
    6. // 使用 fluent query 查询
    7. var result = db.Query<Order>()
    8. .Include(x => x.Customer) // add dbref 1x1
    9. .Include(x => x.Products) // add dbref 1xN
    10. .Where(x => x.Date == DateTime.Today) // 使用索引的查询
    11. .Where(x => x.Active) // used indexes query
    12. .ToList();
    13. var p = db.Query<Product>()
    14. .Where(x => x.ProductName.StartsWith("Table"))
    15. .Where(x => x.Price < 200)
    16. .Limit(10)
    17. .ToEnumerable();
    18. var c = db.Query<Customer>()
    19. .Where(txtName.Text != null, x => x.Name == txtName.Text) // conditional filter
    20. .ToList();
    21. }

    集合名称可以省略,通过新的 BsonMapper.ResolveCollectionName 函数解析获得 (默认:typeof(T).Name)。

    这个 API 受启发于这个牛X项目NPoco Micro-ORM