19 using System.Collections.Generic;
23 namespace Deveel.Data.Services {
34 container = parent.
container.OpenScope(scopeName)
35 .With(rules => rules.WithDefaultReuseInsteadOfTransient(Reuse.InCurrentNamedScope(scopeName)));
37 ScopeName = scopeName;
39 container =
new Container(Rules.Default
40 .WithDefaultReuseInsteadOfTransient(Reuse.Singleton)
41 .WithoutThrowOnRegisteringDisposableTransient());
44 registrationProviders =
new List<IRegistrationConfigurationProvider>();
51 object IServiceProvider.GetService(Type serviceType) {
52 return Resolve(serviceType, null);
55 protected virtual void Dispose(
bool disposing) {
58 if (container != null)
66 private string ScopeName {
get; set; }
70 GC.SuppressFinalize(
this);
74 if (registrationProviders != null && registrationProviders.Count > 0) {
75 foreach (var provider
in registrationProviders) {
76 RegisterConfiguration(provider);
79 registrationProviders.Clear();
90 Register(registration);
94 RegisterConfigurations();
98 public object Resolve(Type serviceType,
object name) {
99 if (serviceType == null)
100 throw new ArgumentNullException(
"serviceType");
102 if (container == null)
103 throw new InvalidOperationException(
"The container was not initialized.");
106 return container.Resolve(serviceType, name, IfUnresolved.ReturnDefault);
112 registrationProviders.Add(config);
117 if (serviceType == null)
118 throw new ArgumentNullException(
"serviceType");
120 if (container == null)
121 throw new InvalidOperationException(
"The container was not initialized.");
124 return container.ResolveMany<
object>(serviceType);
129 if (registration == null)
130 throw new ArgumentNullException(
"registration");
132 if (container == null)
133 throw new InvalidOperationException(
"The container was not initialized.");
137 var service = registration.
Instance;
141 var reuse = Reuse.Singleton;
142 if (!String.IsNullOrEmpty(ScopeName))
143 reuse = Reuse.InCurrentNamedScope(ScopeName);
145 if (!String.IsNullOrEmpty(registration.
Scope))
146 reuse = Reuse.InCurrentNamedScope(registration.
Scope);
148 if (service == null) {
149 container.Register(serviceType, implementationType, serviceKey: serviceName, reuse: reuse);
151 container.RegisterInstance(serviceType, service, serviceKey: serviceName, reuse: reuse);
156 public bool Unregister(Type serviceType,
object serviceName) {
157 if (serviceType == null)
158 throw new ArgumentNullException(
"serviceType");
160 if (container == null)
161 throw new InvalidOperationException(
"The container was not initialized.");
164 container.Unregister(serviceType, serviceName);
169 #region RegistrationConfiguration
173 Container = container;
178 ImplementationType = typeof (TImplementation);
185 ImplementationType = typeof (TImplementation);
191 public Type ServiceType {
192 get {
return typeof (TService); }
197 throw new InvalidOperationException(
"The registration was already bound.");
202 private bool IsBound {
get; set; }
204 public Type ImplementationType {
get; set; }
206 public object ServiceKey {
get; set; }
208 public string ScopeName {
get; set; }
210 public object Instance {
get;
private set; }
215 #region RegistrationWithBindingConfiguration
221 this.configuration = configuration;
object Resolve(Type serviceType, object name)
RegistrationConfiguration< TService > configuration
IRegistrationWithBindingConfiguration< TService, TImplementation > WithKey(object serviceKey)
List< IRegistrationConfigurationProvider > registrationProviders
RegistrationConfiguration(ServiceContainer container)
void Register(ServiceRegistration registration)
bool Unregister(Type serviceType, object serviceName)
IRegistrationWithBindingConfiguration< TService, TImplementation > InScope(string scopeName)
void RegisterConfiguration(IRegistrationConfigurationProvider provider)
ServiceContainer(ServiceContainer parent, string scopeName)
IEnumerable ResolveAll(Type serviceType)
RegistrationWithBindingConfiguration(RegistrationConfiguration< TService > configuration)
virtual void Dispose(bool disposing)
void RegisterConfigurations()
IScope OpenScope(string name)