当前位置: 首页> 文旅> 酒店 > 前端开发工程师的工作内容_沈阳妇科医院哪家好_石家庄seo关键词_好搜网惠州seo

前端开发工程师的工作内容_沈阳妇科医院哪家好_石家庄seo关键词_好搜网惠州seo

时间:2025/7/26 20:21:35来源:https://blog.csdn.net/weixin_43632687/article/details/142759147 浏览次数:0次
前端开发工程师的工作内容_沈阳妇科医院哪家好_石家庄seo关键词_好搜网惠州seo

LiteDB介绍

LiteDB 是一个小巧、快速和轻量级的 .NET NoSQL 嵌入式数据库。

  • 无服务器的 NoSQL 文档存储
  • 简单的 API,类似于 MongoDB
  • 100% 的 C# 代码支持 .NET 4.5 / NETStandard 1.3/2.0,以单个 DLL(不到 450KB)形式提供
  • 线程安全
  • 支持 ACID,完整的事务支持
  • 写入失败后的数据恢复(WAL 日志文件)
  • 使用 DES(AES)加密算法对数据文件进行加密
  • 使用属性或流畅的映射器 API 将 POCO 类映射为 BsonDocument
  • 存储文件和流数据(类似于 MongoDB 的 GridFS)
  • 单一数据文件存储(类似于 SQLite)
  • 对文档字段建立索引以实现快速搜索
  • 支持 LINQ 查询
  • 提供类似于 SQL 的命令来访问/转换数据
  • LiteDB Studio - 数据访问的精美用户界面
  • 开源且免费供所有人使用,包括商业用途

nuget安装

dotnet add package LiteDB --version 5.0.21

BsonRef定义关联关系

using LiteDB;namespace LiteDBAPI.Models
{public class Customer:BaseClass{[BsonField("customername")]public string Name { get; set; }}public class Order: BaseClass{[BsonField("price")]public double Price { get; set; }[BsonRef("customers")]public Customer Customer { get; set; }}public class BaseClass{[BsonId]public ObjectId ID { get; set; }}
}

封装helper

using LiteDB;
using LiteDBAPI.Models;
using System.Linq.Expressions;namespace LiteDBAPI
{public class LiteDBHelper{public readonly LiteDatabase db;public LiteDBHelper(){db = new LiteDatabase("Filename=database.db;Password=1234;Connection=shared");}public BsonValue insert<T>(T value,string collectionName){// Get a collection (or create, if doesn't exist)var col = db.GetCollection<T>(collectionName);// Insert new customer document (Id will be auto-incremented)return col.Insert(value); }public T getOrder<T, TRelated>(string orderid,string collectionName, Expression<Func<T, TRelated>> includeExpression) where T : BaseClass{ObjectId objectId = new ObjectId(orderid);var col = db.GetCollection<T>(collectionName);return col.Query().Where(x =>x.ID== objectId).Include(includeExpression).FirstOrDefault();}public bool update<T>(T instance,string collectionName){var col = db.GetCollection<T>(collectionName);return col.Update(instance);}public List<T> queryByCondition<T>(string collectionName,Dictionary<string,string> keyValuePairs){var col = db.GetCollection<T>(collectionName);// Create a list to hold individual query expressionsvar queryList = new List<BsonExpression>();BsonExpression? combinedQuery = null;// Loop through the key-value pairs and create equality conditionsforeach (var pair in keyValuePairs){queryList.Add(Query.EQ(pair.Key, pair.Value));}if (queryList.Count>1){// Combine the individual conditions into an AND querycombinedQuery = Query.And(queryList.ToArray());}else{combinedQuery = queryList[0];}// Execute the query and return the results as a listreturn col.Find(combinedQuery).ToList();}}
}

controller使用

using LiteDB;
using LiteDBAPI.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;namespace LiteDBAPI.Controllers
{[Route("api/[controller]/[action]")][ApiController]public class LiteDBController : ControllerBase{private readonly LiteDBHelper db;public LiteDBController(LiteDBHelper db){this.db = db;}[HttpPost]public IActionResult insert([FromBody] string customername){Customer customer = new Customer() { Name = customername };// customerthis.db.insert(customer, "customers");// orderthis.db.insert(new Order() { Customer=customer }, "orders");return Ok(new { result="success"});}[HttpGet]public Order getOrder([FromQuery] string orderid){return db.getOrder<Order,Customer>(orderid,"orders",x=>x.Customer);}[HttpPut]public IActionResult updatePrice([FromQuery] string orderid){Order order = db.getOrder<Order, Customer>(orderid, "orders", x => x.Customer);order.Price =order.Price + 100;return Ok(db.update<Order>(order, "orders"));}[HttpPost]public List<Customer> getCustomer([FromBody] Dictionary<string,string> keyValuePairs){return db.queryByCondition<Customer>("customers",keyValuePairs);}}
}

加密

通过更改连接参数,添加password实现

官网

代码

关键字:前端开发工程师的工作内容_沈阳妇科医院哪家好_石家庄seo关键词_好搜网惠州seo

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: