PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/FluentNHibernate/Testing/PersistenceSpecification.cs

https://github.com/signoredems/fluent-nhibernate
C# | 99 lines | 74 code | 17 blank | 8 comment | 3 complexity | eb39664ea0abd8bc9732d80796ea5dd1 MD5 | raw file
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using FluentNHibernate.Testing.Values;
  4. using FluentNHibernate.Utils;
  5. using NHibernate;
  6. using System;
  7. namespace FluentNHibernate.Testing
  8. {
  9. public class PersistenceSpecification<T>
  10. {
  11. protected readonly List<Property<T>> allProperties = new List<Property<T>>();
  12. private readonly ISession currentSession;
  13. private readonly IEqualityComparer entityEqualityComparer;
  14. private readonly bool hasExistingSession;
  15. public PersistenceSpecification(ISessionSource source)
  16. : this(source.CreateSession())
  17. {
  18. }
  19. public PersistenceSpecification(ISessionSource source, IEqualityComparer entityEqualityComparer)
  20. : this(source.CreateSession(), entityEqualityComparer)
  21. {
  22. }
  23. public PersistenceSpecification(ISession session)
  24. : this(session, null)
  25. {
  26. }
  27. public PersistenceSpecification(ISession session, IEqualityComparer entityEqualityComparer)
  28. {
  29. currentSession = session;
  30. hasExistingSession = currentSession.Transaction != null && currentSession.Transaction.IsActive;
  31. this.entityEqualityComparer = entityEqualityComparer;
  32. }
  33. public void VerifyTheMappings()
  34. {
  35. VerifyTheMappings(typeof(T).InstantiateUsingParameterlessConstructor<T>());
  36. }
  37. public void VerifyTheMappings(T first)
  38. {
  39. // Set the "suggested" properties, including references
  40. // to other entities and possibly collections
  41. allProperties.ForEach(p => p.SetValue(first));
  42. // Save the first copy
  43. TransactionalSave(first);
  44. object firstId = currentSession.GetIdentifier(first);
  45. // Clear and reset the current session
  46. currentSession.Flush();
  47. currentSession.Clear();
  48. // "Find" the same entity from the second IRepository
  49. var second = currentSession.Get<T>(firstId);
  50. // Validate that each specified property and value
  51. // made the round trip
  52. // It's a bit naive right now because it fails on the first failure
  53. allProperties.ForEach(p => p.CheckValue(second));
  54. }
  55. public void TransactionalSave(object propertyValue)
  56. {
  57. if (hasExistingSession)
  58. {
  59. currentSession.Save(propertyValue);
  60. }
  61. else
  62. {
  63. using (var tx = currentSession.BeginTransaction())
  64. {
  65. currentSession.Save(propertyValue);
  66. tx.Commit();
  67. }
  68. }
  69. }
  70. public PersistenceSpecification<T> RegisterCheckedProperty(Property<T> property)
  71. {
  72. return RegisterCheckedProperty(property, null);
  73. }
  74. public PersistenceSpecification<T> RegisterCheckedProperty(Property<T> property, IEqualityComparer equalityComparer)
  75. {
  76. property.EntityEqualityComparer = equalityComparer ?? entityEqualityComparer;
  77. allProperties.Add(property);
  78. property.HasRegistered(this);
  79. return this;
  80. }
  81. }
  82. }