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
.Facilities
.FactorySupport
19 using Castle
.Core
.Configuration
;
20 using Castle
.MicroKernel
;
21 using Castle
.MicroKernel
.Facilities
;
23 public class FactorySupportFacility
: AbstractFacility
25 protected override void Init()
27 Kernel
.ComponentModelCreated
+= new ComponentModelDelegate(Kernel_ComponentModelCreated
);
30 private void Kernel_ComponentModelCreated(ComponentModel model
)
32 if (model
.Configuration
== null)
37 String instanceAccessor
= model
.Configuration
.Attributes
["instance-accessor"];
38 String factoryId
= model
.Configuration
.Attributes
["factoryId"];
39 String factoryCreate
= model
.Configuration
.Attributes
["factoryCreate"];
41 if ((factoryId
!= null && factoryCreate
== null) ||
42 (factoryId
== null && factoryCreate
!= null))
44 String message
= String
.Format("When a factoryId is specified, you must specify " +
45 "the factoryCreate (which is the method to be called) as well - component {0}",
48 throw new FacilityException(message
);
51 if (instanceAccessor
!= null)
53 model
.Constructors
.Clear();
54 model
.ExtendedProperties
.Add("instance.accessor", instanceAccessor
);
55 model
.CustomComponentActivator
= typeof(AccessorActivator
);
57 else if (factoryId
!= null)
59 model
.Constructors
.Clear();
60 model
.ExtendedProperties
.Add("factoryId", factoryId
);
61 model
.ExtendedProperties
.Add("factoryCreate", factoryCreate
);
62 model
.CustomComponentActivator
= typeof(FactoryActivator
);
66 #region Programmatic configuration support
68 public void AddAccessor
<TService
, TFactory
>(
69 string serviceKey
, string instanceAccessorMethod
)
71 AddAccessor
<TService
, TFactory
>(serviceKey
, instanceAccessorMethod
,
72 typeof(TFactory
).FullName
);
75 public void AddAccessor
<TService
, TFactory
>(
76 string serviceKey
, string instanceAccessorMethod
, string factoryId
)
78 IConfiguration cfg
= new MutableConfiguration(serviceKey
);
79 cfg
.Attributes
["instance-accessor"] = instanceAccessorMethod
;
81 AddFactoryComponent
<TService
, TFactory
>(cfg
, factoryId
, serviceKey
);
84 public void AddFactory
<TService
, TFactory
>(
85 string serviceKey
, string factoryCreateMethodName
)
87 AddFactory
<TService
, TFactory
>(
88 serviceKey
, factoryCreateMethodName
, typeof(TFactory
).FullName
);
91 public void AddFactory
<TService
, TFactory
>(
92 string serviceKey
, string factoryCreateMethodName
, string factoryId
)
94 IConfiguration cfg
= new MutableConfiguration(serviceKey
);
95 cfg
.Attributes
["factoryCreate"] = factoryCreateMethodName
;
97 AddFactoryComponent
<TService
, TFactory
>(cfg
, factoryId
, serviceKey
);
102 private void AddFactoryComponent
<TService
, TFactory
>(
103 IConfiguration cfg
, string factoryId
, string serviceKey
)
105 Type factoryType
= typeof(TFactory
);
106 Type serviceType
= typeof(TService
);
108 EnsureFactoryIsRegistered(factoryId
, factoryType
);
110 ComponentModel serviceModel
= Kernel
.ComponentModelBuilder
.BuildModel(
111 serviceKey
, serviceType
,
114 cfg
.Attributes
["factoryId"] = factoryId
;
116 serviceModel
.Configuration
= cfg
;
118 Kernel
.AddCustomComponent(serviceModel
);
121 private void EnsureFactoryIsRegistered(string factoryId
, Type factoryType
)
123 if (!Kernel
.HasComponent(factoryType
))
125 Kernel
.AddComponent(factoryId
, factoryType
);