Context.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using FrameworkBench;
  3. using Revenj.DatabasePersistence;
  4. using Revenj.DomainPatterns;
  5. using Revenj.Extensibility;
  6. namespace Revenj.Bench
  7. {
  8. internal class Context
  9. {
  10. public readonly IPersistableRepository<World> WorldRepository;
  11. public readonly IQueryableRepository<Fortune> FortuneRepository;
  12. public readonly Random Random = new Random(0);
  13. public readonly World[] Worlds = new World[512];
  14. //private readonly IRepositoryBulkReader BulkReader;
  15. //private readonly Lazy<World>[] LazyWorlds = new Lazy<World>[512];
  16. public Context(IObjectFactory factory, IDatabaseQueryManager manager)
  17. {
  18. var scope = factory.CreateScope(null);
  19. scope.RegisterInterfaces(manager.StartQuery(false));
  20. WorldRepository = scope.Resolve<IPersistableRepository<World>>();
  21. FortuneRepository = scope.Resolve<IQueryableRepository<Fortune>>();
  22. //BulkReader = scope.BulkRead(ChunkedMemoryStream.Static());
  23. }
  24. /* bulk loading of worlds. use such pattern for production code */
  25. /*public void LoadWorldsFast(int repeat, World[] worlds)
  26. {
  27. BulkReader.Reset(true);
  28. for (int i = 0; i < repeat; i++)
  29. {
  30. var id = Random.Next(10000) + 1;
  31. LazyWorlds[i] = BulkReader.Find<World>(id.ToString());
  32. }
  33. BulkReader.Execute();
  34. for (int i = 0; i < repeat; i++)
  35. worlds[i] = LazyWorlds[i].Value;
  36. }*/
  37. /* multiple roundtrips loading of worlds. don't write such production code */
  38. public void LoadWorldsSlow(int repeat, World[] worlds)
  39. {
  40. for (int i = 0; i < repeat; i++)
  41. {
  42. var id = Random.Next(10000) + 1;
  43. worlds[i] = WorldRepository.Find(id);
  44. }
  45. }
  46. }
  47. }