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
38 interface IObjectFactory
\r
41 /// Registers <paramref name="implementationType" /> as an
\r
42 /// implementation for all implemented interfaces.
\r
44 /// <param name="implementationType">
\r
45 /// A <see cref="T:System.Type" /> which is the implementation type
\r
49 /// Once this method has been called,
\r
50 /// <paramref name="implementationType" /> may be used in future
\r
51 /// <see cref="M:Create(Type)" /> and <see cref="M:Get(Type)" />
\r
54 void Register(Type implementationType);
\r
57 /// Unregisters <paramref name="implementationType" /> as an
\r
58 /// implementation for all implemented interfaces.
\r
60 /// <param name="implementationType">
\r
61 /// A <see cref="T:System.Type" /> which is the implementation type
\r
65 /// Once this method has been called,
\r
66 /// <paramref name="implementationType" /> will no longer be used by
\r
67 /// subsequent <see cref="M:Create(Type)" /> and
\r
68 /// <see cref="M:Get(Type)" /> invocations.
\r
70 void Unregister(Type implementationType);
\r
73 /// Returns an instance of a stateless class (may be a singleton)
\r
76 /// An instance of type <paramref name="interfaceType" />.
\r
77 /// This instance will be shared by other invocations of
\r
78 /// <c>Get()</c> with the same <param name="interfaceType" /> value.
\r
80 object Get(Type interfaceType);
\r
83 /// Returns a new instance of the specified class (can not be a singleton)
\r
86 /// A new instance of type <paramref name="interfaceType" />.
\r
87 /// This instance will not be shared by other invocations of
\r
88 /// <c>Create()</c> with the same <param name="interfaceType" />
\r
91 object Create(Type interfaceType);
\r
94 /// Returns a list of types implementing the required interface
\r
96 /// <param name="interfaceType"></param>
\r
98 /// An <see cref="T:IEnumerable{Type}" /> containing all registered
\r
99 /// implementations for the interface
\r
100 /// <paramref name="interfaceType" />.
\r
102 IEnumerable<Type> GetImplementations(Type interfaceType);
\r
106 /// Extension methods for <see cref="T:IObjectFactory" />.
\r
111 static class ObjectFactoryExtensions
\r
114 /// Creates a new instance of <typeparamref name="T" /> from
\r
115 /// <paramref name="self" />.
\r
117 /// <typeparam name="T">The type to create.</typeparam>
\r
118 /// <param name="self">
\r
119 /// An <see cref="T:IObjectFactory" /> to use to create a new instance
\r
120 /// of type <typeparamref name="T" />.
\r
123 /// A newly created instance of type <typeparamref name="T" />.
\r
125 /// <seealso cref="M:IObjectFactory.Create(Type)"/>
\r
126 public static T Create<T>(this IObjectFactory self)
\r
128 return (T) self.Create(typeof(T));
\r
132 /// Gets a (possibly pre-existing) instance of
\r
133 /// <typeparamref name="T" /> from <paramref name="self" />.
\r
135 /// <typeparam name="T">The type to get.</typeparam>
\r
136 /// <param name="self">
\r
137 /// An <see cref="T:IObjectFactory" /> to use to get an instance
\r
138 /// of type <typeparamref name="T" />.
\r
141 /// A (possibly pre-existing) instance of type
\r
142 /// <typeparamref name="T" />.
\r
144 /// <seealso cref="M:IObjectFactory.Get(Type)"/>
\r
145 public static T Get<T>(this IObjectFactory self)
\r
147 return (T) self.Get(typeof(T));
\r