Added container accessor to Castle.Core
[castle.git] / InversionOfControl / Castle.MicroKernel / Handlers / DefaultHandler.cs
blob1fb5b511f56cde1c212418e534dbbfdde1fd4032
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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.Handlers
17 using System;
19 using Castle.Core;
20 using System.Collections;
22 /// <summary>
23 /// Summary description for DefaultHandler.
24 /// </summary>
25 [Serializable]
26 public class DefaultHandler : AbstractHandler
28 /// <summary>
29 /// Initializes a new instance of the <see cref="DefaultHandler"/> class.
30 /// </summary>
31 /// <param name="model"></param>
32 public DefaultHandler(ComponentModel model) : base(model)
36 /// <summary>
37 /// Returns an instance of the component this handler
38 /// is responsible for
39 /// </summary>
40 /// <param name="context"></param>
41 /// <returns></returns>
42 public override object Resolve(CreationContext context)
44 if (!context.HasAdditionalParameters)
46 AssertNotWaitingForDependency();
49 using (context.ResolvingHandler(this))
51 return lifestyleManager.Resolve(context);
55 /// <summary>
56 /// disposes the component instance (or recycle it)
57 /// </summary>
58 /// <param name="instance"></param>
59 public override void Release(object instance)
61 lifestyleManager.Release(instance);
64 protected void AssertNotWaitingForDependency()
66 if (CurrentState == HandlerState.WaitingDependency)
68 String message = String.Format("Can't create component '{1}' " +
69 "as it has dependencies to be satisfied. {0}",
70 ObtainDependencyDetails(new ArrayList()), ComponentModel.Name);
72 throw new HandlerException(message);