DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
ScopeExtensions.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20 using System.Runtime.InteropServices;
21 
22 namespace Deveel.Data.Services {
23  public static class ScopeExtensions {
24  public static object Resolve(this IScope scope, Type serviceType) {
25  return scope.Resolve(serviceType, null);
26  }
27 
28  public static TService Resolve<TService>(this IScope scope, object serviceKey) {
29  return (TService) scope.Resolve(typeof(TService), serviceKey);
30  }
31 
32  public static TService Resolve<TService>(this IScope scope) {
33  return scope.Resolve<TService>(null);
34  }
35 
36  public static IEnumerable<TService> ResolveAll<TService>(this IScope scope) {
37  return scope.ResolveAll(typeof (TService)).Cast<TService>();
38  }
39 
40  public static void Register(this IScope scope, Type serviceType, Type implementationType, object serviceKey) {
41  var registration = new ServiceRegistration(serviceType, implementationType);
42  registration.ServiceKey = serviceKey;
43  scope.Register(registration);
44  }
45 
46  public static void Register(this IScope scope, Type serviceType) {
47  Register(scope, serviceType, null);
48  }
49 
50  public static void Register(this IScope scope, Type serviceType, object serviceKey) {
51  if (serviceType == null)
52  throw new ArgumentNullException("serviceType");
53 
54  if (!serviceType.IsClass)
55  throw new ArgumentException(String.Format("The service type '{0}' to register is not a class.", serviceType));
56 
57  var interfaces = serviceType.GetInterfaces();
58  foreach (var interfaceType in interfaces) {
59  scope.Register(interfaceType, serviceType, serviceKey);
60  }
61 
62  scope.Register(serviceType, serviceType, serviceKey);
63  }
64 
65  public static void Register<TService, TImplementation>(this IScope scope, object serviceKey)
66  where TImplementation : class, TService {
67  scope.Register(typeof(TService), typeof(TImplementation), serviceKey);
68  }
69 
70  public static void Register<TService, TImplementation>(this IScope scope)
71  where TImplementation : class, TService {
72  scope.Register<TService, TImplementation>(null);
73  }
74 
75  public static void Register<TService>(this IScope scope, object serviceKey)
76  where TService : class {
77  scope.Register(typeof(TService), serviceKey);
78  }
79 
80  public static void Register<TService>(this IScope scope)
81  where TService : class {
82  scope.Register<TService>(null);
83  }
84 
85  public static void RegisterInstance(this IScope scope, Type serviceType, object instance) {
86  RegisterInstance(scope, serviceType, instance, null);
87  }
88 
89  public static void RegisterInstance(this IScope scope, Type serviceType, object instance, object serviceKey) {
90  if (serviceType == null)
91  throw new ArgumentNullException("serviceType");
92  if (instance == null)
93  throw new ArgumentNullException("instance");
94 
95  var implementationType = instance.GetType();
96  var registration = new ServiceRegistration(serviceType, implementationType);
97  registration.Instance = instance;
98  registration.ServiceKey = serviceKey;
99  scope.Register(registration);
100  }
101 
102  public static void RegisterInstance<TService>(this IScope scope, TService instance) where TService : class {
103  RegisterInstance(scope, instance, null);
104  }
105 
106  public static void RegisterInstance<TService>(this IScope scope, TService instance, object serviceKey) where TService : class {
107  var interfaces = typeof (TService).GetInterfaces();
108  foreach (var interfaceType in interfaces) {
109  scope.RegisterInstance(interfaceType, instance, serviceKey);
110  }
111 
112  scope.RegisterInstance(typeof(TService), instance, serviceKey);
113  }
114 
115  public static bool Unregister(this IScope scope, Type serviceType) {
116  return scope.Unregister(serviceType, null);
117  }
118 
119  public static bool Unregister<TService>(this IScope scope, object serviceKey) {
120  return scope.Unregister(typeof (TService), serviceKey);
121  }
122 
123  public static bool Unregister<TService>(this IScope scope) {
124  return scope.Unregister<TService>(null);
125  }
126 
127  public static void Replace(this IScope scope, Type serviceType, Type implementationType) {
128  scope.Replace(serviceType, implementationType, null);
129  }
130 
131  public static void Replace(this IScope scope, Type serviceType, Type implementationType, object serviceKey) {
132  if (scope.Unregister(serviceType, serviceKey))
133  scope.Register(serviceType, implementationType, serviceKey);
134  }
135 
136  public static void Replace<TService, TImplementation>(this IScope scope)
137  where TImplementation : class, TService {
138  scope.Replace<TService, TImplementation>(null);
139  }
140 
141  public static void Replace<TService, TImplementation>(this IScope scope, object serviceKey)
142  where TImplementation : class, TService {
143  scope.Replace(typeof(TService), typeof(TImplementation), serviceKey);
144  }
145  }
146 }
static void RegisterInstance(this IScope scope, Type serviceType, object instance, object serviceKey)
bool Unregister(Type serviceType, object serviceKey)
static bool Unregister(this IScope scope, Type serviceType)
static void Replace(this IScope scope, Type serviceType, Type implementationType)
static void Replace(this IScope scope, Type serviceType, Type implementationType, object serviceKey)
static void Register(this IScope scope, Type serviceType, Type implementationType, object serviceKey)
static object Resolve(this IScope scope, Type serviceType)
object Resolve(Type serviceType, object serviceKey)
static void RegisterInstance(this IScope scope, Type serviceType, object instance)
static void Register(this IScope scope, Type serviceType)
static void Register(this IScope scope, Type serviceType, object serviceKey)
void Register(ServiceRegistration registration)