PageRenderTime 101ms CodeModel.GetById 40ms app.highlight 4ms RepoModel.GetById 36ms app.codeStats 0ms

/src/LinFu.IoC/FactoryStorageExtensions.cs

http://github.com/philiplaureano/LinFu
C# | 83 lines | 37 code | 5 blank | 41 comment | 1 complexity | 12565e1ca9784c6f2217fcc293413735 MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using LinFu.IoC.Interfaces;
 5
 6namespace LinFu.IoC
 7{
 8    /// <summary>
 9    ///     An extension class that adds a few helper methods to the
10    ///     <see cref="IFactoryStorage" /> interface.
11    /// </summary>
12    public static class FactoryStorageExtensions
13    {
14        /// <summary>
15        ///     Adds a factory to the current <see cref="IFactoryStorage" /> instance.
16        /// </summary>
17        /// <param name="storage">The <see cref="IFactoryStorage" /> object that will store the target factory.</param>
18        /// <param name="serviceName">The name that will be associated with the target factory.</param>
19        /// <param name="serviceType">The service type that the factory will be able to create.</param>
20        /// <param name="additionalParameterTypes">The list of additional parameters that this factory type will support.</param>
21        /// <param name="factory">The <see cref="IFactory" /> instance that will create the object instance.</param>
22        public static void AddFactory(this IFactoryStorage storage, string serviceName,
23            Type serviceType, IEnumerable<Type> additionalParameterTypes, IFactory factory)
24        {
25            var info = new ServiceInfo(serviceName, serviceType, additionalParameterTypes);
26            storage.AddFactory(info, factory);
27        }
28
29        /// <summary>
30        ///     Determines which factories should be used
31        ///     for a particular service request.
32        /// </summary>
33        /// <param name="storage">The <see cref="IFactoryStorage" /> object that holds the target factory.</param>
34        /// <param name="serviceName">The name that will be associated with the target factory.</param>
35        /// <param name="serviceType">The service type that the factory will be able to create.</param>
36        /// <param name="additionalParameters">
37        ///     The list of additional parameter values that this factory type will use to
38        ///     instantiate the service.
39        /// </param>
40        /// <returns>A factory instance.</returns>
41        public static IFactory GetFactory(this IFactoryStorage storage, string serviceName, Type serviceType,
42            IEnumerable<object> additionalParameters)
43        {
44            var additionalParameterTypes = from arg in additionalParameters
45                let argType = arg != null ? arg.GetType() : typeof(object)
46                select argType;
47
48            var info = new ServiceInfo(serviceName, serviceType, additionalParameterTypes);
49            return storage.GetFactory(info);
50        }
51
52        /// <summary>
53        ///     Determines which factories should be used
54        ///     for a particular service request.
55        /// </summary>
56        /// <param name="storage">The <see cref="IFactoryStorage" /> object that holds the target factory.</param>
57        /// <param name="serviceName">The name that will be associated with the target factory.</param>
58        /// <param name="serviceType">The service type that the factory will be able to create.</param>
59        /// <param name="additionalParameterTypes">The list of additional parameters that this factory type will support.</param>
60        /// <returns>A factory instance.</returns>
61        public static IFactory GetFactory(this IFactoryStorage storage, string serviceName, Type serviceType,
62            IEnumerable<Type> additionalParameterTypes)
63        {
64            var info = new ServiceInfo(serviceName, serviceType, additionalParameterTypes);
65            return storage.GetFactory(info);
66        }
67
68        /// <summary>
69        ///     Determines whether or not a factory exists in storage.
70        /// </summary>
71        /// <param name="storage">The <see cref="IFactoryStorage" /> object that holds the target factory.</param>
72        /// <param name="serviceName">The name that will be associated with the target factory.</param>
73        /// <param name="serviceType">The service type that the factory will be able to create.</param>
74        /// <param name="additionalParameterTypes">The list of additional parameters that this factory type will support.</param>
75        /// <returns>Returns <c>true</c> if the factory exists; otherwise, it will return <c>false</c>.</returns>
76        public static bool ContainsFactory(this IFactoryStorage storage, string serviceName, Type serviceType,
77            IEnumerable<Type> additionalParameterTypes)
78        {
79            var info = new ServiceInfo(serviceName, serviceType, additionalParameterTypes);
80            return storage.ContainsFactory(info);
81        }
82    }
83}