当前位置:网站首页> Asp.netcore利用dynamic简化数据库访问
Asp.netcore利用dynamic简化数据库访问
2022-07-01 12:59:00 【1024问】
今天写了一个数据库的帮助类,代码如下。
public static class DbEx{ public static dynamic ReadToObject(this IDataReader reader) { var obj = new DbObject(); for (int i = 0; i < reader.FieldCount; i++) { obj[reader.GetName(i)] = new DbField() { DbData = reader[i] }; } return obj; } public class DbObject : DynamicObject { //自己实现一个,不用ExpandoObject, 以支持无视大小写读取 public override bool TryGetMember(GetMemberBinder binder, out object result) { result = this[binder.Name]; return true; } Dictionary<string, object> _values = new Dictionary<string, object>(StringComparer.CurrentCultureIgnoreCase); public object this[string index] { get => _values[index]; set => _values[index] = value; } } public class DbField { public object DbData { get; set; } public T Value<T>() { return (T)Convert.ChangeType(DbData, typeof(T)); } public static implicit operator string(DbField data) => data.Value<string>(); public static implicit operator int(DbField data) => data.Value<int>(); public static implicit operator DateTime(DbField data) => data.Value<DateTime>(); public static implicit operator double(DbField data) => data.Value<double>(); public static implicit operator bool(DbField data) => data.Value<bool>(); }}简单的来讲,可以把如下代码
GpsData parse(IDataReader reader){ return new GpsData() { IsValid = (bool)reader["IsValid"], Location = new Location () { Lon = (double)reader["Lon"], Lat = (double)reader["Lat"], }, Angle = (double)reader["Angle"], Speed = (double)reader["Speed"]), UpdateTime = (double)reader["Speed"]), };}转换为如下形式
GpsData parse(IDataReader reader){ var obj = reader.ReadToObject(); var state = new GpsData() { IsValid = obj.IsValid, Location = new Location() { Lon = obj.Lon, Lat = obj.Lat, }, Angle = obj.Angle, Speed = obj.Speed, UpdateTime = obj.UpdateTime, }; return state;}到此这篇关于Asp.net core利用dynamic简化数据库访问的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持软件开发网。
边栏推荐
- VS Code 设置单击打开新文件窗口,不覆盖前一个窗口
- MySQL gap lock
- The future of game guild in decentralized games
- 有人碰到过这种情况吗,oracle logminer 同步的时候,clob字段的值丢失
- Huawei interview question: Recruitment
- Ikvm of toolbox Net project new progress
- Powerful, easy-to-use, professional editor / notebook software suitable for programmers / software developers, comprehensive evaluation and comprehensive recommendation
- Topic 1004: the story of cows (recursion)
- 下半年还有很多事要做
- 请问flink mysql cdc 全量读取mysql某个表数据,对原始的mysql数据库有影响吗
猜你喜欢

Manage nodejs with NVM (downgrade the high version to the low version)

redis探索之缓存击穿、缓存雪崩、缓存穿透

MHA high availability cluster deployment and failover of database

leetcode:329. 矩阵中的最长递增路径【dfs + cache + 无需回溯 + 优雅】

Fiori applications are shared through the enhancement of adaptation project

Vs code setting Click to open a new file window without overwriting the previous window

基于开源流批一体数据同步引擎 ChunJun 数据还原 —DDL 解析模块的实战分享

Nc100 converts strings to integers (ATOI)

Look at the sky at dawn and the clouds at dusk, and enjoy the beautiful pictures

工具箱之 IKVM.NET 项目新进展
随机推荐
Jenkins+webhooks-多分支参数化构建-
ZABBIX 6.0 source code installation and ha configuration
Fiori applications are shared through the enhancement of adaptation project
Vs code setting Click to open a new file window without overwriting the previous window
Manage nodejs with NVM (downgrade the high version to the low version)
机器学习—性能度量
Three stages of aho
请问flink mysql cdc 全量读取mysql某个表数据,对原始的mysql数据库有影响吗
Zero copy technology of MySQL
nexus搭建npm依赖私库
ustime写出了bug
从数据库中更新一条数据,用cdc会同时获得op字段分别为d和c的两条数据吗?我记得之前是只有op为u
科学创业三问:关于时机、痛点与重要决策
Localtime can't re-enter. It's a pit
R语言使用yardstick包的conf_mat函数计算多分类(Multiclass)模型在每个交叉验证(或者重采样)的每一折fold上的混淆矩阵、并使用summary输出每个fold的其它详细指标
高薪程序员&面试题精讲系列118之Session共享有哪些方案?
I spent tens of thousands of dollars to learn and bring goods: I earned 3 yuan in three days, and the transaction depends on the bill
The sky is blue and misty
Using burpsuite to capture app packages
Redis explores cache consistency