Added container accessor to Castle.Core
[castle.git] / InversionOfControl / Castle.MicroKernel / Handlers / ParentHandlerWithChildResolver.cs
blob1baa3c0b89da204d0437e87c8f85661b089bbc33
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;
21 /// <summary>
22 /// Redirects resolution to the main resolver, and if not found uses
23 /// the parent handler.
24 /// </summary>
25 public class ParentHandlerWithChildResolver : IHandler, IDisposable
27 private readonly IHandler parentHandler;
28 private readonly ISubDependencyResolver childResolver;
30 /// <summary>
31 /// Initializes a new instance of the <see cref="ParentHandlerWithChildResolver"/> class.
32 /// </summary>
33 /// <param name="parentHandler">The parent handler.</param>
34 /// <param name="childResolver">The child resolver.</param>
35 public ParentHandlerWithChildResolver(IHandler parentHandler, ISubDependencyResolver childResolver)
37 if (parentHandler == null) throw new ArgumentNullException("parentHandler");
38 if (childResolver == null) throw new ArgumentNullException("childResolver");
40 this.parentHandler = parentHandler;
41 parentHandler.OnHandlerStateChanged += new HandlerStateDelegate(RaiseHandlerStateChanged);
42 this.childResolver = childResolver;
45 #region ISubDependencyResolver Members
47 public virtual object Resolve(CreationContext context, ISubDependencyResolver parentResolver, ComponentModel model, DependencyModel dependency)
49 object value = childResolver.Resolve(context, null, model, dependency);
51 if (value == null)
53 value = parentHandler.Resolve(context, parentResolver, model, dependency);
56 return value;
59 public virtual bool CanResolve(CreationContext context, ISubDependencyResolver parentResolver, ComponentModel model, DependencyModel dependency)
61 bool canResolve = childResolver.CanResolve(context, null, model, dependency);
63 if (!canResolve)
65 canResolve = parentHandler.CanResolve(context, parentResolver, model, dependency);
68 return canResolve;
71 #endregion
73 #region IHandler Members
75 public virtual void Init(IKernel kernel)
77 throw new NotImplementedException();
80 public virtual object Resolve(CreationContext context)
82 return parentHandler.Resolve(context);
85 public virtual void Release(object instance)
87 parentHandler.Release(instance);
90 public virtual HandlerState CurrentState
92 get { return parentHandler.CurrentState; }
95 public virtual ComponentModel ComponentModel
97 get { return parentHandler.ComponentModel; }
100 public event HandlerStateDelegate OnHandlerStateChanged;
102 protected virtual void RaiseHandlerStateChanged(object s, EventArgs e)
104 if (OnHandlerStateChanged != null)
106 OnHandlerStateChanged(s, e);
110 public virtual void AddCustomDependencyValue(string key, object value)
112 parentHandler.AddCustomDependencyValue(key, value);
115 public virtual void RemoveCustomDependencyValue(string key)
117 parentHandler.RemoveCustomDependencyValue(key);
120 public virtual bool HasCustomParameter(string key)
122 return parentHandler.HasCustomParameter(key);
125 #endregion
127 #region IDisposable Members
129 public void Dispose()
131 Dispose(true);
134 protected virtual void Dispose(bool disposing)
136 if (disposing)
138 if (parentHandler != null)
140 parentHandler.OnHandlerStateChanged -= new HandlerStateDelegate(RaiseHandlerStateChanged);
145 #endregion