PageRenderTime 57ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ZebraFlickr/Core/ChangeTracker.cs

https://bitbucket.org/garethl/zebraflickr
C# | 124 lines | 73 code | 21 blank | 30 comment | 18 complexity | df21bfab0c90afc51edc21b6637b5144 MD5 | raw file
  1. #region License
  2. // Copyright (c) 2012 Gareth Lennox (garethl@dwakn.com)
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without modification,
  6. // are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Gareth Lennox nor the names of its
  14. // contributors may be used to endorse or promote products derived from this
  15. // software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  21. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #endregion
  28. using System;
  29. using System.Collections;
  30. using LinFu.DynamicProxy;
  31. namespace ZebraFlickr.Core
  32. {
  33. public class ChangeTracker<T> : IDisposable, IInvokeWrapper
  34. where T : class
  35. {
  36. private static readonly ProxyFactory _proxyFactory = new ProxyFactory();
  37. private T _proxy;
  38. private T _value;
  39. public ChangeTracker(T value)
  40. {
  41. _value = value;
  42. _proxy = _proxyFactory.CreateProxy<T>(new CallAdapter(this));
  43. }
  44. public T Wrapped
  45. {
  46. get { return _proxy; }
  47. }
  48. #region Implementation of IInvokeWrapper
  49. public void BeforeInvoke(InvocationInfo info)
  50. {
  51. if (info.TargetMethod.Name.StartsWith("set_") && info.Arguments.Length == 1)
  52. {
  53. var result = typeof (T).GetMethod("get_" + info.TargetMethod.Name.Substring(4)).Invoke(_value, null);
  54. if (result is Array)
  55. {
  56. var a = (object[])info.Arguments[0];
  57. var b = (object[])result;
  58. if (a.Length == b.Length)
  59. {
  60. for (int x = 0; x < a.Length; x++)
  61. {
  62. if (Comparer.Default.Compare(a[x], b[x]) != 0)
  63. {
  64. if (Changed != null)
  65. Changed();
  66. return;
  67. }
  68. }
  69. return;
  70. }
  71. if (Changed != null)
  72. Changed();
  73. }
  74. else if (Comparer.Default.Compare(info.Arguments[0], result) != 0)
  75. {
  76. if (Changed != null)
  77. Changed();
  78. }
  79. }
  80. }
  81. public object DoInvoke(InvocationInfo info)
  82. {
  83. return info.TargetMethod.Invoke(_value, info.Arguments);
  84. }
  85. public void AfterInvoke(InvocationInfo info, object returnValue)
  86. {
  87. //nothing
  88. }
  89. #endregion
  90. public event Action Changed;
  91. #region Implementation of IDisposable
  92. /// <summary>
  93. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  94. /// </summary>
  95. /// <filterpriority>2</filterpriority>
  96. public void Dispose()
  97. {
  98. _proxy = null;
  99. _value = null;
  100. }
  101. #endregion
  102. }
  103. }