Added non-generic registration interface to IKernel and IWindsor to accommodate dynam...
[castle.git] / MonoRail / Castle.MonoRail.Framework / ServiceProviderLocator.cs
blob9a160eed7b37080591ccccd6e2d1e94a7880c119
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.MonoRail.Framework
17 using System.Collections.Generic;
18 using System.Web;
19 using Castle.Core;
21 /// <summary>
22 /// Uses the HttpContext and the <see cref="IServiceProviderExAccessor"/>
23 /// to access the container instance.
24 /// </summary>
25 public class ServiceProviderLocator : IServiceProviderLocator
27 private static readonly ServiceProviderLocator instance = new ServiceProviderLocator();
29 private readonly IList<IAccessorStrategy> locatorStrategies = new List<IAccessorStrategy>();
31 /// <summary>
32 /// Initializes a new instance of the <see cref="ServiceProviderLocator"/> class.
33 /// </summary>
34 public ServiceProviderLocator()
36 AddLocatorStrategy(new ServiceProviderAccessorStrategy());
39 /// <summary>
40 /// Gets the instance.
41 /// </summary>
42 /// <value>The instance.</value>
43 public static ServiceProviderLocator Instance
45 get { return instance; }
48 /// <summary>
49 /// Locates the service provider using the registered strategies.
50 /// </summary>
51 /// <returns></returns>
52 public IServiceProviderEx LocateProvider()
54 foreach(IAccessorStrategy strategy in locatorStrategies)
56 IServiceProviderEx serviceProvider = strategy.LocateProvider();
58 if (serviceProvider != null)
60 return serviceProvider;
64 return null;
67 /// <summary>
68 /// Adds a locator strategy.
69 /// </summary>
70 /// <param name="strategy">The strategy.</param>
71 public void AddLocatorStrategy(IAccessorStrategy strategy)
73 locatorStrategies.Add(strategy);
76 /// <summary>
77 /// Abstract an approach to access a <see cref="IServiceProviderEx"/>
78 /// </summary>
79 public interface IAccessorStrategy
81 /// <summary>
82 /// Locates the provider.
83 /// </summary>
84 /// <returns></returns>
85 IServiceProviderEx LocateProvider();
88 /// <summary>
89 /// Default strategy to access a service provider
90 /// </summary>
91 public class ServiceProviderAccessorStrategy : IAccessorStrategy
93 /// <summary>
94 /// Locates the provider using the ApplicationInstance and casting it to
95 /// <see cref="IServiceProviderExAccessor"/>
96 /// </summary>
97 /// <returns></returns>
98 public IServiceProviderEx LocateProvider()
100 IServiceProviderExAccessor containerAccessor =
101 HttpContext.Current.ApplicationInstance as IServiceProviderExAccessor;
103 if (containerAccessor == null)
105 return null;
108 return containerAccessor.ServiceProvider;