code_junkie

Customizing IQueryable<T>

Nov 13th, 2011
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. public partial class Product: IEntityBase
  2. {
  3.  
  4. public Product()
  5. {
  6. _DataContext = new SampleDataContext();
  7. }
  8.  
  9. private long _Id;
  10. [Column(Storage="_Id", AutoSync=AutoSync.OnInsert, DbType="BigInt NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
  11. public long Id
  12. {
  13. get{ return _Id; }
  14. set{ _Id = value; }
  15. }
  16.  
  17. private string _Name;
  18. [Column(Storage="_Name", DbType="NVarChar(MAX) NOT NULL", CanBeNull=false
  19. public string Name
  20. {
  21. get{ return _Name; }
  22. set{ _Name = value; }
  23. }
  24.  
  25. private SampleDataContext _DataContext;
  26.  
  27. //This is the property extending the Product class and should be set when this class is being returned
  28. //by IQueryable<T>.GetEnumerator()
  29. public SampleDataContext DataContext
  30. {
  31. get{ return _Name; }
  32. set{ _Name = value; }
  33. }
  34.  
  35. public MyQueryable<Product> GetProducts()
  36. {
  37. MyQueryable<Product> result = from p in context.Products
  38. where {Some Conditions 1}
  39. select p;
  40. result.DataContext = _DataContext;
  41. return result;
  42. }
  43.  
  44. public void SomeMethod()
  45. {
  46. //This query will not actually set the DataCotnext property.
  47. //And the generated sql query is something like:
  48. //SELECT * FROM Products WHERE {Some Conditions 1} AND {Some Conditions 2}
  49. var products = GetProducts().Where( {Some Conditions 2} );
  50.  
  51. //Now that the GetEnumerator() is called the DataContext property of the products
  52. //will be set.
  53. foreach( var item in products )
  54. {
  55. item.Name = "Test Name";
  56. item.DataContext.SubmitChanges();
  57. }
  58. }
  59. }
  60.  
  61. public MyQueryable<T>: IQueryable<T>
  62. where T: class, IEntityBase
  63. {
  64. //
  65. //Implementation of IQueryable which is my question
  66. //
  67.  
  68. public IEnumerator<T> GetEnumerator()
  69. {
  70. foreach( var item in Provider.GetEnumerator<T>() )
  71. {
  72. item.DataContext = this.DataContext;
  73. yield return item;
  74. }
  75. }
  76.  
  77. public SampleDataContext DataContext{ get; set; }
  78. }
  79.  
  80. public interface IEntityBase
  81. {
  82. SampleDataContext DataContext{ get; set; };
  83. }
  84.  
  85. public MyQueryable<T, TContext>: IQueryable<T>
  86. where T: class, IEntityBase
  87. where TContext: DataContext, new()
  88. {
  89.  
  90. public MyQueryable<T>(TContext context, IQueryable<T> baseIQueryable)
  91. {
  92. if( baseIQueryable == null )
  93. throw new ArgumentNullException("baseIQueryable");
  94.  
  95. this.Provider = baseIQueryable.Provider;
  96. this.Expression = baseIQueryable.Expression;
  97.  
  98. this.DataContext = context;
  99. }
  100.  
  101. public IEnumerator<T> GetEnumerator()
  102. {
  103. var enumerator = Provider.Execute<IEnumerable<T>>(Expression);
  104. foreach( var item in enumerator )
  105. {
  106. item.DataContext = this.DataContext ?? new TContext();
  107. yield return item;
  108. }
  109. }
  110.  
  111. IEnumerator IEnumerable.GetEnumerator()
  112. {
  113. var enumerator = Provider.Execute<IEnumerable>(Expression);
  114. foreach( var item in enumerator )
  115. {
  116. ((IEntityBase<TContext>)item).DataContext = this.DataContext;
  117. yield return item;
  118. }
  119. }
  120.  
  121. //
  122. //Other implementations...
  123. //
  124. public SampleDataContext DataContext{ get; set; }
  125. }
  126.  
  127. public partial class Product: IEntityBase
  128. {
  129. public MyQueryable<Product> GetProducts()
  130. {
  131. var result = from p in context.Products
  132. where {Some Conditions 1}
  133. select p;
  134. return new MyQueryable<typeof(Product), DataContext>(this.DataContext, result);
  135. }
  136. }
  137.  
  138. public IEnumerable<Product> GetProducts()
  139. {
  140. IQueryable<Product> result = from p in context.Products
  141. where {Some Conditions 1}
  142. select p;
  143. return result.AsEnumerable().Select( x => {
  144. x.SomeProp = context;
  145. return x;
  146. });
  147. }
  148.  
  149. return result.AssociateWith(context);
  150.  
  151. public static IEnumerable<T> AssociateWith<T>(
  152. this IEnumerable<T> source,
  153. DataContext context)
  154. where T : IEntityBase
  155. {
  156. foreach(T item in source)
  157. {
  158. item.DataContext = context;
  159. yield return item;
  160. }
  161. }
Add Comment
Please, Sign In to add comment