PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/System.Data.Linq/src/DbLinq/Data/Linq/Identity/Implementation/IdentityReader.cs

https://bitbucket.org/danipen/mono
C# | 83 lines | 39 code | 5 blank | 39 comment | 4 complexity | 21c9540ffbd0cff80d0afe1aaea996ec MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. #region MIT license
  2. //
  3. // MIT license
  4. //
  5. // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. #endregion
  26. using System;
  27. using System.Reflection;
  28. using System.Collections.Generic;
  29. using DbLinq.Data.Linq.Identity;
  30. using DbLinq.Util;
  31. #if MONO_STRICT
  32. using System.Data.Linq;
  33. #else
  34. using DbLinq.Data.Linq;
  35. #endif
  36. namespace DbLinq.Data.Linq.Identity.Implementation
  37. {
  38. /// <summary>
  39. /// IIdentityReader default implementation
  40. /// Currently uses reflection
  41. /// TODO: use compilation
  42. /// </summary>
  43. internal class IdentityReader : IIdentityReader
  44. {
  45. private readonly Type type;
  46. private readonly IList<MemberInfo> keyMembers = new List<MemberInfo>();
  47. /// <summary>
  48. /// Gets an object identity
  49. /// </summary>
  50. /// <param name="entity"></param>
  51. /// <returns></returns>
  52. public IdentityKey GetIdentityKey(object entity)
  53. {
  54. // no PK? --> null as identity (==we can not collect it)
  55. if (keyMembers.Count == 0)
  56. return null;
  57. var keys = new List<object>();
  58. foreach (var keyMember in keyMembers)
  59. {
  60. var key = keyMember.GetMemberValue(entity);
  61. if(key != null)
  62. keys.Add(key);
  63. }
  64. return new IdentityKey(type, keys);
  65. }
  66. /// <summary>
  67. /// Initializes a new instance of the <see cref="IdentityReader"/> class.
  68. /// </summary>
  69. /// <param name="t">The t.</param>
  70. /// <param name="dataContext">The data context.</param>
  71. public IdentityReader(Type t, DataContext dataContext)
  72. {
  73. foreach (var member in dataContext.Mapping.GetTable(t).RowType.IdentityMembers)
  74. keyMembers.Add(member.Member);
  75. type = t;
  76. }
  77. }
  78. }