2 // This file is part of the LWES .NET Binding (LWES.net)
4 // COPYRIGHT© 2009, Phillip Clark (cerebralkungfu[at*g mail[dot*com)
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
;
28 /// Utility methods for interacting with IoC containers.
30 public abstract class IoCAdapter
34 static IoCAdapter __current
;
35 static Object __creationLock
= new Object();
36 static bool __hardFailOnAccessingIoC
;
42 internal static IoCAdapter Current
46 if (__hardFailOnAccessingIoC
) return null;
47 return Util
.LazyInitializeWithLock(ref __current
, __creationLock
, () =>
49 return CreateIoCAdapterFromConfiguration();
59 /// Tries to create a named instance of type T using the currently
60 /// configured IoC container.
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
)
73 // Try to use the service locator - this is IoC implementation independent
74 IoCAdapter current
= IoCAdapter
.Current
;
77 return current
.TryCreate(out instance
);
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
);
92 /// Tries to create a named instance of type T using the currently
93 /// configured IoC container.
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
;
111 return current
.TryCreate(out instance
);
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
);
126 /// Tries to create/access an instance of type T
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
);
135 /// Tries to create/access a named instance of type T
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();