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