1 // Copyright 2004-2007 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
.Handlers
22 /// Redirects resolution to the main resolver, and if not found uses
23 /// the parent handler.
25 public class ParentHandlerWithChildResolver
: IHandler
, IDisposable
27 private readonly IHandler parentHandler
;
28 private readonly ISubDependencyResolver childResolver
;
31 /// Initializes a new instance of the <see cref="ParentHandlerWithChildResolver"/> class.
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
);
53 value = parentHandler
.Resolve(context
, parentResolver
, model
, dependency
);
59 public virtual bool CanResolve(CreationContext context
, ISubDependencyResolver parentResolver
, ComponentModel model
, DependencyModel dependency
)
61 bool canResolve
= childResolver
.CanResolve(context
, null, model
, dependency
);
65 canResolve
= parentHandler
.CanResolve(context
, parentResolver
, model
, dependency
);
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
);
127 #region IDisposable Members
129 public void Dispose()
134 protected virtual void Dispose(bool disposing
)
138 if (parentHandler
!= null)
140 parentHandler
.OnHandlerStateChanged
-= new HandlerStateDelegate(RaiseHandlerStateChanged
);