[AttributeUsage (AttributeTargets.Class)] Public classTableattribute:attribute { PublicTableattribute (stringtableName) {Name=TableName; } Public stringName {Get;Private Set; } } [AttributeUsage (Attributetargets.property)] Public classKeyattribute:attribute { PublicKeyattribute (): This(false) { } PublicKeyattribute (BOOLisidentity) {isidentity=isidentity; } Public BOOLisidentity {Get;Private Set; } } [AttributeUsage (Attributetargets.property)] Public classIgnoreattribute:attribute {} [AttributeUsage (Attributetargets.property)] Public classColumnattribute:attribute { PublicColumnattribute (stringcolumnName) {Name=ColumnName; QueryName=ColumnName; } PublicColumnattribute (stringColumnName,stringqueryname): This(columnName) {queryname=QueryName; } Public stringName {Get;Private Set; } Public stringQueryName {Get;Private Set; } }
The class to use TypeInfo must flag the above attribute on the entity, especially if the key attribute must exist as the primary key of the database table, otherwise the match in all fields may produce an inconsistent problem
The following is the implementation of entity additions and deletions function
Public Static classEntitymapper {Private Static ReadOnlyConcurrentdictionary<runtimetypehandle, typeinfo> typetableinfo =NewConcurrentdictionary<runtimetypehandle, typeinfo>(); Public StaticT get<t> (SSGCLASS.EXPRESSOPR exp)whereT:New() { varinfo = Trygetinfo (typeof(T)); varReader = SSGClass.DBConnect.ServerDb.ExecuteReader (info. SelectText +exp. Toparameters (), exp. Parameters); if(reader. Read ()) {returnInfo. Genentity<t>(reader); } Throw NewExceptiontecnical (string. Format (Global.Consts.OutOfRing,typeof(T). Name)); } Statictypeinfo trygetinfo (type type) {TypeInfo info; RuntimeTypeHandle Handle=type. Typehandle; if(! Typetableinfo.trygetvalue (Handle, outinfo)) {Info=Newtypeinfo (type); Typetableinfo[handle]=info; } returninfo; } Public StaticList<t> getlist<t> (SSGCLASS.EXPRESSOPR exp =NULL)whereT:New() {List<T> LST =NewList<t>(); varinfo = Trygetinfo (typeof(T)); System.Data.Common.DbDataReader reader; if(exp = =NULL) Reader= SSGClass.DBConnect.ServerDb.ExecuteReader (info. SelectText,NULL); ElseReader= SSGClass.DBConnect.ServerDb.ExecuteReader (info. SelectText +exp. Toparameters (), exp. Parameters); while(reader. Read ()) {lst. ADD (info. Genentity<T>(reader)); } returnlst; } Public Static intDelete<t> (SSGCLASS.EXPRESSOPR exp =NULL) { stringDeleteText = Trygetinfo (typeof(T)). DeleteText; if(exp = =NULL) returnSSGClass.DBConnect.ServerDb.ExcuteCommand (DeleteText,NULL); Else returnSSGClass.DBConnect.ServerDb.ExcuteCommand (DeleteText +exp. Toparameters (), exp. Parameters); } Public Static intDeleteall<t>() { returnDelete<t>(); } Public Static intDelete (Objectentity) { varinfo =Trygetinfo (entity. GetType ()); returnDelete (info. Getdeleteexpress (entity)); } Public Static intUpdate (Objectentity) { varinfo =Trygetinfo (entity. GetType ()); returnSSGClass.DBConnect.ServerDb.ExcuteCommand (info. UPDATETEXT, Info. Getentitypatams (entity)); } Public Static intInsert (Objectentity) { varinfo =Trygetinfo (entity. GetType ()); returnSSGClass.DBConnect.ServerDb.ExcuteCommand (info. InsertText, Info. Getentitypatams (entity)); } }