Advertisement
Guest User

Untitled

a guest
Aug 2nd, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. public void Test()
  2. {
  3. var items = new List<EntityA>();
  4. for (int i = 0; i < 100000; i++)
  5. {
  6. items.Add(new EntityA { FirstName = "A" + i });
  7. }
  8.  
  9. ManualResetEvent stopChangingMappingFunction = new ManualResetEvent(false);
  10.  
  11. Thread t1 = new Thread(() =>
  12. {
  13.  
  14. int i = 1;
  15. while (true)
  16. {
  17. if (stopChangingMappingFunction.WaitOne(TimeSpan.Zero))
  18. return;
  19.  
  20. var i1 = i++;
  21. Mapper.CreateMap<EntityA, EntityB>().ForMember(x => x.Age, y => y.ResolveUsing(new Func<EntityA, object>(a => i1)));
  22. }
  23. });
  24.  
  25. Thread t2 = new Thread(() =>
  26. {
  27. int i = -1;
  28. while (true)
  29. {
  30. if (stopChangingMappingFunction.WaitOne(TimeSpan.Zero))
  31. return;
  32.  
  33. var i1 = i--;
  34. Mapper.CreateMap<EntityA, EntityB>().ForMember(x => x.Age, y => y.ResolveUsing(new Func<EntityA, object>(a => i1)));
  35. }
  36. });
  37.  
  38. List<int> distinctAges1 = null;
  39. List<int> distinctAges2 = null;
  40.  
  41. Thread t3 = new Thread(() =>
  42. {
  43. Thread.Sleep(1000);
  44.  
  45. var res = Mapper.Map<IList<EntityA>, IList<EntityB>>(items);
  46. distinctAges1 = res.Select(x => x.Age).Distinct().ToList();
  47.  
  48. Thread.Sleep(1000);
  49.  
  50. var res2 = Mapper.Map<IList<EntityA>, IList<EntityB>>(items);
  51. distinctAges2 = res.Select(x => x.Age).Distinct().ToList();
  52.  
  53. stopChangingMappingFunction.Set();
  54. });
  55.  
  56. t1.Start();
  57. t2.Start();
  58. t3.Start();
  59.  
  60. t1.Join();
  61. t2.Join();
  62. t3.Join();
  63.  
  64. Console.WriteLine("First Mapping: " + string.Join(", ", distinctAges1.ToArray()));
  65. Console.WriteLine("Second Mapping: " + string.Join(", ", distinctAges2.ToArray()));
  66. Console.ReadKey();
  67. }
  68.  
  69. public class EntityA
  70. {
  71. public string FirstName { get; set; }
  72. }
  73.  
  74. public class EntityB
  75. {
  76. public string FirstName { get; set; }
  77. public int Age { get; set; }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement