当前位置:网站首页>Asp. NETCORE uses dynamic to simplify database access
Asp. NETCORE uses dynamic to simplify database access
2022-07-01 13:02:00 【1024 questions】
Today I wrote a database help class , The code is as follows .
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 { // Realize one by yourself , no need ExpandoObject, To support case insensitive reading 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>(); }}In a nutshell , You can put the following code
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"]), };}To the following form
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;}This is about Asp.net core utilize dynamic This is the end of the article on simplifying database access . I hope it will be helpful for your study , I also hope you can support the software development network .
边栏推荐
- leetcode 322. Coin Change 零钱兑换(中等)
- 彩色五角星SVG动态网页背景js特效
- MySQL statistical bill information (Part 2): data import and query
- When Sqlalchemy deletes records with foreign key constraints, the foreign key constraints do not work. What is the solution?
- 股票开户要认识谁?实际上网上开户安全么?
- Vs code set code auto save
- 使用nvm管理nodejs(把高版本降级为低版本)
- 天青色等烟雨
- be based on. NETCORE development blog project starblog - (13) add friendship link function
- 哪个券商公司开户佣金低又安全又可靠
猜你喜欢

Detailed explanation of OSPF LSA of routing Foundation

How can genetic testing help patients fight disease?

【开发大杀器】之Idea

MHA high availability cluster deployment and failover of database

基于.NetCore开发博客项目 StarBlog - (13) 加入友情链接功能

Vs code set code auto save
![leetcode:329. The longest incremental path in the matrix [DFS + cache + no backtracking + elegance]](/img/10/acd162c3adf9d6f14fa5a551dc0d25.png)
leetcode:329. The longest incremental path in the matrix [DFS + cache + no backtracking + elegance]
![[Niu Ke's questions -sql big factory interview real questions] no2 User growth scenario (a certain degree of information flow)](/img/a0/e9e7506c9c34986dc73562539c8410.png)
[Niu Ke's questions -sql big factory interview real questions] no2 User growth scenario (a certain degree of information flow)

Shell script imports stored procedures into the database

Operator-1初识Operator
随机推荐
Logstash error: cannot reload pipeline, because the existing pipeline is not reloadable
MySQL statistical bill information (Part 2): data import and query
Zabbix 6.0 源码安装以及 HA 配置
Interpretation of hard threshold function [easy to understand]
题目 2612: 蓝桥杯2021年第十二届省赛真题-最少砝码(枚举找规律+递推)
有没有大佬 遇到过flink监控postgresql数据库, 检查点无法使用的问题
机器学习—性能度量
股票开户要认识谁?实际上网上开户安全么?
Fiori applications are shared through the enhancement of adaptation project
Operator-1 first acquaintance with operator
逆向调试入门-PE结构-输入表输出表05/07
VS Code 设置代码自动保存
华为HMS Core携手超图为三维GIS注入新动能
mysql统计账单信息(下):数据导入及查询
localtime居然不可重入,踩坑了
Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing
ROS2 Foxy depthai_ ROS tutorial
shell脚本导入存储过程到数据库
VM virtual machine configuration dynamic IP and static IP access
Perl 5.10.0 installation package download