PageRenderTime 62ms CodeModel.GetById 40ms app.highlight 4ms RepoModel.GetById 16ms app.codeStats 1ms

/src/LinFu.IoC/Configuration/Extensions/FluentExtensions.cs

http://github.com/philiplaureano/LinFu
C# | 102 lines | 48 code | 9 blank | 45 comment | 0 complexity | 834f5e9817c8d81aa723823bc0b02df5 MD5 | raw file
  1using System;
  2using LinFu.IoC.Configuration;
  3using LinFu.IoC.Configuration.Interfaces;
  4using LinFu.IoC.Interfaces;
  5
  6namespace LinFu.IoC
  7{
  8    /// <summary>
  9    ///     A class that adds fluent syntax support to <see cref="IServiceContainer" />
 10    ///     instances.
 11    /// </summary>
 12    public static class FluentExtensions
 13    {
 14        /// <summary>
 15        ///     Injects a <typeparamref name="TService" /> type
 16        ///     into a <paramref name="container" /> using the
 17        ///     given <paramref name="serviceName" />
 18        /// </summary>
 19        /// <typeparam name="TService">The type of service to inject.</typeparam>
 20        /// <param name="container">The container that will hold the actual service service instance.</param>
 21        /// <param name="serviceName">The name of the service to create.</param>
 22        /// <returns>A non-null <see cref="IUsingLambda{TService}" /> instance.</returns>
 23        public static IUsingLambda<TService> Inject<TService>(this IServiceContainer container, string serviceName)
 24        {
 25            var context = new InjectionContext<TService>
 26            {
 27                ServiceName = serviceName,
 28                Container = container
 29            };
 30
 31            return new UsingLambda<TService>(context);
 32        }
 33
 34        /// <summary>
 35        ///     Injects a <typeparamref name="TService" /> type
 36        ///     into a <paramref name="container" />.
 37        /// </summary>
 38        /// <typeparam name="TService">The type of service to inject.</typeparam>
 39        /// <param name="container">The container that will hold the actual service service instance.</param>
 40        /// <returns>A non-null <see cref="IUsingLambda{TService}" /> instance.</returns>
 41        public static IUsingLambda<TService> Inject<TService>(this IServiceContainer container)
 42        {
 43            var context = new InjectionContext<TService>
 44            {
 45                ServiceName = null,
 46                Container = container
 47            };
 48
 49            return new UsingLambda<TService>(context);
 50        }
 51
 52        /// <summary>
 53        ///     Initializes services that match the given <typeparamref name="TService" /> type.
 54        /// </summary>
 55        /// <typeparam name="TService">The service type to initialize.</typeparam>
 56        /// <param name="container">The container that will create the service itself.</param>
 57        /// <returns>A <see cref="IPropertyInjectionLambda{T}" /> instance. This cannot be <c>null</c>.</returns>
 58        public static IPropertyInjectionLambda<TService> Initialize<TService>(this IServiceContainer container)
 59        {
 60            return container.Initialize<TService>(null);
 61        }
 62
 63        /// <summary>
 64        ///     Initializes services that match the given <paramref name="serviceName" /> and <typeparamref name="TService" />
 65        ///     type.
 66        /// </summary>
 67        /// <typeparam name="TService">The service type to initialize.</typeparam>
 68        /// <param name="container">The container that will create the service itself.</param>
 69        /// <param name="serviceName">The name of the service to initialize.</param>
 70        /// <returns>A <see cref="IPropertyInjectionLambda{T}" /> instance. This cannot be <c>null</c>.</returns>
 71        public static IPropertyInjectionLambda<TService> Initialize<TService>(this IServiceContainer container,
 72            string serviceName)
 73        {
 74            var context = new ActionContext<TService>
 75            {
 76                ServiceName = serviceName,
 77                Container = container
 78            };
 79
 80            return new PropertyInjectionLambda<TService>(context);
 81        }
 82
 83        /// <summary>
 84        ///     Converts a <see cref="Func{Type, IServiceContainer, TArgs, TService}" />
 85        ///     lambda into an equivalent <see cref="Func{Type, IContainer, TArgs, TService}" />
 86        ///     instance.
 87        /// </summary>
 88        /// <typeparam name="TService">The type of service to create.</typeparam>
 89        /// <param name="func">The lambda function to be converted.</param>
 90        /// <returns>
 91        ///     The equivalent <see cref="Func{IFactoryRequest, TService}" />
 92        ///     that delegates its calls back to the <paramref name="func" /> lambda function.
 93        /// </returns>
 94        internal static Func<IFactoryRequest, TService> CreateAdapter<TService>(
 95            this Func<IFactoryRequest, TService> func)
 96        {
 97            var adapter = func;
 98
 99            return adapter;
100        }
101    }
102}