5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
\r
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
\r
8 // of this software and associated documentation files (the "Software"), to deal
\r
9 // in the Software without restriction, including without limitation the rights
\r
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
11 // copies of the Software, and to permit persons to whom the Software is
\r
12 // furnished to do so, subject to the following conditions:
\r
14 // The above copyright notice and this permission notice shall be included in
\r
15 // all copies or substantial portions of the Software.
\r
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
27 using System.Collections.Generic;
\r
29 namespace DbLinq.Factory
\r
32 /// The object factory is the start point for DbLinq main factory.
\r
33 /// See ObjectFactory.Current for details
\r
40 interface IObjectFactory
\r
43 /// Registers <paramref name="implementationType" /> as an
\r
44 /// implementation for all implemented interfaces.
\r
46 /// <param name="implementationType">
\r
47 /// A <see cref="T:System.Type" /> which is the implementation type
\r
51 /// Once this method has been called,
\r
52 /// <paramref name="implementationType" /> may be used in future
\r
53 /// <see cref="M:Create(Type)" /> and <see cref="M:Get(Type)" />
\r
56 void Register(Type implementationType);
\r
59 /// Unregisters <paramref name="implementationType" /> as an
\r
60 /// implementation for all implemented interfaces.
\r
62 /// <param name="implementationType">
\r
63 /// A <see cref="T:System.Type" /> which is the implementation type
\r
67 /// Once this method has been called,
\r
68 /// <paramref name="implementationType" /> will no longer be used by
\r
69 /// subsequent <see cref="M:Create(Type)" /> and
\r
70 /// <see cref="M:Get(Type)" /> invocations.
\r
72 void Unregister(Type implementationType);
\r
75 /// Returns an instance of a stateless class (may be a singleton)
\r
78 /// An instance of type <paramref name="interfaceType" />.
\r
79 /// This instance will be shared by other invocations of
\r
80 /// <c>Get()</c> with the same <param name="interfaceType" /> value.
\r
82 object Get(Type interfaceType);
\r
85 /// Returns a new instance of the specified class (can not be a singleton)
\r
88 /// A new instance of type <paramref name="interfaceType" />.
\r
89 /// This instance will not be shared by other invocations of
\r
90 /// <c>Create()</c> with the same <param name="interfaceType" />
\r
93 object Create(Type interfaceType);
\r
96 /// Returns a list of types implementing the required interface
\r
98 /// <param name="interfaceType"></param>
\r
100 /// An <see cref="T:IEnumerable{Type}" /> containing all registered
\r
101 /// implementations for the interface
\r
102 /// <paramref name="interfaceType" />.
\r
104 IEnumerable<Type> GetImplementations(Type interfaceType);
\r
108 /// Extension methods for <see cref="T:IObjectFactory" />.
\r
113 static class ObjectFactoryExtensions
\r
116 /// Creates a new instance of <typeparamref name="T" /> from
\r
117 /// <paramref name="self" />.
\r
119 /// <typeparam name="T">The type to create.</typeparam>
\r
120 /// <param name="self">
\r
121 /// An <see cref="T:IObjectFactory" /> to use to create a new instance
\r
122 /// of type <typeparamref name="T" />.
\r
125 /// A newly created instance of type <typeparamref name="T" />.
\r
127 /// <seealso cref="M:IObjectFactory.Create(Type)"/>
\r
128 public static T Create<T>(this IObjectFactory self)
\r
130 return (T) self.Create(typeof(T));
\r
134 /// Gets a (possibly pre-existing) instance of
\r
135 /// <typeparamref name="T" /> from <paramref name="self" />.
\r
137 /// <typeparam name="T">The type to get.</typeparam>
\r
138 /// <param name="self">
\r
139 /// An <see cref="T:IObjectFactory" /> to use to get an instance
\r
140 /// of type <typeparamref name="T" />.
\r
143 /// A (possibly pre-existing) instance of type
\r
144 /// <typeparamref name="T" />.
\r
146 /// <seealso cref="M:IObjectFactory.Get(Type)"/>
\r
147 public static T Get<T>(this IObjectFactory self)
\r
149 return (T) self.Get(typeof(T));
\r