PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/FluentNHibernate/Testing/PersistenceSpecification.cs

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