CN103246743A - 一种利用编写实体类特性实现简单数据库的方法 - Google Patents

一种利用编写实体类特性实现简单数据库的方法 Download PDF

Info

Publication number
CN103246743A
CN103246743A CN2013101896572A CN201310189657A CN103246743A CN 103246743 A CN103246743 A CN 103246743A CN 2013101896572 A CN2013101896572 A CN 2013101896572A CN 201310189657 A CN201310189657 A CN 201310189657A CN 103246743 A CN103246743 A CN 103246743A
Authority
CN
China
Prior art keywords
entity class
class
string
data base
characteristic
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN2013101896572A
Other languages
English (en)
Inventor
王彦军
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Inspur Group Shandong General Software Co Ltd
Original Assignee
Inspur Group Shandong General Software Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Inspur Group Shandong General Software Co Ltd filed Critical Inspur Group Shandong General Software Co Ltd
Priority to CN2013101896572A priority Critical patent/CN103246743A/zh
Publication of CN103246743A publication Critical patent/CN103246743A/zh
Pending legal-status Critical Current

Links

Images

Landscapes

  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

本发明提供一种利用编写实体类特性实现简单数据库的方法,该方法是利用自定义用于类的特性类TableAttribute和用于属性的特性类TableAttribute,通过实体类中特性的信息,实现对逻辑业务层中简单的保存、修改的封装,从而使得在对数据库简单的操作中,只需编写相对应的实体类,调用封装的统一泛型类FoolDB<T>就可以实现傻瓜式的保存、修改等操作。提高了编写程序的效率,尤其是在对外部做WebService接口时,节省了大量的人力、物力。

Description

一种利用编写实体类特性实现简单数据库的方法
技术领域
本发明涉及计算机软件C#对象实例创建领域,特别涉及利用编写实体类特性从而实现简单处理数据的通用方法, 具体地说是一种利用编写实体类特性实现简单数据库的方法。
背景技术
目前随着信息化的日益加深,市场上系统也琳琅满目,各种各样的系统也带来了数据的交互问题,由于一个大的企业对于信息化的要求不同,不同的部门、不同的业务也需要有不同的系统来支撑,那么怎么样才能更好更快的实现不同系统之间的数据交互呢?
   在.NET中通用的做法是做WebService向外部提供接口,包括提供给外部程序的接口和外部环境推送过来的数据等,那么怎么样将这些简单的涉及到数据库操作的工作简化,快速实现,解放劳动力就成了我们需要考虑的问题。
 现在军工行业版中对外提供的接口,所采用的是一套固定的模式,在接口最上层写外层展现方法,逻辑层写抽取数据的逻辑(或者转化数据层的数据),数据层将数据转化并保存到数据库中(或者取数据),这样当一个客户每提出要同步一个别的系统的表数据到本系统中(或同步本系统数据到别系统中时),我们不得不每一层都加一个方法,以实现接口。这样就比较繁琐了,既然我们有用到实体类,何不利用实体类中根据特性所提供的信息,将逻辑层和数据层进行一次封装,而每当我们需要增加一个简单的接口时,只需要在外层(即asmx这个页面)写方法,然后配置一个实体类,就可以将接口完成。
发明内容
本发明的目的是提供一种利用编写实体类特性实现简单数据库的方法。
本发明的目的是按以下方式实现的,通过反射、泛型、自定义特性技术实现简单业务层的统一口径的封装,针对不同的实体类实现数据库的简单操作,有效降低了业务逻辑层的编写工作强度,提高了工作效率,步骤如下:
1)自定义特性类,用于存储实体类所对应的一些操作信息;
2)利用泛型技术,统一封装以对应不同的实体类;
3)通过反射技术收集实体类的属性、特性信息;
4)实现面向接口编程与创建对象的融合。
本发明的优异效果:
创建了特性类;利用泛型技术实现了本方法对实体类的通用;提供系统扩展便利方式;实体类中的一些信息都通过反射等技术实现,统一管理;本技术实现简单,无特殊保密算法;调用实现简单易用;降低了编程的复杂度,进而提高了编程的效率。
附图说明
图1是系统结构示意图;
图2为工作流程图。
具体实施方式
参照说明书附图对本发明的方法作以下详细地说明。
本发明所要解决的技术问题是通过自定义特性类,将一些表信息分装到特性类中,进而封装简单的插入、更改的逻辑。
本发明还利用的泛型技术,对不同的实体类实现封装操作。
为了解决上边的问题,本发明实施例提供了一种编写实体类特性从而实现简单处理数据的通用方法。包括:
自定义特性类,用于存储实体类所对应的一些操作信息;
利用先进的泛型技术,统一封装以对应不同的实体类;
通过反射等技术收集实体类的属性,特性信息;
实现面向接口编程与创建对象的融合。
实施例
本发明方法适用于C#程序的对象实例创建及面向接口编程实现。其具体实现方式如图所示。
、自定义特性类如下:
    //描述类的特性类TableAttribute
[AttributeUsage(AttributeTargets.Class)]
    public class TableAttribute : Attribute
    {
        private string tableName;
        private string tableWhere;
        public TableAttribute(string tableName, string tabelwhere)
        {
            this.tableName = tableName;
            this.tableWhere = tabelwhere;
        }
        /// <summary>
        /// 表名
        /// </summary>
        public string TableName
        {
            get { return tableName; }
            set { this.tableName = value; }
        }
        /// <summary>
        /// 获取、跟新的条件
        /// </summary>
        public string TableWhere
        {
            get { return tableWhere; }
            set { this.tableWhere = value; }
}
}
//描述属性的特性类ColumnAttribute
public enum ColType
    {
sSTRING,
sNUMBER
}
 [AttributeUsage(AttributeTargets.Property)]
public class ColumnAttribute:Attribute
{
private bool _iscol;
private string _colname;
private bool _isKey;
private ColType _coltype;
public ColumnAttribute(bool iscol, string colname, bool isKey, ColType type)
{
_iscol = iscol;
_colname = colname;
_isKey = isKey;
_coltype = type;
  }
public bool IsCol
{
get { return _iscol; }
set { _iscol = value; }
}
public bool IsKey
{
get { return _isKey; }
set { _isKey = value; }
}
public string ColName
{
get { return _colname; }
set { _colname = value; }
}
public ColType ColStyle
{
get { return _coltype; }
set { _coltype = value; }
}
}
2、封装具体逻辑的过程:
public class FoolDB<T> where T : new()
{
#region 私有变量
private PropertyInfo[] lstpro;//存放spi中所有的属性值
private List<PropertyInfo> exportpro = new List<PropertyInfo>();//存放有特性需要导出的属性字段列表
private string sql = string.Empty; //用于存放sql容器
private List<string> lstsql = new List<string>();  //用于存放所要一次性执行的sql集合
private DataBase DBHandle; //封装于底层的DataBase
private GMessage mess = new GMessage(); //所要返回的信息
private string tabname = string.Empty; //执行数据库操作的表名
private string tabwhere = string.Empty; //跟新/选取记录时的条件
#endregion
#region 共有方法
#region 新增操作
/// <summary>
/// 普通的插入操作
/// </summary>
/// <param name="lstT"></param>
/// <param name="sztxx">注册的实例</param>
/// <returns></returns>
public GMessage Insert(List<T> lstT,  string sztxx)
{
DBHandle = new DataBase(sztxx);
string cols = string.Empty;
  string vals = string.Empty;
ColumnAttribute att;
Type tt = typeof(T);
lstpro = tt.GetProperties();
GetTableAttributeInfo(); //获取SPI类的特性,获取表名及记录选取条件
foreach (T newT in lstT)
{
foreach (PropertyInfo p in lstpro)
{
//选取spi中的列特性
att = Attribute.GetCustomAttribute(p, typeof(ColumnAttribute)) as ColumnAttribute;
if (att != null) //不为空时,取出特性中的列名和值
{
if (!att.IsCol) continue;
cols += att.ColName+",";
//分情况获取1:字符串类型;:数值型
vals += GetColumnAttributeInfo(newT, att, p);
}
else //为空时默认选取类名为表名,列为字符型
{
cols += p.Name + ", ";
vals += "'" + p.GetValue(newT, null) + "',";
}
}
sql = " INSERT INTO " + tabname + "( " + cols.Remove(cols.LastIndexOf(","), 1) + " ) VALUES(" + vals.Remove(vals.LastIndexOf(","), 1) + " ) ";
lstsql.Add(sql);
}
try
{
DBHandle.Execute(lstsql.ToArray());
mess.Flag = GMessageFlag.SUCCESS;
mess.Content = "保存成功!";
}
catch (Exception ex)
{
 mess.Flag = GMessageFlag.FAIL;
mess.Content = "保存失败!" + ex.Message;
}
return mess;
  }
#endregion
#region 跟新操作
/// <summary>
/// 
/// </summary>
/// <param name="lstT"></param>
/// <param name="sztxx">注册的实例</param>
/// <returns></returns>
public GMessage Update(List<T> lstT, string sztxx)
{
DBHandle = new DataBase(sztxx);
string col = string.Empty;
 string val = string.Empty;
string colval = string.Empty;
string tabwhere = string.Empty;
Type t = typeof(T);
ColumnAttribute att;
lstpro = t.GetProperties();
GetTableAttributeInfo();//获取SPI类的特性,获取表名及记录选取条件
foreach (T newT in lstT)
{
foreach (PropertyInfo p in lstpro)
{
//选取spi中的列特性
att = Attribute.GetCustomAttribute(p, typeof(ColumnAttribute)) as ColumnAttribute;
if (att != null)//不为空时,取出特性中的列名和值
{
if (!att.IsCol) continue;
col = att.ColName;
  //分情况获取1:字符串类型;:数值型
val = GetColumnAttributeInfo(newT, att, p);
if (att.IsKey)
{
tabwhere += col + "=" + val;
  }
else
{   
colval += col + "=" + val;
}
}
else//为空时默认选取类名为表名,列为字符型
{
colval += p.Name + "=" + "'" + p.GetValue(newT, null) + "',  ";
}
}
sql = " UPDATE " + tabname + " SET    " + colval.Substring(0, colval.Length - 1) + " WHERE  " + tabwhere.Substring(0, tabwhere.Length - 1);
  lstsql.Add(sql);
}
try
{
DBHandle.Execute(lstsql.ToArray());
mess.Flag = GMessageFlag.SUCCESS;
mess.Content = "保存成功!";
}
catch (Exception ex)
{
mess.Flag = GMessageFlag.FAIL;
                mess.Content = "保存失败!" + ex.Message;
            }
            return mess;
        }
        #endregion
        #endregion
        #region 私有方法
        //获取类特性
        private void GetTableAttributeInfo()
        {
            TableAttribute tablearr = Attribute.GetCustomAttribute(typeof(T), typeof(TableAttribute)) as TableAttribute;
            if (tablearr != null)
            {
                tabname = tablearr.TableName;
                tabwhere = tablearr.TableWhere;
            }
            else
            {
                tabname = typeof(T).Name;
                tabwhere = " 1=1 ";
            }
        }
               /// <summary>
        /// 分情况获取属性特性值
        /// </summary>
        /// <param name="instance">类的实例</param>
        /// <param name="ca">列的特性类</param>
        /// <param name="p">列的PropertyInfo</param>
        /// <returns></returns>
        private string GetColumnAttributeInfo(T instance,ColumnAttribute ca,PropertyInfo p)
        {
            string val;
            if (ca.ColStyle.Equals(ColType.sNUMBER))
            {
                val = " " + p.GetValue(instance, null) + ", ";
            }
            else
            {
                val = "'" + p.GetValue(instance, null) + "',  ";
            }
            return val;
        }
        #endregion
    }
3、具体实施步骤
泛型实例化 FoolDB 然后调用类中的方法,示例如下:
                List<PMIS_KTXXB> lstktxxb = new List<PMIS_KTXXB>();
                PMIS_KTXXB ktxxb = new PMIS_KTXXB();
                ktxxb.KTXXB_GUID = System.Guid.NewGuid().ToString();
                ktxxb.KTXXB_JHSL = "12";
                ktxxb.KTXXB_KTBH = "345";
                lstktxxb.Add(ktxxb);
                FoolDB<PMIS_KTXXB> fool = new FoolDB<PMIS_KTXXB>();
  fool.Insert(lstktxxb, "0");
应当理解的是,本发明的上述对具体实施例的描述较为具体,但不能因此而理解为对本发明请求保护范围的限制。
除说明书所述的技术特征外,均为本专业技术人员的已知技术。

Claims (1)

1.一种利用编写实体类特性实现简单数据库的方法, 其特征在于通过反射、泛型、自定义特性技术实现简单业务层的统一口径的封装,针对不同的实体类实现数据库的简单操作,有效降低了业务逻辑层的编写工作强度,提高了工作效率,步骤如下:
1)自定义特性类,用于存储实体类所对应的一些操作信息;
2)利用泛型技术,统一封装以对应不同的实体类;
3)通过反射技术收集实体类的属性、特性信息;
4)实现面向接口编程与创建对象的融合。
CN2013101896572A 2013-05-21 2013-05-21 一种利用编写实体类特性实现简单数据库的方法 Pending CN103246743A (zh)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN2013101896572A CN103246743A (zh) 2013-05-21 2013-05-21 一种利用编写实体类特性实现简单数据库的方法

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN2013101896572A CN103246743A (zh) 2013-05-21 2013-05-21 一种利用编写实体类特性实现简单数据库的方法

Publications (1)

Publication Number Publication Date
CN103246743A true CN103246743A (zh) 2013-08-14

Family

ID=48926263

Family Applications (1)

Application Number Title Priority Date Filing Date
CN2013101896572A Pending CN103246743A (zh) 2013-05-21 2013-05-21 一种利用编写实体类特性实现简单数据库的方法

Country Status (1)

Country Link
CN (1) CN103246743A (zh)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN103838574A (zh) * 2014-02-20 2014-06-04 浪潮集团山东通用软件有限公司 一种对数据表进行分组汇总的通用方法
CN112148746A (zh) * 2020-08-21 2020-12-29 杭州安恒信息技术股份有限公司 生成数据库表结构文档的方法、装置、电子装置和存储介质

Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2002037335A2 (en) * 2000-10-31 2002-05-10 Loudcloud, Inc. Object-oriented database abstraction and statement generation
CN102346774A (zh) * 2011-09-26 2012-02-08 深圳市信游天下网络科技有限公司 一种数据库操作方法及装置

Patent Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2002037335A2 (en) * 2000-10-31 2002-05-10 Loudcloud, Inc. Object-oriented database abstraction and statement generation
CN102346774A (zh) * 2011-09-26 2012-02-08 深圳市信游天下网络科技有限公司 一种数据库操作方法及装置

Non-Patent Citations (3)

* Cited by examiner, † Cited by third party
Title
张全;陈可为;杨积斌;高海军: "基于反射技术及面向对象的关系数据存取方法", 《赤峰学院学报(自然科学版)》 *
张少应: "基于实体类的数据库访问技术的研究与实现", 《计算机与信息技术》 *
张金波: ".NET平台分层架构开发中泛型实现通用数据访问层", 《计算机与数字工程》 *

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN103838574A (zh) * 2014-02-20 2014-06-04 浪潮集团山东通用软件有限公司 一种对数据表进行分组汇总的通用方法
CN112148746A (zh) * 2020-08-21 2020-12-29 杭州安恒信息技术股份有限公司 生成数据库表结构文档的方法、装置、电子装置和存储介质
CN112148746B (zh) * 2020-08-21 2024-03-26 杭州安恒信息技术股份有限公司 生成数据库表结构文档的方法、装置、电子装置和存储介质

Similar Documents

Publication Publication Date Title
CN108765158B (zh) 一种基于区块链的智能合约引擎系统及其合约执行方法
CN103218220A (zh) 基于动态可插拔组件的物联网中间件系统
US9176769B2 (en) Partitioned array objects in a distributed runtime
CN108345691B (zh) 数据源通用处理框架构建方法、数据源处理方法及装置
US9535678B2 (en) Providing distributed array containers for programming objects
CN113626128A (zh) 视听媒体微服务第三方模块接入方法、系统、电子设备
CN110532058B (zh) 容器集群服务的管理方法、装置、设备及可读存储介质
US9183040B2 (en) Method for manipulating objects in a SOA registry
CN103246743A (zh) 一种利用编写实体类特性实现简单数据库的方法
US8024374B2 (en) Computer object conversion using an intermediate object
CN104133678B (zh) 构建、运行交互地震解释系统的方法及装置
CN105103125A (zh) 时钟中断信号的获取方法和nfv功能实体
CN111124618B (zh) 资源部署方法、装置、存储介质及设备
CN105893039A (zh) 一种利用编写实体类特性从而实现简单数据库的通用方法
CN102609253B (zh) 一种智能卡的应用实现方法及系统
CN104375832A (zh) 一种计算机底层硬件屏蔽设备及方法
CN104331275A (zh) 一种Java卡及其应用打补丁的方法
CN116680209A (zh) 基于wasm的多智能合约实例管理方法
CN105677347A (zh) 一种数据处理的方法及装置
US20030056194A1 (en) Enhanced software components
US8566814B2 (en) Transporting object packets in a nested system landscape
CN108694048A (zh) 一种批量发布服务的实现方法
CN102354282A (zh) 基于分级OSGi的RFID中间件构件产品运行时监控系统
CN106598551B (zh) 一种智能卡、智能卡的处理方法和装置
CN108153564A (zh) 界面管理方法、设备及系统

Legal Events

Date Code Title Description
C06 Publication
PB01 Publication
C10 Entry into substantive examination
SE01 Entry into force of request for substantive examination
WD01 Invention patent application deemed withdrawn after publication
WD01 Invention patent application deemed withdrawn after publication

Application publication date: 20130814