Added LwesBinaryInstaller0.1.486.19070.msi
[lwes-dotnet.git] / Org.Lwes / IoCAdapter.cs
bloba1884f00f93465ce933e5ec46c1f3cabbc3a3a35
1 //
2 // This file is part of the LWES .NET Binding (LWES.net)
3 //
4 // COPYRIGHT© 2009, Phillip Clark (phillip[at*flitbit[dot*org)
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;
26 using Org.Lwes.Trace;
28 /// <summary>
29 /// Utility methods for interacting with IoC containers.
30 /// </summary>
31 public abstract class IoCAdapter
33 #region Fields
35 static IoCAdapter __current;
36 static Object __creationLock = new Object();
37 static bool __hardFailOnAccessingIoC;
39 #endregion Fields
41 #region Properties
43 internal static IoCAdapter Current
45 get
47 if (__hardFailOnAccessingIoC) return null;
48 return Util.LazyInitializeWithLock(ref __current, __creationLock, () =>
50 return CreateIoCAdapterFromConfiguration();
51 });
55 #endregion Properties
57 #region Methods
59 /// <summary>
60 /// Tries to create a named instance of type T using the currently
61 /// configured IoC container.
62 /// </summary>
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)
72 try
74 // Try to use the service locator - this is IoC implementation independent
75 IoCAdapter current = IoCAdapter.Current;
76 if (current != null)
78 return current.TryCreate(out instance);
81 catch (Exception e)
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);
89 return false;
92 /// <summary>
93 /// Tries to create a named instance of type T using the currently
94 /// configured IoC container.
95 /// </summary>
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;
110 if (current != null)
112 return current.TryCreate(out instance);
115 catch (Exception e)
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);
125 return false;
128 /// <summary>
129 /// Tries to create/access an instance of type T
130 /// </summary>
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);
137 /// <summary>
138 /// Tries to create/access a named instance of type T
139 /// </summary>
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>();
158 return true;
161 public override bool TryCreate<T>(string name, out T instance)
163 // no support for named instances
164 instance = default(T);
165 return false;
169 #endregion Methods