User: xtoff
Date: 2009/12/18 03:25 PM
Added:
/InversionOfControl/trunk/src/Castle.MicroKernel/Lifestyle/
PerThreadThreadStaticLifestyleManager.cs
Modified:
/InversionOfControl/trunk/src/Castle.MicroKernel/
Castle.MicroKernel-vs2008.csproj
/InversionOfControl/trunk/src/Castle.MicroKernel/Handlers/
AbstractHandler.cs
/InversionOfControl/trunk/src/Castle.MicroKernel/Lifestyle/
PerThreadLifestyleManager.cs
Log:
- added PerThreadThreadStaticLifestyleManager (impl by Simon Cropp) to be used
instead of PerThreadLifestyleManager in Silverlight, where the latter can not
be used
File Changes:
Directory: /InversionOfControl/trunk/src/Castle.MicroKernel/
============================================================
File [modified]: Castle.MicroKernel-vs2008.csproj
Delta lines: +4 -0
===================================================================
--- InversionOfControl/trunk/src/Castle.MicroKernel/Handlers/AbstractHandler.cs
2009-12-18 22:09:55 UTC (rev 6459)
+++ InversionOfControl/trunk/src/Castle.MicroKernel/Handlers/AbstractHandler.cs
2009-12-18 22:25:23 UTC (rev 6460)
@@ -377,7 +377,11 @@
}
else if (type == LifestyleType.Thread)
{
+#if (SILVERLIGHT)
+ manager = new
PerThreadThreadStaticLifestyleManager();
+#else
manager = new PerThreadLifestyleManager();
+#endif
}
else if (type == LifestyleType.Transient)
Directory: /InversionOfControl/trunk/src/Castle.MicroKernel/Handlers/
=====================================================================
File [modified]: AbstractHandler.cs
Delta lines: +7 -15
===================================================================
---
InversionOfControl/trunk/src/Castle.MicroKernel/Lifestyle/PerThreadLifestyleManager.cs
2009-12-18 22:09:55 UTC (rev 6459)
+++
InversionOfControl/trunk/src/Castle.MicroKernel/Lifestyle/PerThreadLifestyleManager.cs
2009-12-18 22:25:23 UTC (rev 6460)
@@ -15,6 +15,7 @@
namespace Castle.MicroKernel.Lifestyle
{
+#if (!SILVERLIGHT)
using System;
using System.Collections.Generic;
using System.Threading;
@@ -23,21 +24,13 @@
/// <summary>
/// Summary description for PerThreadLifestyleManager.
/// </summary>
-#if (SILVERLIGHT)
- public class PerThreadLifestyleManager : AbstractLifestyleManager
-#else
[Serializable]
public class PerThreadLifestyleManager : AbstractLifestyleManager,
IDeserializationCallback
-#endif
{
-#if (!SILVERLIGHT)
[NonSerialized]
-#endif
private static LocalDataStoreSlot slot =
Thread.AllocateNamedDataSlot("CastlePerThread");
-#if (!SILVERLIGHT)
[NonSerialized]
-#endif
private IList<object> instances = new List<object>();
/// <summary>
@@ -60,22 +53,22 @@
{
lock(slot)
{
- var map = (Dictionary<object, object>)Thread.GetData(slot);
+ var map = (Dictionary<IComponentActivator,
object>)Thread.GetData(slot);
if (map == null)
{
- map = new Dictionary<object, object>();
+ map = new
Dictionary<IComponentActivator, object>();
Thread.SetData( slot, map );
}
Object instance;
- if (!map.TryGetValue(ComponentActivator, out
instance))
+ if (!map.TryGetValue(ComponentActivator, out
instance))
{
instance = base.Resolve(context);
- map.Add( ComponentActivator, instance );
- instances.Add( instance );
+ map.Add(ComponentActivator, instance);
+ instances.Add(instance);
}
return instance;
@@ -88,12 +81,11 @@
return false;
}
-#if (!SILVERLIGHT)
public void OnDeserialization(object sender)
{
slot = Thread.AllocateNamedDataSlot("CastlePerThread");
instances = new List<object>();
}
-#endif
}
+#endif
Directory: /InversionOfControl/trunk/src/Castle.MicroKernel/Lifestyle/
======================================================================
File [modified]: PerThreadLifestyleManager.cs
Delta lines: +78 -0
===================================================================
---
InversionOfControl/trunk/src/Castle.MicroKernel/Lifestyle/PerThreadThreadStaticLifestyleManager.cs
(rev 0)
+++
InversionOfControl/trunk/src/Castle.MicroKernel/Lifestyle/PerThreadThreadStaticLifestyleManager.cs
2009-12-18 22:25:23 UTC (rev 6460)
@@ -0,0 +1,79 @@
+// Copyright 2004-2009 Castle Project - http://www.castleproject.org/
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+
+namespace Castle.MicroKernel.Lifestyle
+{
+ using System;
+ using System.Collections.Generic;
+
+ /// <summary>
+ /// per thread LifestyleManager implementation compatibile with
Silverlight.
+ /// </summary>
+#if (!SILVERLIGHT)
+ [Serializable]
+#endif
+ public class PerThreadThreadStaticLifestyleManager :
AbstractLifestyleManager
+ {
+#if (!SILVERLIGHT)
+ [NonSerialized]
+#endif
+ [ThreadStatic]
+ private static Dictionary<IComponentActivator, object> map;
+
+ public static Dictionary<IComponentActivator, object> Map
+ {
+ get
+ {
+ if (map == null)
+ {
+ map = new
Dictionary<IComponentActivator, object>();
+ }
+ return map;
+ }
+ }
+
+ /// <summary>
+ ///
+ /// </summary>
+ public override void Dispose()
+ {
+ foreach (var instance in Map.Values)
+ {
+ base.Release(instance);
+ }
+ }
+
+ public override object Resolve(CreationContext context)
+ {
+ Object instance;
+
+ var dictionary = Map;
+ if (!dictionary.TryGetValue(ComponentActivator, out
instance))
+ {
+ instance = base.Resolve(context);
+ dictionary.Add(ComponentActivator, instance);
+ }
+
+ return instance;
+ }
+
+ public override bool Release(object instance)
+ {
+ // Do nothing.
+ return false;
+ }
+
+ }
File [added]: PerThreadThreadStaticLifestyleManager.cs
Delta lines: +0 -0
===================================================================
--
You received this message because you are subscribed to the Google Groups
"Castle Project Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/castle-project-commits?hl=en.