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
.MonoRail
.Framework
.Providers
18 using System
.Collections
.Generic
;
19 using Castle
.MonoRail
.Framework
;
24 /// Creates <see cref="ViewComponentDescriptor"/> from attributes
25 /// associated with the <see cref="ViewComponent"/>
27 public class DefaultViewComponentDescriptorProvider
: IViewComponentDescriptorProvider
29 private readonly IDictionary
<Type
, ViewComponentDescriptor
> type2Desc
=
30 new Dictionary
<Type
, ViewComponentDescriptor
>();
33 /// Services the specified provider.
35 /// <param name="provider">The provider.</param>
36 public void Service(IMonoRailServices provider
)
41 /// Creates a <see cref="ViewComponentDescriptor"/> by inspecting the
42 /// specified view component type.
44 /// <param name="viewComponentType">Type of the view component.</param>
45 /// <returns></returns>
46 public ViewComponentDescriptor
Collect(Type viewComponentType
)
48 if (viewComponentType
== null) throw new ArgumentNullException("viewComponentType");
50 if (type2Desc
.ContainsKey(viewComponentType
))
52 return type2Desc
[viewComponentType
];
55 object[] attrs
= viewComponentType
.GetCustomAttributes(typeof(ViewComponentDetailsAttribute
), true);
57 ViewComponentDescriptor descriptor
;
59 if (attrs
.Length
== 0)
61 descriptor
= ViewComponentDescriptor
.Empty
;
65 ViewComponentDetailsAttribute details
= (ViewComponentDetailsAttribute
) attrs
[0];
67 IViewComponentCacheKeyGenerator generator
= null;
69 if (details
.Cache
== ViewComponentCache
.Always
)
71 generator
= new AlwaysCacheKeyGenerator();
73 else if (details
.CacheKeyFactory
!= null)
77 generator
= (IViewComponentCacheKeyGenerator
)
78 Activator
.CreateInstance(details
.CacheKeyFactory
);
82 throw new MonoRailException(
83 "Could not instantiate IViewComponentCacheKeyGenerator implementation or " +
84 "it does not implement this interface. Type: " + details
.CacheKeyFactory
.FullName
, ex
);
88 descriptor
= new ViewComponentDescriptor(details
.Cache
!= ViewComponentCache
.Disabled
, details
.Cache
, generator
);
91 type2Desc
[viewComponentType
] = descriptor
;