2 // This file is part of the LWES .NET Binding (LWES.net)
4 // COPYRIGHT© 2009, Phillip Clark (phillip[at*flitbit[dot*org)
5 // original .NET implementation
7 // LWES.net is free software: you can redistribute it and/or modify
8 // it under the terms of the Lesser GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
12 // LWES.net is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // Lesser GNU General Public License for more details.
17 // You should have received a copy of the Lesser GNU General Public License
18 // along with LWES.net. If not, see <http://www.gnu.org/licenses/>.
23 using System
.Diagnostics
;
25 using Org
.Lwes
.Properties
;
29 /// Utility methods for interacting with IoC containers.
31 public abstract class IoCAdapter
35 static IoCAdapter __current
;
36 static Object __creationLock
= new Object();
37 static bool __hardFailOnAccessingIoC
;
43 internal static IoCAdapter Current
47 if (__hardFailOnAccessingIoC
) return null;
48 return Util
.LazyInitializeWithLock(ref __current
, __creationLock
, () =>
50 return CreateIoCAdapterFromConfiguration();
60 /// Tries to create a named instance of type T using the currently
61 /// configured IoC container.
63 /// <typeparam name="T">instance type T</typeparam>
64 /// <param name="instance">reference to a variable where the resulting
65 /// instance will be stored</param>
66 /// <returns><em>true</em> if a valid instance of type <typeparamref name="T"/> was stored
67 /// in <paramref name="instance"/>; otherwise <em>false</em></returns>
68 public static bool TryCreateFromIoC
<T
>(out T instance
)
70 if (!__hardFailOnAccessingIoC
)
74 // Try to use the service locator - this is IoC implementation independent
75 IoCAdapter current
= IoCAdapter
.Current
;
78 return current
.TryCreate(out instance
);
83 Traceable
.TraceData(typeof(IoCAdapter
), TraceEventType
.Error
, Resources
.Error_IocAdapterFailure
, e
);
84 /* IoC failure - remember this and stop trying */
85 __hardFailOnAccessingIoC
= true;
88 instance
= default(T
);
93 /// Tries to create a named instance of type T using the currently
94 /// configured IoC container.
96 /// <typeparam name="T">instance type T</typeparam>
97 /// <param name="name">name of the instance</param>
98 /// <param name="instance">reference to a variable where the resulting
99 /// instance will be stored</param>
100 /// <returns><em>true</em> if a valid instance of type T was stored
101 /// in <paramref name="instance"/>; otherwise <em>false</em></returns>
102 public static bool TryCreateFromIoC
<T
>(string name
, out T instance
)
104 if (!__hardFailOnAccessingIoC
)
108 // Try to use the service locator - this is IoC implementation independent
109 IoCAdapter current
= IoCAdapter
.Current
;
112 return current
.TryCreate(out instance
);
117 // Set the failure first so we don't recurse inadvertently
118 __hardFailOnAccessingIoC
= true;
120 Traceable
.TraceData(typeof(IoCAdapter
), TraceEventType
.Error
, Resources
.Error_IocAdapterFailure
, e
);
121 /* IoC failure - remember this and stop trying */
124 instance
= default(T
);
129 /// Tries to create/access an instance of type T
131 /// <typeparam name="T">target type T</typeparam>
132 /// <param name="instance">reference to a variable that will hold the instance</param>
133 /// <returns><em>true</em> if the variable is set to a valid instance of type T
134 /// during the call; otherwise <em>false</em></returns>
135 public abstract bool TryCreate
<T
>(out T instance
);
138 /// Tries to create/access a named instance of type T
140 /// <typeparam name="T">target type T</typeparam>
141 /// <param name="name">name of an instance to create</param>
142 /// <param name="instance">reference to a variable that will hold the instance</param>
143 /// <returns><em>true</em> if the variable is set to a valid instance of type T
144 /// during the call; otherwise <em>false</em></returns>
145 public abstract bool TryCreate
<T
>(string name
, out T instance
);
147 private static IoCAdapter
CreateIoCAdapterFromConfiguration()
149 // TODO: Add config sections for various IoC adapters
150 return new DefaultIoCAdapter();
153 class DefaultIoCAdapter
: IoCAdapter
155 public override bool TryCreate
<T
>(out T instance
)
157 instance
= Activator
.CreateInstance
<T
>();
161 public override bool TryCreate
<T
>(string name
, out T instance
)
163 // no support for named instances
164 instance
= default(T
);