Guest User

Untitled

a guest
Jul 16th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Client.Framework;
  6. using NHibernate;
  7.  
  8. namespace Example.Specs.Creators
  9. {
  10. // Creator<T> provides an framework for defining streamlined, *test-only* factories for domain entities..
  11. // it mostly provides needed infrastructure that a test harness can hook into to make it possible to
  12. // create and persist Nhib entities in a test environment.
  13. public class Creator<T>
  14. {
  15. // the only thing worth knowing about ISessionProvider is that it wraps an NHib ISession
  16. ISessionProvider _sessionProvider;
  17. // This represents the various databases that the client has
  18. DatabaseName _db;
  19.  
  20. // we keep an instance of the FluentFixtures ourselves as a member here so that a Creator
  21. // has access to New, the same as the test code shown in fluent_fixtures_example.cs
  22. FluentFixtures _fixtures;
  23. public FluentFixtures New { get { return _fixtures; } }
  24.  
  25. public Creator(ISessionProvider sessionProvider, DatabaseName db,FluentFixtures fixtures)
  26. {
  27. _sessionProvider = sessionProvider;
  28. _db = db;
  29. _fixtures = fixtures;
  30. }
  31.  
  32. public void Flush()
  33. {
  34. Session.Flush();
  35. }
  36.  
  37. public void Evict()
  38. {
  39. Session.Evict(Creation);
  40. }
  41.  
  42. public void Save()
  43. {
  44. Session.Save(Creation);
  45. // we're flushing left-and-right for convenience.. not performant at all and
  46. // obviously not aprop. for production use.
  47. Flush();
  48. }
  49.  
  50. public K SimpleChange<K>(Action<T> input) where K: Creator<T>
  51. {
  52. input(Creation);
  53. Save();
  54. return (K)this;
  55. }
  56.  
  57. public T Creation { get; set; }
  58.  
  59. public ISession Session { get { return _sessionProvider.CurrentSessionOn(_db); } }
  60.  
  61. public static implicit operator T (Creator<T> input)
  62. {
  63. return input.Creation;
  64. }
  65. }
  66. }
Add Comment
Please, Sign In to add comment