StateFactory.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using Wayne.Lib.StateEngine.Generic;
  6. using System.Diagnostics;
  7. namespace Wayne.Lib.StateEngine
  8. {
  9. /// <summary>
  10. /// State factory with the Main object T
  11. /// </summary>
  12. /// <typeparam name="T"></typeparam>
  13. public class StateFactory<T> : IStateFactory2
  14. {
  15. private readonly Func<T> main;
  16. private readonly IServiceLocator serviceContainer;
  17. /// <summary>
  18. /// Constructor
  19. /// </summary>
  20. /// <param name="main"></param>
  21. /// <param name="serviceContainer"></param>
  22. public StateFactory(T main, IServiceLocator serviceContainer)
  23. {
  24. this.main = () => main;
  25. this.serviceContainer = serviceContainer;
  26. }
  27. /// <summary>
  28. /// Constructor
  29. /// </summary>
  30. /// <param name="mainFunc"></param>
  31. /// <param name="serviceContainer"></param>
  32. public StateFactory(Func<T> mainFunc, IServiceLocator serviceContainer)
  33. {
  34. this.main = mainFunc;
  35. this.serviceContainer = serviceContainer;
  36. }
  37. #region Implementation of IStateFactory
  38. /// <summary>
  39. /// Tries to create the requested state
  40. /// </summary>
  41. /// <param name="stateFactoryName"></param>
  42. /// <returns></returns>
  43. public State CreateState(string stateFactoryName)
  44. {
  45. throw new InvalidOperationException("Use CreateState with StateTypeContainer");
  46. }
  47. private State TryCreate(Type stateType)
  48. {
  49. var constructors = stateType.GetConstructors();
  50. var lastConstructor = constructors[constructors.Length - 1];
  51. var parameterInfos = lastConstructor.GetParameters();
  52. List<object> parameters = new List<object>();
  53. foreach (var parameterInfo in parameterInfos)
  54. {
  55. try
  56. {
  57. parameters.Add(serviceContainer.GetService(parameterInfo.ParameterType));
  58. }
  59. catch (ServiceContainerException ex)
  60. {
  61. Debug.WriteLine(string.Format("Creating state {0} because its dependency {1} could not be found in the container. Error message : {2}", stateType, parameterInfo.ParameterType, ex.Message));
  62. throw;
  63. }
  64. }
  65. var stateObject = lastConstructor.Invoke(parameters.ToArray());
  66. return stateObject as State;
  67. }
  68. /// <summary>
  69. /// Prepare a state with a Main object if it has got one.
  70. /// </summary>
  71. /// <param name="stateObject"></param>
  72. protected virtual void PrepareState(object stateObject)
  73. {
  74. IGenericState<T> genericClientState = stateObject as IGenericState<T>;
  75. if (genericClientState != null)
  76. genericClientState.WritableMain = main();
  77. }
  78. /// <summary>
  79. /// Name of the State Factory
  80. /// </summary>
  81. public string Name
  82. {
  83. get { return GetType().Name; }
  84. }
  85. /// <summary>
  86. /// Creates the given state using the supplied StateTypeContainer
  87. /// </summary>
  88. /// <param name="stateFactoryName"></param>
  89. /// <param name="stateTypeContainer"></param>
  90. /// <returns></returns>
  91. public State CreateState(string stateFactoryName, StateTypeContainer stateTypeContainer)
  92. {
  93. Type stateType;
  94. if (stateTypeContainer.TryGetStateType(stateFactoryName, out stateType))
  95. {
  96. //Check if Main is of same type as T.
  97. List<Type> ancestry = new List<Type>();
  98. var mainObjectValue = main();
  99. Type type = mainObjectValue.GetType();
  100. do
  101. {
  102. ancestry.Add(type);
  103. type = type.BaseType;
  104. } while (type != typeof(object));
  105. State state = TryCreate(stateType);
  106. foreach (Type t in ancestry)
  107. {
  108. Type genericType = typeof(IGenericState<>);
  109. Type actualType = genericType.MakeGenericType(t);
  110. if (actualType.IsInstanceOfType(state))
  111. {
  112. PropertyInfo propertyInfo = actualType.GetProperty("WritableMain");
  113. propertyInfo.SetValue(state, mainObjectValue, new object[] { });
  114. }
  115. }
  116. return state;
  117. }
  118. return null;
  119. }
  120. #endregion
  121. }
  122. }