/src/LinFu.IoC/ServiceInfo.cs

http://github.com/philiplaureano/LinFu · C# · 112 lines · 54 code · 17 blank · 41 comment · 7 complexity · 6105d8f2e32f8c40c8c8a35051ff2dd2 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using LinFu.IoC.Interfaces;
  5. namespace LinFu.IoC
  6. {
  7. /// <summary>
  8. /// Represents the default implementation of the ServiceInfo class.
  9. /// </summary>
  10. internal class ServiceInfo : IServiceInfo
  11. {
  12. /// <summary>
  13. /// Initializes the class with the given <paramref name="serviceName" />
  14. /// and <paramref name="serviceType" />.
  15. /// </summary>
  16. /// <param name="serviceName">The name of the service.</param>
  17. /// <param name="serviceType">The type of service that can be created.</param>
  18. public ServiceInfo(string serviceName, Type serviceType)
  19. {
  20. ServiceType = serviceType;
  21. ServiceName = serviceName;
  22. ArgumentTypes = new Type[0];
  23. }
  24. /// <summary>
  25. /// Initializes the class with the given <paramref name="serviceName" />
  26. /// and <paramref name="serviceType" />.
  27. /// </summary>
  28. /// <param name="serviceName">The name of the service.</param>
  29. /// <param name="serviceType">The type of service that can be created.</param>
  30. /// <param name="arguments">The parameter types required by the given service.</param>
  31. public ServiceInfo(string serviceName, Type serviceType, IEnumerable<Type> arguments)
  32. {
  33. ServiceType = serviceType;
  34. ServiceName = serviceName;
  35. ArgumentTypes = arguments;
  36. }
  37. /// <summary>
  38. /// The name of the service being created. By default, this property is blank.
  39. /// </summary>
  40. public string ServiceName { get; }
  41. /// <summary>
  42. /// The type of service being requested.
  43. /// </summary>
  44. public Type ServiceType { get; }
  45. /// <summary>
  46. /// Gets a value indicating the list of arguments required by this particular service.
  47. /// </summary>
  48. public IEnumerable<Type> ArgumentTypes { get; }
  49. /// <summary>
  50. /// Displays the name of the current service and the current service type.
  51. /// </summary>
  52. /// <returns>The name of the current service and the current service type.</returns>
  53. public override string ToString()
  54. {
  55. return string.Format("Service Name: '{0}', Service Type = '{1}'", ServiceName,
  56. ServiceType.AssemblyQualifiedName);
  57. }
  58. /// <summary>
  59. /// Determines if the other object is equal to the current <see cref="IServiceInfo" /> instance.
  60. /// </summary>
  61. /// <param name="obj">The other object that will be used in the comparison.</param>
  62. /// <returns>
  63. /// Returns <c>true</c> if both instances have the same service name, implement the same service type and have the
  64. /// same arguments; otherwise, it will return <c>false</c>.
  65. /// </returns>
  66. public override bool Equals(object obj)
  67. {
  68. if (!(obj is IServiceInfo))
  69. return false;
  70. var other = (IServiceInfo) obj;
  71. return GetHashCode() == other.GetHashCode();
  72. }
  73. public override int GetHashCode()
  74. {
  75. var hash = 0;
  76. // Hash the service name
  77. if (!string.IsNullOrEmpty(ServiceName))
  78. hash = ServiceName.GetHashCode();
  79. // Hash the service type
  80. hash ^= ServiceType.GetHashCode();
  81. // Hash the arguments
  82. foreach (var argType in ArgumentTypes)
  83. {
  84. if (argType == null)
  85. continue;
  86. hash ^= argType.GetHashCode();
  87. }
  88. // Hash the number of arguments
  89. var argCount = ArgumentTypes == null ? 0 : ArgumentTypes.Count();
  90. if (argCount > 0)
  91. hash ^= argCount.GetHashCode();
  92. return hash;
  93. }
  94. }
  95. }