18 using System.Collections.Generic;
20 using System.Runtime.InteropServices;
22 namespace Deveel.Data.Services {
25 return scope.
Resolve(serviceType, null);
28 public static TService Resolve<TService>(
this IScope scope,
object serviceKey) {
29 return (TService) scope.Resolve(typeof(TService), serviceKey);
32 public static TService Resolve<TService>(
this IScope scope) {
33 return scope.Resolve<TService>(null);
36 public static IEnumerable<TService> ResolveAll<TService>(
this IScope scope) {
37 return scope.ResolveAll(typeof (TService)).Cast<TService>();
40 public static void Register(
this IScope scope, Type serviceType, Type implementationType,
object serviceKey) {
42 registration.ServiceKey = serviceKey;
47 Register(scope, serviceType, null);
50 public static void Register(
this IScope scope, Type serviceType,
object serviceKey) {
51 if (serviceType == null)
52 throw new ArgumentNullException(
"serviceType");
54 if (!serviceType.IsClass)
55 throw new ArgumentException(String.Format(
"The service type '{0}' to register is not a class.", serviceType));
57 var interfaces = serviceType.GetInterfaces();
58 foreach (var interfaceType
in interfaces) {
59 scope.Register(interfaceType, serviceType, serviceKey);
62 scope.Register(serviceType, serviceType, serviceKey);
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);
70 public static void Register<TService, TImplementation>(
this IScope scope)
71 where TImplementation :
class, TService {
72 scope.Register<TService, TImplementation>(null);
75 public static void Register<TService>(
this IScope scope,
object serviceKey)
76 where TService :
class {
77 scope.Register(typeof(TService), serviceKey);
80 public static void Register<TService>(
this IScope scope)
81 where TService :
class {
82 scope.Register<TService>(null);
86 RegisterInstance(scope, serviceType, instance, null);
90 if (serviceType == null)
91 throw new ArgumentNullException(
"serviceType");
93 throw new ArgumentNullException(
"instance");
95 var implementationType = instance.GetType();
97 registration.Instance = instance;
98 registration.ServiceKey = serviceKey;
102 public static void RegisterInstance<TService>(
this IScope scope, TService instance) where TService :
class {
103 RegisterInstance(scope, instance, null);
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);
112 scope.RegisterInstance(typeof(TService), instance, serviceKey);
119 public static bool Unregister<TService>(
this IScope scope,
object serviceKey) {
120 return scope.Unregister(typeof (TService), serviceKey);
123 public static bool Unregister<TService>(
this IScope scope) {
124 return scope.Unregister<TService>(null);
127 public static void Replace(
this IScope scope, Type serviceType, Type implementationType) {
128 scope.Replace(serviceType, implementationType, null);
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);
136 public static void Replace<TService, TImplementation>(
this IScope scope)
137 where TImplementation :
class, TService {
138 scope.Replace<TService, TImplementation>(null);
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);
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)