1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.MicroKernel
.Lifestyle
18 using System
.Collections
;
19 using System
.Threading
;
20 using System
.Runtime
.Serialization
;
23 /// Summary description for PerThreadLifestyleManager.
26 public class PerThreadLifestyleManager
: AbstractLifestyleManager
, IDeserializationCallback
29 private static LocalDataStoreSlot slot
= Thread
.AllocateNamedDataSlot("CastlePerThread");
32 private IList instances
= new ArrayList();
37 public override void Dispose()
39 foreach( object instance
in instances
)
41 base.Release( instance
);
46 Thread
.FreeNamedDataSlot( "CastlePerThread" );
49 #region IResolver Members
51 public override object Resolve(CreationContext context
)
55 Hashtable map
= (Hashtable
) Thread
.GetData( slot
);
59 map
= new Hashtable();
61 Thread
.SetData( slot
, map
);
64 Object instance
= map
[ ComponentActivator
];
66 if ( instance
== null )
68 instance
= base.Resolve(context
);
69 map
.Add( ComponentActivator
, instance
);
70 instances
.Add( instance
);
77 public override void Release(object instance
)
84 public void OnDeserialization(object sender
)
86 slot
= Thread
.AllocateNamedDataSlot("CastlePerThread");
87 instances
= new ArrayList();