IServiceContainer.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. namespace Wayne.Lib
  3. {
  4. /// <summary>
  5. /// Interface representing a service container. Dispose will dispose all disposable services.
  6. /// </summary>
  7. public interface IServiceContainer : IServiceLocator, IDisposable
  8. {
  9. /// <summary>
  10. /// Register an external resolver, if you want to hook up the container against another Ioc framework for example.
  11. /// </summary>
  12. /// <param name="requestedType"></param>
  13. void RegisterResolver(Func<Type, object> requestedType);
  14. /// <summary>
  15. /// Registers service of type TService in the container. A new instance of this type will be created by the container.
  16. /// </summary>
  17. /// <typeparam name="TService">Type of service.</typeparam>
  18. void RegisterService<TService>();
  19. /// <summary>
  20. /// Registers service of type TService in the container. A new instance of this type will be created by the container.
  21. /// </summary>
  22. /// <typeparam name="TService">Type of service.</typeparam>
  23. /// <param name="serviceId">A string to identfy this entry.</param>
  24. void RegisterService<TService>(string serviceId);
  25. /// <summary>
  26. /// Registers service of type TServiceContract in the container. An instance of TServiceImplementation will be created by the container and registerd as TServiceContract.
  27. /// </summary>
  28. /// <typeparam name="TServiceContract">Contract that should be registered in the container, e.g. IFoo.</typeparam>
  29. /// <typeparam name="TServiceImplementation">Implementation of the contract, e.g. Foo implementing IFoo.</typeparam>
  30. void RegisterService<TServiceContract, TServiceImplementation>()
  31. where TServiceImplementation : TServiceContract
  32. where TServiceContract : class;
  33. /// <summary>
  34. /// Registers service of type TServiceContract in the container. An instance of TServiceImplementation will be created by the container and registerd as TServiceContract.
  35. /// </summary>
  36. /// <typeparam name="TServiceContract">Contract that should be registered in the container, e.g. IFoo.</typeparam>
  37. /// <typeparam name="TServiceImplementation">Implementation of the contract, e.g. Foo implementing IFoo.</typeparam>
  38. /// <param name="serviceId">A string to identfy this entry.</param>
  39. void RegisterService<TServiceContract, TServiceImplementation>(string serviceId)
  40. where TServiceImplementation : TServiceContract
  41. where TServiceContract : class;
  42. /// <summary>
  43. /// Registers service of type TServiceContract in the container. The instance that must implement this service contract is passed as an argument.
  44. /// </summary>
  45. /// <typeparam name="TServiceContract">Contract that should be registerd in the container, e.g. IFoo.</typeparam>
  46. /// <param name="serviceInstance">Instance of service implementing TServiceContract, e.g. an instance of Foo where Foo implements IFoo.</param>
  47. void RegisterService<TServiceContract>(TServiceContract serviceInstance);
  48. /// <summary>
  49. /// Registers service of type TServiceContract in the container. The instance that must implement this service contract is passed as an argument.
  50. /// </summary>
  51. /// <typeparam name="TServiceContract">Contract that should be registerd in the container, e.g. IFoo.</typeparam>
  52. /// <param name="serviceInstance">Instance of service implementing TServiceContract, e.g. an instance of Foo where Foo implements IFoo.</param>
  53. /// <param name="serviceId"> A string to identfy this entry.</param>
  54. void RegisterService<TServiceContract>(TServiceContract serviceInstance, string serviceId);
  55. /// <summary>
  56. /// Registers a service using a given a delegate that creates the service. This enables the user of this container
  57. /// to control the creation of the service and at the same time use dependencies from the container.
  58. /// Example using lambdas : serviceContainer.RegisterService(container => new Foo("myConstant", container.GetService&lt;IDependency&gt;()))
  59. /// </summary>
  60. /// <typeparam name="TServiceContract">Contract that should be registerd in the container, e.g. IFoo.</typeparam>
  61. /// <param name="serviceConstructor">Delegate that constructs the service.</param>
  62. void RegisterService<TServiceContract>(ObjectConstructor<IServiceLocator, TServiceContract> serviceConstructor);
  63. /// <summary>
  64. /// Registers a service using a given a delegate that creates the service. This enables the user of this container
  65. /// to control the creation of the service and at the same time use dependencies from the container.
  66. /// Example using lambdas : serviceContainer.RegisterService(container => new Foo("myConstant", container.GetService&lt;IDependency&gt;()))
  67. /// </summary>
  68. /// <typeparam name="TServiceContract">Contract that should be registerd in the container, e.g. IFoo.</typeparam>
  69. /// <param name="serviceConstructor">Delegate that constructs the service.</param>
  70. /// <param name="serviceId"> A string to identfy this entry.</param>
  71. void RegisterService<TServiceContract>(ObjectConstructor<IServiceLocator, TServiceContract> serviceConstructor, string serviceId);
  72. }
  73. /// <summary>
  74. /// Delegate used by the service container to enable outside control of the construction of an object in the container.
  75. /// </summary>
  76. /// <typeparam name="TServiceLocator"></typeparam>
  77. /// <typeparam name="TReturnType"></typeparam>
  78. /// <param name="serviceLocator"></param>
  79. /// <returns></returns>
  80. public delegate TReturnType ObjectConstructor<TServiceLocator, TReturnType>(TServiceLocator serviceLocator);
  81. }