/src/LinFu.IoC/BaseFactoryStorage.cs

http://github.com/philiplaureano/LinFu · C# · 59 lines · 28 code · 7 blank · 24 comment · 1 complexity · d6c4e9d142e622a6b1e3b00fe1cb52e0 MD5 · raw file

  1. using System.Collections.Generic;
  2. using LinFu.IoC.Interfaces;
  3. namespace LinFu.IoC
  4. {
  5. /// <summary>
  6. /// Represents the default base implementation of the <see cref="IFactoryStorage" /> class.
  7. /// </summary>
  8. public abstract class BaseFactoryStorage : IFactoryStorage
  9. {
  10. private readonly Dictionary<IServiceInfo, IFactory> _entries = new Dictionary<IServiceInfo, IFactory>();
  11. private readonly object _lock = new object();
  12. /// <summary>
  13. /// Determines which factories should be used
  14. /// for a particular service request.
  15. /// </summary>
  16. /// <param name="serviceInfo">The <see cref="IServiceInfo" /> object that describes the target factory.</param>
  17. /// <returns>A factory instance.</returns>
  18. public virtual IFactory GetFactory(IServiceInfo serviceInfo)
  19. {
  20. if (_entries.ContainsKey(serviceInfo))
  21. return _entries[serviceInfo];
  22. return null;
  23. }
  24. /// <summary>
  25. /// Adds a <see cref="IFactory" /> to the current <see cref="IFactoryStorage" /> object.
  26. /// </summary>
  27. /// <param name="serviceInfo">The <see cref="IServiceInfo" /> object that describes the target factory.</param>
  28. /// <param name="factory">The <see cref="IFactory" /> instance that will create the object instance.</param>
  29. public virtual void AddFactory(IServiceInfo serviceInfo, IFactory factory)
  30. {
  31. lock (_lock)
  32. {
  33. _entries[serviceInfo] = factory;
  34. }
  35. }
  36. /// <summary>
  37. /// Determines whether or not a factory exists in storage.
  38. /// </summary>
  39. /// <param name="serviceInfo">The <see cref="IServiceInfo" /> object that describes the target factory.</param>
  40. /// <returns>Returns <c>true</c> if the factory exists; otherwise, it will return <c>false</c>.</returns>
  41. public virtual bool ContainsFactory(IServiceInfo serviceInfo)
  42. {
  43. return _entries.ContainsKey(serviceInfo);
  44. }
  45. /// <summary>
  46. /// Gets a value indicating the list of <see cref="IServiceInfo" /> objects
  47. /// that describe each available factory in the current <see cref="IFactoryStorage" />
  48. /// instance.
  49. /// </summary>
  50. public virtual IEnumerable<IServiceInfo> AvailableFactories => _entries.Keys;
  51. }
  52. }