上一篇全部是ExpressionVisitor
这一篇再贴一些类出来
DbContext类
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | using System; using System.Collections.Generic; using System.Data.Linq.Mapping; using System.Linq; using System.Text; namespace FYJ.Data.Linq { public partial class DbContext: System.Data.Linq.DataContext where T : class { private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource(); #region 可扩展性方法定义 partial void OnCreated(); partial void Insert(T instance) ; partial void Update(T instance); partial void Delete(T instance); #endregion #region 构造函数 public DbContext( string connectionString) : base (connectionString, mappingSource) { OnCreated(); } public DbContext(System.Data.IDbConnection connection) : base (connection, mappingSource) { OnCreated(); } public DbContext( string connection, System.Data.Linq.Mapping.MappingSource mappingSource) : base (connection, mappingSource) { OnCreated(); } public DbContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) : base (connection, mappingSource) { OnCreated(); } #endregion public System.Data.Linq.TableTables { get { return this .GetTable(); } } } } |
1 | DbQueryable类 |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Text; namespace FYJ.Data.Linq { public class DbQueryable: IQueryable, IOrderedQueryable{ public DbQueryable(FYJ.Data.IDbHelper db= null ) { Provider = new DbQueryProvider( typeof (T),db); Expression = Expression.Constant( this ); } public DbQueryable(IQueryProvider provider) { if (provider == null ) { throw new ArgumentNullException( "provider" ); } Provider = provider; Expression = Expression.Constant( this ); } public DbQueryable(IQueryProvider provider, Expression expression) { if (provider == null ) { throw new ArgumentNullException( "provider" ); } if (expression == null ) { throw new ArgumentNullException( "expression" ); } if (! typeof (IQueryable).IsAssignableFrom(expression.Type)) { throw new ArgumentOutOfRangeException( "expression" ); } Provider = provider; Expression = expression; } #region IEnumerable成员 public IEnumeratorGetEnumerator() { return (Provider.Execute(Expression)).GetEnumerator(); } #endregion #region IEnumerable 成员 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return Provider.Execute(Expression).GetEnumerator(); } #endregion #region IQueryable 成员 public Type ElementType { get { return typeof (T); } } public Expression Expression { get ; private set ; } public IQueryProvider Provider { get ; private set ; } #endregion } }DbQueryProvider类 |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | using System; using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Text; namespace FYJ.Data.Linq { public class DbQueryProvider :QueryProviderAbstract { public DbQueryProvider(Type elementType, IDbHelper db) : base (elementType,db) { } public override System.Data.DataTable ExecuteDataTable( string sql) { DataTable dt = db.GetDataTable(sql); if (dt.Columns.Contains( "rownum" )) { dt.Columns.Remove( "rownum" ); } return dt; } } }ObjectReader 类 |
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Linq; using System.Reflection; using System.Text; namespace FYJ.Data.Linq { internal class ObjectReader: IEnumerable, IEnumerable { Enumerator enumerator; internal ObjectReader(DataTable dt) { this .enumerator = new Enumerator(dt); } internal ObjectReader(DataTable dt, Funcprojector) { this .enumerator = new Enumerator(dt, projector); } #region IEnumerable成员 public IEnumeratorGetEnumerator() { Enumerator e = this .enumerator; if (e == null ) { throw new InvalidOperationException( "Cannot enumerate more than once" ); } this .enumerator = null ; return e; } #endregion #region IEnumerable 成员 IEnumerator IEnumerable.GetEnumerator() { return this .GetEnumerator(); } #endregion class Enumerator :ProjectionRow,IEnumerator, IEnumerator, IDisposable { Funcprojector; private DataTable dt; T current; private int currentIndex = 0; //当前索引 internal Enumerator(DataTable dt) { this .dt = dt; } internal Enumerator(DataTable dt, Funcprojector) { this .dt = dt; this .projector = projector; } public T Current { get { return this .current; } } object IEnumerator.Current { get { if ( typeof (T) == typeof (DataTable)) { return dt; } return this .current; } } public override object GetValue( int index) //这里的index是列索引 { if (index >= 0) { if (dt == null ) { return null ; } if ( typeof (T) == typeof (DataTable)) { return dt; } object obj = null ; obj = this .dt.Rows[ this .currentIndex][index]; if (obj == DBNull.Value) { return null ; } return obj; } throw new IndexOutOfRangeException(); } public void Dispose() { } #region IEnumerator 成员 public bool MoveNext() { if ( typeof (T) == typeof (DataTable)) { return false ; } if (dt== null ||currentIndex >= dt.Rows.Count) { return false ; } if ( this .projector != null ) { this .current = this .projector( this ); } else { this .current=(T)DataTableToObject( this .dt, Activator.CreateInstance( typeof (T)), this .currentIndex); } currentIndex++; return true ; } public void Reset() { this .currentIndex = 0; } #endregion private object DataTableToObject(DataTable dt, object obj, int currentIndex) { if (dt != null && dt.Rows.Count > 0&¤tIndex<dt.rows.count) {= "" foreach = "" (propertyinfo= "" info= "" in = "" obj.gettype().getproperties())= "" if = "" (dt.columns.contains(info.name))= "" (dt.rows[currentindex][info.name]= "" != "DBNull.Value)" info.setvalue(obj,= "" dt.rows[currentindex][info.name],= "" null );= "" }= "" return = "" obj;= "" } |