ObjectDataSourceView.cs 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. //
  2. // System.Web.UI.WebControls.ObjectDataSourceView
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc. (http://www.novell.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #if NET_2_0
  30. using System;
  31. using System.Reflection;
  32. using System.Collections;
  33. using System.Collections.Specialized;
  34. using System.ComponentModel;
  35. using System.IO;
  36. using System.Data;
  37. namespace System.Web.UI.WebControls
  38. {
  39. public class ObjectDataSourceView : DataSourceView, IStateManager
  40. {
  41. ObjectDataSource owner;
  42. HttpContext context;
  43. Type objectType;
  44. Type dataObjectType;
  45. StateBag viewState = new StateBag ();
  46. ParameterCollection selectParameters;
  47. ParameterCollection updateParameters;
  48. ParameterCollection deleteParameters;
  49. ParameterCollection insertParameters;
  50. ParameterCollection filterParameters;
  51. private static readonly object DeletedEvent = new object();
  52. private static readonly object DeletingEvent = new object();
  53. private static readonly object FilteringEvent = new object();
  54. private static readonly object InsertedEvent = new object();
  55. private static readonly object InsertingEvent = new object();
  56. private static readonly object ObjectCreatedEvent = new object();
  57. private static readonly object ObjectCreatingEvent = new object();
  58. private static readonly object ObjectDisposingEvent = new object();
  59. // private static readonly object ResolvingMethodEvent = new object();
  60. private static readonly object SelectedEvent = new object();
  61. private static readonly object SelectingEvent = new object();
  62. private static readonly object UpdatedEvent = new object();
  63. private static readonly object UpdatingEvent = new object();
  64. public ObjectDataSourceView (ObjectDataSource owner, string name, HttpContext context): base (owner, name)
  65. {
  66. this.owner = owner;
  67. this.context = context;
  68. }
  69. public event ObjectDataSourceStatusEventHandler Deleted {
  70. add { Events.AddHandler (DeletedEvent, value); }
  71. remove { Events.RemoveHandler (DeletedEvent, value); }
  72. }
  73. public event ObjectDataSourceMethodEventHandler Deleting {
  74. add { Events.AddHandler (DeletingEvent, value); }
  75. remove { Events.RemoveHandler (DeletingEvent, value); }
  76. }
  77. public event ObjectDataSourceFilteringEventHandler Filtering {
  78. add { Events.AddHandler (FilteringEvent, value); }
  79. remove { Events.RemoveHandler (FilteringEvent, value); }
  80. }
  81. public event ObjectDataSourceStatusEventHandler Inserted {
  82. add { Events.AddHandler (InsertedEvent, value); }
  83. remove { Events.RemoveHandler (InsertedEvent, value); }
  84. }
  85. public event ObjectDataSourceMethodEventHandler Inserting {
  86. add { Events.AddHandler (InsertingEvent, value); }
  87. remove { Events.RemoveHandler (InsertingEvent, value); }
  88. }
  89. public event ObjectDataSourceObjectEventHandler ObjectCreated {
  90. add { Events.AddHandler (ObjectCreatedEvent, value); }
  91. remove { Events.RemoveHandler (ObjectCreatedEvent, value); }
  92. }
  93. public event ObjectDataSourceObjectEventHandler ObjectCreating {
  94. add { Events.AddHandler (ObjectCreatingEvent, value); }
  95. remove { Events.RemoveHandler (ObjectCreatingEvent, value); }
  96. }
  97. public event ObjectDataSourceDisposingEventHandler ObjectDisposing {
  98. add { Events.AddHandler (ObjectDisposingEvent, value); }
  99. remove { Events.RemoveHandler (ObjectDisposingEvent, value); }
  100. }
  101. /* public event ObjectDataSourceResolvingMethodEventHandler ResolvingMethod {
  102. add { Events.AddHandler (ResolvingMethodEvent, value); }
  103. remove { Events.RemoveHandler (ResolvingMethodEvent, value); }
  104. }
  105. */
  106. public event ObjectDataSourceStatusEventHandler Selected {
  107. add { Events.AddHandler (SelectedEvent, value); }
  108. remove { Events.RemoveHandler (SelectedEvent, value); }
  109. }
  110. public event ObjectDataSourceSelectingEventHandler Selecting {
  111. add { Events.AddHandler (SelectingEvent, value); }
  112. remove { Events.RemoveHandler (SelectingEvent, value); }
  113. }
  114. public event ObjectDataSourceStatusEventHandler Updated {
  115. add { Events.AddHandler (UpdatedEvent, value); }
  116. remove { Events.RemoveHandler (UpdatedEvent, value); }
  117. }
  118. public event ObjectDataSourceMethodEventHandler Updating {
  119. add { Events.AddHandler (UpdatingEvent, value); }
  120. remove { Events.RemoveHandler (UpdatingEvent, value); }
  121. }
  122. protected virtual void OnDeleted (ObjectDataSourceStatusEventArgs e)
  123. {
  124. if (Events != null) {
  125. ObjectDataSourceStatusEventHandler eh = (ObjectDataSourceStatusEventHandler) Events [DeletedEvent];
  126. if (eh != null) eh (this, e);
  127. }
  128. }
  129. protected virtual void OnDeleting (ObjectDataSourceMethodEventArgs e)
  130. {
  131. if (Events != null) {
  132. ObjectDataSourceMethodEventHandler eh = (ObjectDataSourceMethodEventHandler) Events [DeletingEvent];
  133. if (eh != null) eh (this, e);
  134. }
  135. }
  136. protected virtual void OnFiltering (ObjectDataSourceFilteringEventArgs e)
  137. {
  138. if (Events != null) {
  139. ObjectDataSourceFilteringEventHandler eh = (ObjectDataSourceFilteringEventHandler) Events [FilteringEvent];
  140. if (eh != null) eh (this, e);
  141. }
  142. }
  143. protected virtual void OnInserted (ObjectDataSourceStatusEventArgs e)
  144. {
  145. if (Events != null) {
  146. ObjectDataSourceStatusEventHandler eh = (ObjectDataSourceStatusEventHandler) Events [InsertedEvent];
  147. if (eh != null) eh (this, e);
  148. }
  149. }
  150. protected virtual void OnInserting (ObjectDataSourceMethodEventArgs e)
  151. {
  152. if (Events != null) {
  153. ObjectDataSourceMethodEventHandler eh = (ObjectDataSourceMethodEventHandler) Events [InsertingEvent];
  154. if (eh != null) eh (this, e);
  155. }
  156. }
  157. protected virtual void OnObjectCreated (ObjectDataSourceEventArgs e)
  158. {
  159. if (Events != null) {
  160. ObjectDataSourceObjectEventHandler eh = (ObjectDataSourceObjectEventHandler) Events [ObjectCreatedEvent];
  161. if (eh != null) eh (this, e);
  162. }
  163. }
  164. protected virtual void OnObjectCreating (ObjectDataSourceEventArgs e)
  165. {
  166. if (Events != null) {
  167. ObjectDataSourceObjectEventHandler eh = (ObjectDataSourceObjectEventHandler) Events [ObjectCreatingEvent];
  168. if (eh != null) eh (this, e);
  169. }
  170. }
  171. protected virtual void OnObjectDisposing (ObjectDataSourceDisposingEventArgs e)
  172. {
  173. if (Events != null) {
  174. ObjectDataSourceDisposingEventHandler eh = (ObjectDataSourceDisposingEventHandler) Events [ObjectDisposingEvent];
  175. if (eh != null) eh (this, e);
  176. }
  177. }
  178. /* protected virtual void OnResolvingMethod (ObjectDataSourceResolvingMethodEventArgs e)
  179. {
  180. if (Events != null) {
  181. ObjectDataSourceResolvingMethodEventHandler eh = (ObjectDataSourceResolvingMethodEventHandler) Events [ResolvingMethodEvent];
  182. if (eh != null) eh (this, e);
  183. }
  184. }
  185. */
  186. protected virtual void OnSelected (ObjectDataSourceStatusEventArgs e)
  187. {
  188. if (Events != null) {
  189. ObjectDataSourceStatusEventHandler eh = (ObjectDataSourceStatusEventHandler) Events [SelectedEvent];
  190. if (eh != null) eh (this, e);
  191. }
  192. }
  193. protected virtual void OnSelecting (ObjectDataSourceSelectingEventArgs e)
  194. {
  195. if (Events != null) {
  196. ObjectDataSourceSelectingEventHandler eh = (ObjectDataSourceSelectingEventHandler) Events [SelectingEvent];
  197. if (eh != null) eh (this, e);
  198. }
  199. }
  200. protected virtual void OnUpdated (ObjectDataSourceStatusEventArgs e)
  201. {
  202. if (Events != null) {
  203. ObjectDataSourceStatusEventHandler eh = (ObjectDataSourceStatusEventHandler) Events [UpdatedEvent];
  204. if (eh != null) eh (this, e);
  205. }
  206. }
  207. protected virtual void OnUpdating (ObjectDataSourceMethodEventArgs e)
  208. {
  209. if (Events != null) {
  210. ObjectDataSourceMethodEventHandler eh = (ObjectDataSourceMethodEventHandler) Events [UpdatingEvent];
  211. if (eh != null) eh (this, e);
  212. }
  213. }
  214. StateBag ViewState {
  215. get { return viewState; }
  216. }
  217. public override bool CanDelete {
  218. get { return DeleteMethod.Length > 0; }
  219. }
  220. public override bool CanInsert {
  221. get { return InsertMethod.Length > 0; }
  222. }
  223. public override bool CanPage {
  224. get { return EnablePaging; }
  225. }
  226. public override bool CanRetrieveTotalRowCount {
  227. get {
  228. if( SelectCountMethod.Length > 0)
  229. return true;
  230. return !EnablePaging;
  231. }
  232. }
  233. public override bool CanSort {
  234. get { return true; }
  235. }
  236. public override bool CanUpdate {
  237. get { return UpdateMethod.Length > 0; }
  238. }
  239. public ConflictOptions ConflictDetection {
  240. get {
  241. object ret = ViewState ["ConflictDetection"];
  242. return ret != null ? (ConflictOptions)ret : ConflictOptions.OverwriteChanges;
  243. }
  244. set {
  245. ViewState ["ConflictDetection"] = value;
  246. }
  247. }
  248. public bool ConvertNullToDBNull {
  249. get {
  250. object ret = ViewState ["ConvertNullToDBNull"];
  251. return ret != null ? (bool)ret : false;
  252. }
  253. set {
  254. ViewState ["ConvertNullToDBNull"] = value;
  255. }
  256. }
  257. public string DataObjectTypeName {
  258. get {
  259. object ret = ViewState ["DataObjectTypeName"];
  260. return ret != null ? (string)ret : string.Empty;
  261. }
  262. set {
  263. ViewState ["DataObjectTypeName"] = value;
  264. }
  265. }
  266. public string DeleteMethod {
  267. get {
  268. object ret = ViewState ["DeleteMethod"];
  269. return ret != null ? (string)ret : string.Empty;
  270. }
  271. set {
  272. ViewState ["DeleteMethod"] = value;
  273. }
  274. }
  275. public ParameterCollection DeleteParameters {
  276. get {
  277. if (deleteParameters == null) {
  278. deleteParameters = new ParameterCollection ();
  279. deleteParameters.ParametersChanged += new EventHandler (OnParametersChanged);
  280. if (IsTrackingViewState)
  281. ((IStateManager)deleteParameters).TrackViewState ();
  282. }
  283. return deleteParameters;
  284. }
  285. }
  286. public bool EnablePaging {
  287. get {
  288. object ret = ViewState ["EnablePaging"];
  289. return ret != null ? (bool)ret : false;
  290. }
  291. set {
  292. ViewState ["EnablePaging"] = value;
  293. }
  294. }
  295. public string FilterExpression {
  296. get {
  297. object ret = ViewState ["FilterExpression"];
  298. return ret != null ? (string)ret : string.Empty;
  299. }
  300. set {
  301. ViewState ["FilterExpression"] = value;
  302. }
  303. }
  304. public ParameterCollection FilterParameters {
  305. get {
  306. if (filterParameters == null) {
  307. filterParameters = new ParameterCollection ();
  308. filterParameters.ParametersChanged += new EventHandler (OnParametersChanged);
  309. if (IsTrackingViewState)
  310. ((IStateManager)filterParameters).TrackViewState ();
  311. }
  312. return filterParameters;
  313. }
  314. }
  315. public string InsertMethod {
  316. get {
  317. object ret = ViewState ["InsertMethod"];
  318. return ret != null ? (string)ret : string.Empty;
  319. }
  320. set {
  321. ViewState ["InsertMethod"] = value;
  322. }
  323. }
  324. public ParameterCollection InsertParameters {
  325. get {
  326. if (insertParameters == null) {
  327. insertParameters = new ParameterCollection ();
  328. insertParameters.ParametersChanged += new EventHandler (OnParametersChanged);
  329. if (IsTrackingViewState)
  330. ((IStateManager)insertParameters).TrackViewState ();
  331. }
  332. return insertParameters;
  333. }
  334. }
  335. public string MaximumRowsParameterName {
  336. get {
  337. object ret = ViewState ["MaximumRowsParameterName"];
  338. return ret != null ? (string)ret : "maximumRows";
  339. }
  340. set {
  341. ViewState ["MaximumRowsParameterName"] = value;
  342. }
  343. }
  344. [DefaultValueAttribute ("{0}")]
  345. public string OldValuesParameterFormatString {
  346. get {
  347. object ret = ViewState ["OldValuesParameterFormatString"];
  348. return ret != null ? (string)ret : "{0}";
  349. }
  350. set {
  351. ViewState ["OldValuesParameterFormatString"] = value;
  352. }
  353. }
  354. public string SelectCountMethod {
  355. get {
  356. object ret = ViewState ["SelectCountMethod"];
  357. return ret != null ? (string)ret : string.Empty;
  358. }
  359. set {
  360. ViewState ["SelectCountMethod"] = value;
  361. }
  362. }
  363. public string SelectMethod {
  364. get {
  365. object ret = ViewState ["SelectMethod"];
  366. return ret != null ? (string)ret : string.Empty;
  367. }
  368. set {
  369. ViewState ["SelectMethod"] = value;
  370. }
  371. }
  372. public ParameterCollection SelectParameters {
  373. get {
  374. if (selectParameters == null) {
  375. selectParameters = new ParameterCollection ();
  376. selectParameters.ParametersChanged += new EventHandler (OnParametersChanged);
  377. if (IsTrackingViewState)
  378. ((IStateManager)selectParameters).TrackViewState ();
  379. }
  380. return selectParameters;
  381. }
  382. }
  383. public string SortParameterName {
  384. get {
  385. object ret = ViewState ["SortParameterName"];
  386. return ret != null ? (string)ret : string.Empty;
  387. }
  388. set {
  389. ViewState ["SortParameterName"] = value;
  390. }
  391. }
  392. public string StartRowIndexParameterName {
  393. get {
  394. object ret = ViewState ["StartRowIndexParameterName"];
  395. return ret != null ? (string)ret : "startRowIndex";
  396. }
  397. set {
  398. ViewState ["StartRowIndexParameterName"] = value;
  399. }
  400. }
  401. public string TypeName {
  402. get {
  403. object ret = ViewState ["TypeName"];
  404. return ret != null ? (string)ret : string.Empty;
  405. }
  406. set {
  407. ViewState ["TypeName"] = value;
  408. objectType = null;
  409. }
  410. }
  411. public string UpdateMethod {
  412. get {
  413. object ret = ViewState ["UpdateMethod"];
  414. return ret != null ? (string)ret : string.Empty;
  415. }
  416. set {
  417. ViewState ["UpdateMethod"] = value;
  418. }
  419. }
  420. public ParameterCollection UpdateParameters {
  421. get {
  422. if (updateParameters == null) {
  423. updateParameters = new ParameterCollection ();
  424. updateParameters.ParametersChanged += new EventHandler (OnParametersChanged);
  425. if (IsTrackingViewState)
  426. ((IStateManager)updateParameters).TrackViewState ();
  427. }
  428. return updateParameters;
  429. }
  430. }
  431. private static string privateBinPath;
  432. private static string PrivateBinPath
  433. {
  434. get {
  435. if (privateBinPath != null)
  436. return privateBinPath;
  437. AppDomainSetup setup = AppDomain.CurrentDomain.SetupInformation;
  438. privateBinPath = Path.Combine(setup.ApplicationBase, setup.PrivateBinPath);
  439. return privateBinPath;
  440. }
  441. }
  442. private Type LoadType(string typeName)
  443. {
  444. Type type = null;
  445. Assembly [] assemblies = AppDomain.CurrentDomain.GetAssemblies ();
  446. foreach (Assembly ass in assemblies) {
  447. type = ass.GetType (typeName);
  448. if (type == null)
  449. continue;
  450. return type;
  451. }
  452. if (!Directory.Exists (PrivateBinPath))
  453. return null;
  454. string[] binDlls = Directory.GetFiles(PrivateBinPath, "*.dll");
  455. foreach (string s in binDlls) {
  456. Assembly binA = Assembly.LoadFrom (s);
  457. type = binA.GetType (typeName);
  458. if (type == null)
  459. continue;
  460. return type;
  461. }
  462. return null;
  463. }
  464. Type ObjectType {
  465. get {
  466. if (objectType == null) {
  467. objectType = LoadType (TypeName);
  468. if (objectType == null)
  469. throw new InvalidOperationException ("Type not found: " + TypeName);
  470. }
  471. return objectType;
  472. }
  473. }
  474. Type DataObjectType {
  475. get {
  476. if (dataObjectType == null) {
  477. dataObjectType = LoadType (DataObjectTypeName);
  478. if (objectType == null)
  479. throw new InvalidOperationException ("Type not found: " + DataObjectTypeName);
  480. }
  481. return dataObjectType;
  482. }
  483. }
  484. public IEnumerable Select (DataSourceSelectArguments arguments)
  485. {
  486. return ExecuteSelect (arguments);
  487. }
  488. public int Update (IDictionary keys, IDictionary values, IDictionary oldValues)
  489. {
  490. return ExecuteUpdate (keys, values, oldValues);
  491. }
  492. public int Delete (IDictionary keys, IDictionary oldValues)
  493. {
  494. return ExecuteDelete (keys, oldValues);
  495. }
  496. public int Insert (IDictionary values)
  497. {
  498. return ExecuteInsert (values);
  499. }
  500. protected override int ExecuteInsert (IDictionary values)
  501. {
  502. if (!CanInsert)
  503. throw new NotSupportedException ("Insert operation not supported.");
  504. IOrderedDictionary paramValues;
  505. MethodInfo method;
  506. if (DataObjectTypeName.Length == 0) {
  507. paramValues = MergeParameterValues (InsertParameters, values, null, true);
  508. method = GetObjectMethod (InsertMethod, paramValues);
  509. } else {
  510. method = ResolveDataObjectMethod (InsertMethod, values, null, out paramValues);
  511. }
  512. ObjectDataSourceMethodEventArgs args = new ObjectDataSourceMethodEventArgs (paramValues);
  513. OnInserting (args);
  514. if (args.Cancel)
  515. return -1;
  516. ObjectDataSourceStatusEventArgs rargs = InvokeMethod (method, paramValues);
  517. OnInserted (rargs);
  518. if (rargs.Exception != null && !rargs.ExceptionHandled)
  519. throw rargs.Exception;
  520. OnDataSourceViewChanged (EventArgs.Empty);
  521. return -1;
  522. }
  523. protected override int ExecuteDelete (IDictionary keys, IDictionary oldValues)
  524. {
  525. if (!CanDelete)
  526. throw new NotSupportedException ("Delete operation not supported.");
  527. if (ConflictDetection == ConflictOptions.CompareAllValues && (oldValues == null || oldValues.Count == 0))
  528. throw new InvalidOperationException ("ConflictDetection is set to CompareAllValues and oldValues collection is null or empty.");
  529. IDictionary oldDataValues;
  530. if (ConflictDetection == ConflictOptions.CompareAllValues) {
  531. oldDataValues = new Hashtable ();
  532. foreach (DictionaryEntry de in keys)
  533. oldDataValues [de.Key] = de.Value;
  534. foreach (DictionaryEntry de in oldValues)
  535. oldDataValues [de.Key] = de.Value;
  536. } else
  537. oldDataValues = keys;
  538. IOrderedDictionary paramValues;
  539. MethodInfo method;
  540. if (DataObjectTypeName.Length == 0) {
  541. paramValues = MergeParameterValues (DeleteParameters, null, oldDataValues, false);
  542. method = GetObjectMethod (DeleteMethod, paramValues);
  543. } else {
  544. method = ResolveDataObjectMethod (DeleteMethod, oldDataValues, null, out paramValues);
  545. }
  546. ObjectDataSourceMethodEventArgs args = new ObjectDataSourceMethodEventArgs (paramValues);
  547. OnDeleting (args);
  548. if (args.Cancel)
  549. return -1;
  550. ObjectDataSourceStatusEventArgs rargs = InvokeMethod (method, paramValues);
  551. OnDeleted (rargs);
  552. if (rargs.Exception != null && !rargs.ExceptionHandled)
  553. throw rargs.Exception;
  554. OnDataSourceViewChanged (EventArgs.Empty);
  555. return -1;
  556. }
  557. protected override int ExecuteUpdate (IDictionary keys, IDictionary values, IDictionary oldValues)
  558. {
  559. IOrderedDictionary paramValues;
  560. MethodInfo method;
  561. if (DataObjectTypeName.Length == 0)
  562. {
  563. IDictionary dataValues;
  564. IDictionary oldDataValues;
  565. if (ConflictDetection == ConflictOptions.CompareAllValues) {
  566. oldDataValues = new Hashtable ();
  567. dataValues = values;
  568. foreach (DictionaryEntry de in keys)
  569. oldDataValues [de.Key] = de.Value;
  570. foreach (DictionaryEntry de in oldValues)
  571. oldDataValues [de.Key] = de.Value;
  572. } else {
  573. oldDataValues = keys;
  574. dataValues = values;
  575. }
  576. paramValues = MergeParameterValues (UpdateParameters, dataValues, oldDataValues, false);
  577. method = GetObjectMethod (UpdateMethod, paramValues);
  578. }
  579. else
  580. {
  581. IDictionary dataValues = new Hashtable ();
  582. IDictionary oldDataValues;
  583. foreach (DictionaryEntry de in values)
  584. dataValues [de.Key] = de.Value;
  585. if (ConflictDetection == ConflictOptions.CompareAllValues) {
  586. oldDataValues = new Hashtable ();
  587. foreach (DictionaryEntry de in keys) {
  588. oldDataValues [de.Key] = de.Value;
  589. dataValues [de.Key] = de.Value;
  590. }
  591. foreach (DictionaryEntry de in oldValues)
  592. oldDataValues [de.Key] = de.Value;
  593. } else {
  594. oldDataValues = null;
  595. foreach (DictionaryEntry de in keys)
  596. dataValues [de.Key] = de.Value;
  597. }
  598. method = ResolveDataObjectMethod (UpdateMethod, dataValues, oldDataValues, out paramValues);
  599. }
  600. ObjectDataSourceMethodEventArgs args = new ObjectDataSourceMethodEventArgs (paramValues);
  601. OnUpdating (args);
  602. if (args.Cancel)
  603. return -1;
  604. ObjectDataSourceStatusEventArgs rargs = InvokeMethod (method, paramValues);
  605. OnUpdated (rargs);
  606. if (rargs.Exception != null && !rargs.ExceptionHandled)
  607. throw rargs.Exception;
  608. OnDataSourceViewChanged (EventArgs.Empty);
  609. return -1;
  610. }
  611. protected internal override IEnumerable ExecuteSelect (DataSourceSelectArguments arguments)
  612. {
  613. arguments.RaiseUnsupportedCapabilitiesError (this);
  614. IOrderedDictionary paramValues = MergeParameterValues (SelectParameters, null, null, true);
  615. ObjectDataSourceSelectingEventArgs args = new ObjectDataSourceSelectingEventArgs (paramValues, arguments, false);
  616. OnSelecting (args);
  617. if (args.Cancel)
  618. return new ArrayList ();
  619. if (CanRetrieveTotalRowCount && arguments.RetrieveTotalRowCount)
  620. arguments.TotalRowCount = QueryTotalRowCount (paramValues, arguments);
  621. if (CanPage) {
  622. if (StartRowIndexParameterName.Length == 0)
  623. throw new InvalidOperationException ("Paging is enabled, but the StartRowIndexParameterName property is not set.");
  624. if (MaximumRowsParameterName.Length == 0)
  625. throw new InvalidOperationException ("Paging is enabled, but the MaximumRowsParameterName property is not set.");
  626. paramValues [StartRowIndexParameterName] = arguments.StartRowIndex;
  627. paramValues [MaximumRowsParameterName] = arguments.MaximumRows;
  628. }
  629. if (SortParameterName.Length > 0)
  630. paramValues [SortParameterName] = arguments.SortExpression;
  631. object result = InvokeSelect (SelectMethod, paramValues);
  632. if (result is DataSet) {
  633. DataSet dset = (DataSet) result;
  634. if (dset.Tables.Count == 0)
  635. throw new InvalidOperationException ("The select method returnet a DataSet which doesn't contain any table.");
  636. result = dset.Tables [0];
  637. }
  638. if (result is DataTable) {
  639. DataView dview = new DataView ((DataTable)result);
  640. if (arguments.SortExpression != null && arguments.SortExpression.Length > 0) {
  641. dview.Sort = arguments.SortExpression;
  642. }
  643. if (FilterExpression.Length > 0) {
  644. IOrderedDictionary fparams = FilterParameters.GetValues (context, owner);
  645. ObjectDataSourceFilteringEventArgs fargs = new ObjectDataSourceFilteringEventArgs (fparams);
  646. OnFiltering (fargs);
  647. if (!fargs.Cancel) {
  648. object[] formatValues = new object [fparams.Count];
  649. for (int n=0; n<formatValues.Length; n++) {
  650. formatValues [n] = fparams [n];
  651. if (formatValues [n] == null) return dview;
  652. }
  653. dview.RowFilter = string.Format (FilterExpression, formatValues);
  654. }
  655. }
  656. return dview;
  657. }
  658. if (result is IEnumerable)
  659. return (IEnumerable) result;
  660. else
  661. return new object[] { result };
  662. }
  663. int QueryTotalRowCount (IOrderedDictionary mergedParameters, DataSourceSelectArguments arguments)
  664. {
  665. ObjectDataSourceSelectingEventArgs countArgs = new ObjectDataSourceSelectingEventArgs (mergedParameters, arguments, true);
  666. OnSelecting (countArgs);
  667. if (countArgs.Cancel)
  668. return 0;
  669. object count = InvokeSelect (SelectCountMethod, mergedParameters);
  670. return (int) Convert.ChangeType (count, typeof(int));
  671. }
  672. object InvokeSelect (string methodName, IOrderedDictionary paramValues)
  673. {
  674. MethodInfo method = GetObjectMethod (methodName, paramValues);
  675. ObjectDataSourceStatusEventArgs rargs = InvokeMethod (method, paramValues);
  676. OnSelected (rargs);
  677. if (rargs.Exception != null && !rargs.ExceptionHandled)
  678. throw rargs.Exception;
  679. return rargs.ReturnValue;
  680. }
  681. ObjectDataSourceStatusEventArgs InvokeMethod (MethodInfo method, IOrderedDictionary paramValues)
  682. {
  683. object instance = null;
  684. if (!method.IsStatic)
  685. instance = CreateObjectInstance ();
  686. ParameterInfo[] pars = method.GetParameters ();
  687. ArrayList outParamInfos;
  688. object[] methodArgs = GetParameterArray (pars, paramValues, out outParamInfos);
  689. if (methodArgs == null)
  690. throw CreateMethodException (method.Name, paramValues);
  691. object result = null;
  692. Hashtable outParams = null;
  693. try {
  694. result = method.Invoke (instance, methodArgs);
  695. if (outParamInfos != null) {
  696. outParams = new Hashtable ();
  697. foreach (ParameterInfo op in outParamInfos)
  698. outParams [op.Name] = methodArgs [op.Position - 1];
  699. }
  700. return new ObjectDataSourceStatusEventArgs (result, outParams, null);
  701. }
  702. catch (Exception ex) {
  703. return new ObjectDataSourceStatusEventArgs (result, outParams, ex);
  704. }
  705. finally {
  706. if (instance != null)
  707. DisposeObjectInstance (instance);
  708. }
  709. }
  710. MethodInfo GetObjectMethod (string methodName, IOrderedDictionary parameters)
  711. {
  712. MemberInfo[] methods = ObjectType.GetMember (methodName, BindingFlags.Instance |
  713. BindingFlags.Static |
  714. BindingFlags.Public |
  715. BindingFlags.FlattenHierarchy);
  716. if (methods.Length > 1) {
  717. // MSDN: The ObjectDataSource resolves method overloads by method name and number
  718. // of parameters; the names and types of the parameters are not considered.
  719. foreach (MemberInfo mi in methods) {
  720. MethodInfo me = mi as MethodInfo;
  721. if (me != null && me.GetParameters().Length == parameters.Count)
  722. return me;
  723. }
  724. }
  725. else if (methods.Length == 1) {
  726. MethodInfo me = methods[0] as MethodInfo;
  727. if (me != null && me.GetParameters().Length == parameters.Count)
  728. return me;
  729. }
  730. throw CreateMethodException (methodName, parameters);
  731. }
  732. MethodInfo ResolveDataObjectMethod (string methodName, IDictionary values, IDictionary oldValues, out IOrderedDictionary paramValues)
  733. {
  734. MethodInfo method;
  735. if (oldValues != null)
  736. method = ObjectType.GetMethod (methodName, new Type[] { DataObjectType, DataObjectType });
  737. else
  738. method = ObjectType.GetMethod (methodName, new Type[] { DataObjectType });
  739. if (method == null)
  740. throw new InvalidOperationException ("ObjectDataSource " + owner.ID + " could not find a method named '" + methodName + "' with parameters of type '" + DataObjectType + "' in '" + ObjectType + "'.");
  741. paramValues = new OrderedDictionary ();
  742. ParameterInfo[] ps = method.GetParameters ();
  743. if (oldValues != null) {
  744. if (FormatOldParameter (ps[0].Name) == ps[1].Name) {
  745. paramValues [ps[0].Name] = CreateDataObject (values);
  746. paramValues [ps[1].Name] = CreateDataObject (oldValues);
  747. } else if (FormatOldParameter (ps[1].Name) == ps[0].Name) {
  748. paramValues [ps[0].Name] = CreateDataObject (oldValues);
  749. paramValues [ps[1].Name] = CreateDataObject (values);
  750. } else
  751. throw new InvalidOperationException ("Method '" + methodName + "' does not have any parameter that fits the value of OldValuesParameterFormatString.");
  752. } else {
  753. paramValues [ps[0].Name] = CreateDataObject (values);
  754. }
  755. return method;
  756. }
  757. Exception CreateMethodException (string methodName, IOrderedDictionary parameters)
  758. {
  759. string s = "";
  760. foreach (string p in parameters.Keys) {
  761. s += p + ", ";
  762. }
  763. return new InvalidOperationException ("ObjectDataSource " + owner.ID + " could not find a method named '" + methodName + "' with parameters " + s + "in type '" + ObjectType + "'.");
  764. }
  765. object CreateDataObject (IDictionary values)
  766. {
  767. object ob = Activator.CreateInstance (DataObjectType);
  768. foreach (DictionaryEntry de in values) {
  769. PropertyInfo p = DataObjectType.GetProperty ((string)de.Key);
  770. if (p == null) throw new InvalidOperationException ("Property " + de.Key + " not found in type '" +DataObjectType + "'.");
  771. p.SetValue (ob, ConvertParameter (p.PropertyType, de.Value), null);
  772. }
  773. return ob;
  774. }
  775. object CreateObjectInstance ()
  776. {
  777. ObjectDataSourceEventArgs args = new ObjectDataSourceEventArgs (null);
  778. OnObjectCreating (args);
  779. if (args.ObjectInstance != null)
  780. return args.ObjectInstance;
  781. object ob = Activator.CreateInstance (ObjectType);
  782. args.ObjectInstance = ob;
  783. OnObjectCreated (args);
  784. return args.ObjectInstance;
  785. }
  786. void DisposeObjectInstance (object obj)
  787. {
  788. ObjectDataSourceDisposingEventArgs args = new ObjectDataSourceDisposingEventArgs (obj);
  789. OnObjectDisposing (args);
  790. if (!args.Cancel) {
  791. IDisposable disp = obj as IDisposable;
  792. if (disp != null) disp.Dispose ();
  793. }
  794. }
  795. /// <summary>
  796. /// Merge the current data item fields with view parameter default values
  797. /// </summary>
  798. /// <param name="viewParams">default parameters</param>
  799. /// <param name="values">new parameters for update and insert</param>
  800. /// <param name="oldValues">old parameters for update and delete</param>
  801. /// <param name="allwaysAddNewValues">true for insert, as current item is
  802. /// irrelevant for insert</param>
  803. /// <returns>merged values</returns>
  804. IOrderedDictionary MergeParameterValues (ParameterCollection viewParams, IDictionary values, IDictionary oldValues, bool allwaysAddNewValues)
  805. {
  806. OrderedDictionary mergedValues = new OrderedDictionary ();
  807. foreach (Parameter p in viewParams) {
  808. bool oldAdded = false;
  809. if (oldValues != null && oldValues.Contains (p.Name)) {
  810. object val = Convert.ChangeType (oldValues [p.Name], p.Type);
  811. mergedValues [FormatOldParameter (p.Name)] = val;
  812. oldAdded = true;
  813. }
  814. if (values != null && values.Contains (p.Name)) {
  815. object val = Convert.ChangeType (values [p.Name], p.Type);
  816. mergedValues [p.Name] = val;
  817. } else if (!oldAdded || allwaysAddNewValues) {
  818. object val = p.GetValue (context, owner);
  819. mergedValues [p.Name] = val;
  820. }
  821. }
  822. if (values != null) {
  823. foreach (DictionaryEntry de in values)
  824. if (!mergedValues.Contains (de.Key))
  825. mergedValues [de.Key] = de.Value;
  826. }
  827. if (oldValues != null) {
  828. foreach (DictionaryEntry de in oldValues)
  829. if (!mergedValues.Contains (FormatOldParameter ((string)de.Key)))
  830. mergedValues [FormatOldParameter ((string)de.Key)] = de.Value;
  831. }
  832. return mergedValues;
  833. }
  834. object[] GetParameterArray (ParameterInfo[] methodParams, IOrderedDictionary viewParams, out ArrayList outParamInfos)
  835. {
  836. // FIXME: make this case insensitive
  837. outParamInfos = null;
  838. object[] values = new object [methodParams.Length];
  839. foreach (ParameterInfo mp in methodParams) {
  840. // Parameter names must match
  841. if (!viewParams.Contains (mp.Name)) return null;
  842. values [mp.Position] = ConvertParameter (mp.ParameterType, viewParams [mp.Name]);
  843. if (mp.ParameterType.IsByRef) {
  844. if (outParamInfos == null) outParamInfos = new ArrayList ();
  845. outParamInfos.Add (mp);
  846. }
  847. }
  848. return values;
  849. }
  850. object ConvertParameter (Type targetType, object value)
  851. {
  852. return ConvertParameter (Type.GetTypeCode (targetType), value);
  853. }
  854. object ConvertParameter (TypeCode targetType, object value)
  855. {
  856. if (value == null) {
  857. if (targetType != TypeCode.Object && targetType != TypeCode.String)
  858. value = 0;
  859. else if (targetType == TypeCode.Object && ConvertNullToDBNull)
  860. return DBNull.Value;
  861. }
  862. if (targetType == TypeCode.Object)
  863. return value;
  864. else
  865. return Convert.ChangeType (value, targetType);
  866. }
  867. string FormatOldParameter (string name)
  868. {
  869. string f = OldValuesParameterFormatString;
  870. if (f.Length > 0)
  871. return String.Format (f, name);
  872. else
  873. return name;
  874. }
  875. void OnParametersChanged (object sender, EventArgs args)
  876. {
  877. OnDataSourceViewChanged (EventArgs.Empty);
  878. }
  879. protected virtual void LoadViewState (object savedState)
  880. {
  881. object[] state = (savedState == null) ? new object [6] : (object[]) savedState;
  882. viewState.LoadViewState (state[0]);
  883. ((IStateManager)SelectParameters).LoadViewState (state[1]);
  884. ((IStateManager)UpdateParameters).LoadViewState (state[2]);
  885. ((IStateManager)DeleteParameters).LoadViewState (state[3]);
  886. ((IStateManager)InsertParameters).LoadViewState (state[4]);
  887. ((IStateManager)FilterParameters).LoadViewState (state[5]);
  888. }
  889. protected virtual object SaveViewState()
  890. {
  891. object[] state = new object [6];
  892. state [0] = viewState.SaveViewState ();
  893. if (selectParameters != null)
  894. state [1] = ((IStateManager)selectParameters).SaveViewState ();
  895. if (updateParameters != null)
  896. state [2] = ((IStateManager)updateParameters).SaveViewState ();
  897. if (deleteParameters != null)
  898. state [3] = ((IStateManager)deleteParameters).SaveViewState ();
  899. if (insertParameters != null)
  900. state [4] = ((IStateManager)insertParameters).SaveViewState ();
  901. if (filterParameters != null)
  902. state [5] = ((IStateManager)filterParameters).SaveViewState ();
  903. foreach (object ob in state)
  904. if (ob != null) return state;
  905. return null;
  906. }
  907. protected virtual void TrackViewState()
  908. {
  909. viewState.TrackViewState ();
  910. if (selectParameters != null) ((IStateManager)selectParameters).TrackViewState ();
  911. if (updateParameters != null) ((IStateManager)updateParameters).TrackViewState ();
  912. if (deleteParameters != null) ((IStateManager)deleteParameters).TrackViewState ();
  913. if (insertParameters != null) ((IStateManager)insertParameters).TrackViewState ();
  914. if (filterParameters != null) ((IStateManager)filterParameters).TrackViewState ();
  915. }
  916. protected bool IsTrackingViewState
  917. {
  918. get { return viewState.IsTrackingViewState; }
  919. }
  920. bool IStateManager.IsTrackingViewState
  921. {
  922. get { return IsTrackingViewState; }
  923. }
  924. void IStateManager.TrackViewState()
  925. {
  926. TrackViewState ();
  927. }
  928. void IStateManager.LoadViewState (object savedState)
  929. {
  930. LoadViewState (savedState);
  931. }
  932. object IStateManager.SaveViewState()
  933. {
  934. return SaveViewState ();
  935. }
  936. }
  937. }
  938. #endif