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
.DynamicLoader
18 using System
.Collections
.Generic
;
20 using Castle
.MicroKernel
.Facilities
;
23 /// Stores instances of <see cref="RemoteLoader"/>.
25 public class DynamicLoaderRegistry
: IDisposable
27 private Dictionary
<string, RemoteLoader
> loaders
= new Dictionary
<string, RemoteLoader
>();
30 /// Register a new loader, for the specified <paramref name="domainId"/>.
32 public void RegisterLoader(string domainId
, RemoteLoader loader
)
35 throw new ObjectDisposedException("DynamicLoaderRegistry");
37 loaders
.Add(domainId
, loader
);
41 /// Gets the <see cref="RemoteLoader"/> instance for the specified <paramref name="domainId"/>.
43 public RemoteLoader
GetLoader(string domainId
)
46 throw new ObjectDisposedException("DynamicLoaderRegistry");
49 if (!loaders
.TryGetValue(domainId
, out loader
))
50 throw new FacilityException("Domain not found: " + domainId
);
56 /// Registers a specific component on a specific domain.
59 /// The implementation simply calls <see cref="GetLoader"/> to get the correct
60 /// <see cref="RemoteLoader"/>, then add the component to the <see cref="RemoteLoader.Kernel"/>.
62 public void RegisterComponentOnDomain(string domainId
, string key
, Type service
, Type component
)
65 throw new ObjectDisposedException("DynamicLoaderRegistry");
67 GetLoader(domainId
).Kernel
.AddComponent(key
, service
, component
);
71 /// Implementation of <see cref="IDisposable"/>.
75 foreach (RemoteLoader loader
in loaders
.Values
)
77 loader
.Kernel
.Parent
.RemoveChildKernel(loader
.Kernel
);
81 AppDomain
.Unload(loader
.AppDomain
);