Added initial documentation tree using doxygen. More tweaks on the license text ensur...
[lwes-dotnet/github-mirror.git] / Org.Lwes / IoCAdapter.cs
blobf30da3adfc1570ea60f337f956a2c8f587a4c520
1 //
2 // This file is part of the LWES .NET Binding (LWES.net)
3 //
4 // COPYRIGHT© 2009, Phillip Clark (cerebralkungfu[at*g mail[dot*com)
5 // original .NET implementation
6 //
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/>.
20 namespace Org.Lwes
22 using System;
23 using System.Diagnostics;
25 using Org.Lwes.Properties;
27 /// <summary>
28 /// Utility methods for interacting with IoC containers.
29 /// </summary>
30 public abstract class IoCAdapter
32 #region Fields
34 static IoCAdapter __current;
35 static Object __creationLock = new Object();
36 static bool __hardFailOnAccessingIoC;
38 #endregion Fields
40 #region Properties
42 internal static IoCAdapter Current
44 get
46 if (__hardFailOnAccessingIoC) return null;
47 return Util.LazyInitializeWithLock(ref __current, __creationLock, () =>
49 return CreateIoCAdapterFromConfiguration();
50 });
54 #endregion Properties
56 #region Methods
58 /// <summary>
59 /// Tries to create a named instance of type T using the currently
60 /// configured IoC container.
61 /// </summary>
62 /// <typeparam name="T">instance type T</typeparam>
63 /// <param name="instance">reference to a variable where the resulting
64 /// instance will be stored</param>
65 /// <returns><em>true</em> if a valid instance of type <typeparamref name="T"/> was stored
66 /// in <paramref name="instance"/>; otherwise <em>false</em></returns>
67 public static bool TryCreateFromIoC<T>(out T instance)
69 if (!__hardFailOnAccessingIoC)
71 try
73 // Try to use the service locator - this is IoC implementation independent
74 IoCAdapter current = IoCAdapter.Current;
75 if (current != null)
77 return current.TryCreate(out instance);
80 catch (Exception e)
82 Diagnostics.TraceData(typeof(IoCAdapter), TraceEventType.Error, Resources.Error_IocAdapterFailure, e);
83 /* IoC failure - remember this and stop trying */
84 __hardFailOnAccessingIoC = true;
87 instance = default(T);
88 return false;
91 /// <summary>
92 /// Tries to create a named instance of type T using the currently
93 /// configured IoC container.
94 /// </summary>
95 /// <typeparam name="T">instance type T</typeparam>
96 /// <param name="name">name of the instance</param>
97 /// <param name="instance">reference to a variable where the resulting
98 /// instance will be stored</param>
99 /// <returns><em>true</em> if a valid instance of type T was stored
100 /// in <paramref name="instance"/>; otherwise <em>false</em></returns>
101 public static bool TryCreateFromIoC<T>(string name, out T instance)
103 if (!__hardFailOnAccessingIoC)
107 // Try to use the service locator - this is IoC implementation independent
108 IoCAdapter current = IoCAdapter.Current;
109 if (current != null)
111 return current.TryCreate(out instance);
114 catch (Exception e)
116 Diagnostics.TraceData(typeof(IoCAdapter), TraceEventType.Error, Resources.Error_IocAdapterFailure, e);
117 /* IoC failure - remember this and stop trying */
118 __hardFailOnAccessingIoC = true;
121 instance = default(T);
122 return false;
125 /// <summary>
126 /// Tries to create/access an instance of type T
127 /// </summary>
128 /// <typeparam name="T">target type T</typeparam>
129 /// <param name="instance">reference to a variable that will hold the instance</param>
130 /// <returns><em>true</em> if the variable is set to a valid instance of type T
131 /// during the call; otherwise <em>false</em></returns>
132 public abstract bool TryCreate<T>(out T instance);
134 /// <summary>
135 /// Tries to create/access a named instance of type T
136 /// </summary>
137 /// <typeparam name="T">target type T</typeparam>
138 /// <param name="name">name of an instance to create</param>
139 /// <param name="instance">reference to a variable that will hold the instance</param>
140 /// <returns><em>true</em> if the variable is set to a valid instance of type T
141 /// during the call; otherwise <em>false</em></returns>
142 public abstract bool TryCreate<T>(string name, out T instance);
144 private static IoCAdapter CreateIoCAdapterFromConfiguration()
146 throw new NotImplementedException();
149 #endregion Methods