Esempio n. 1
0
 public ServiceDescriptor(Type typeInterface, Type typeClass, ServiceScope scope, ServiceLifeTime lifeTime = ServiceLifeTime.Transient)
 {
     if (typeClass == null)
     {
         throw new ArgumentNullException(nameof(typeClass));
     }
     Scope         = scope;
     TypeInterface = typeInterface;
     TypeClass     = typeClass;
     LifeTime      = lifeTime;
 }
Esempio n. 2
0
            public IServiceContainer As <TImplementation>(
                Func <TImplementation> getService, ServiceLifeTime lifeTime) where TImplementation : T
            {
                var keyType = _types.First();

                if (_resolver._registrations.ContainsKey(keyType))
                {
                    throw new ArgumentException(
                              string.Format("A service is already registered for '{0}'", keyType.FullName),
                              "getService");
                }

                foreach (var type in _types.Skip(1))
                {
                    _resolver._registrationKeys.Add(type, keyType);
                }

                var fromCache =
                    (Func <IDictionary <Type, object>, Type, Func <TImplementation>, object>)
                        ((c, t, n) =>
                {
                    if (c.ContainsKey(t))
                    {
                        return(c[t]);
                    }
                    var o = n();
                    c.Add(t, o);

                    return(o);
                });

                switch (lifeTime)
                {
                case ServiceLifeTime.Singleton:
                    _resolver._registrations.Add(keyType, () => fromCache(_resolver._singletonCache, keyType, getService));

                    break;

                case ServiceLifeTime.Session:
                    _resolver._registrations.Add(keyType, () => fromCache(_resolver._sessionCache, keyType, getService));

                    break;

                case ServiceLifeTime.Transient:
                    _resolver._registrations.Add(keyType, () => getService());

                    break;
                }

                return(_resolver);
            }
Esempio n. 3
0
        public ServiceDescriptor(Type serviceType, ServiceLifeTime lifetime)
        {
            ServiceType = serviceType;

            Lifetime = lifetime;
        }
Esempio n. 4
0
 public ServiceDescriptor(Type serviceType, ServiceLifeTime lifetime)
 {
     this.ServiceType = serviceType;
     this.Lifetime    = lifetime;
 }
Esempio n. 5
0
 public ServiceDescriptor(object implementation, ServiceLifeTime lifetime)
 {
     ServiceType    = implementation.GetType();
     Implementation = implementation;
     Lifetime       = lifetime;
 }
Esempio n. 6
0
 public static void Register <TService, TImplementation>(ServiceLifeTime lifeTime) where TImplementation : TService
 {
     serviceDescriptors.Add(new ServiceDescriptor(typeof(TService), typeof(TImplementation), lifeTime));
 }
Esempio n. 7
0
 public static void Register <TService>(TService implementation, ServiceLifeTime lifeTime)
 {
     serviceDescriptors.Add(new ServiceDescriptor(implementation, lifeTime));
 }
Esempio n. 8
0
 public ServieceDescriptor(Type ServiceType, Type ImplementationType, ServiceLifeTime lifeTime)
 {
     this.ServiceType        = ServiceType;
     LifeTime                = lifeTime;
     this.ImplementationType = ImplementationType;
 }
Esempio n. 9
0
 public static void Register <TService>(ServiceLifeTime lifeTime)
 {
     serviceDescriptors.Add(new ServiceDescriptor(typeof(TService), lifeTime));
 }
Esempio n. 10
0
        /// <summary>
        /// Create service define.
        /// </summary>
        /// <param name="serviceType">Service type.</param>
        /// <param name="implementInstance">Implemetn instance.</param>
        /// <param name="implementType">Implement type.</param>
        /// <param name="implementFactory">Implement factory.</param>
        /// <param name="lifeTime">Service life time.</param>
        /// <returns>Service defination.</returns>
        private static IServiceDefinition CreateServiceDefine(Type serviceType, object implementInstance, Type implementType, Func <object> implementFactory, ServiceLifeTime lifeTime)
        {
            if (implementInstance == null && implementType == null && implementFactory == null)
            {
                throw new ArgumentNullException("Implement and factory is null.");
            }

            if (implementInstance != null)
            {
                return(new ServiceDefinition(serviceType, implementInstance));
            }
            else if (implementFactory != null)
            {
                return(new ServiceDefinition(serviceType, implementFactory, lifeTime));
            }
            else
            {
                return(new ServiceDefinition(serviceType, implementType, lifeTime));
            }
        }
Esempio n. 11
0
 public ServieceDescriptor(object Implementation, ServiceLifeTime lifeTime)
 {
     this.Implementation = Implementation;
     ServiceType         = Implementation.GetType();
     LifeTime            = lifeTime;
 }
Esempio n. 12
0
 /// <summary>
 /// Register service with service type and implement factory.
 /// </summary>
 /// <typeparam name="TServiceType">Service type.</typeparam>
 /// <param name="services">Service definition collection.</param>
 /// <param name="factory">Implement factory.</param>
 /// <param name="lifeTime">Service life time.</param>
 public static IServiceDefinitionCollection RegisterService <TServiceType>(this IServiceDefinitionCollection services, Func <object> factory, ServiceLifeTime lifeTime)
 {
     return(services.RegisterService(typeof(TServiceType), factory, lifeTime));
 }
Esempio n. 13
0
 /// <summary>
 /// Register service with service type and implement type.
 /// </summary>
 /// <typeparam name="TServiceType">Service type.</typeparam>
 /// <typeparam name="TImplementType">Implement type.</typeparam>
 /// <param name="services">Service definition collection.</param>
 /// <param name="lifeTime">Service life time.</param>
 public static IServiceDefinitionCollection RegisterService <TServiceType, TImplementType>(this IServiceDefinitionCollection services, ServiceLifeTime lifeTime)
 {
     return(services.RegisterService(typeof(TServiceType), typeof(TImplementType), lifeTime));
 }
Esempio n. 14
0
 public ServiceDefinition(Type serivceType, Type implementType, ServiceLifeTime lifeTime)
 {
     this.ServiceType   = serivceType;
     this.ImplementType = implementType;
     this.LifeTime      = lifeTime;
 }
Esempio n. 15
0
 public ServiceDefinition(Type serivceType, Func <object> factory, ServiceLifeTime lifeTime)
 {
     this.ServiceType      = serivceType;
     this.ImplementFactory = factory;
     this.LifeTime         = lifeTime;
 }
Esempio n. 16
0
 // interface + objet déjà instancié
 public ServiceDescriptor(Type serviceType, object implementation, ServiceLifeTime lifeTime)
 {
     ServiceType    = serviceType;
     Implementation = implementation;
     LifeTime       = lifeTime;
 }
Esempio n. 17
0
 public ServiceDescriptor(Type serviceType, Type implementationType, ServiceLifeTime lifetime)
 {
     ServiceType        = serviceType;
     ImplementationType = implementationType;
     Lifetime           = lifetime;
 }
Esempio n. 18
0
        /// <summary>
        /// Register service with service type and implement instance.
        /// </summary>
        /// <param name="services">Service definition collection.</param>
        /// <param name="serviceType">Service type.</param>
        /// <param name="instance">Implement instance.</param>
        /// <param name="lifeTime">Service life time.</param>
        public static IServiceDefinitionCollection RegisterService(this IServiceDefinitionCollection services, Type serviceType, object instance, ServiceLifeTime lifeTime)
        {
            if (serviceType == null)
            {
                throw new ArgumentNullException("Service type is null.");
            }

            services.AddOrUpdateServiceDefinition(serviceType,
                                                  CreateServiceDefine(serviceType, instance, null, null, lifeTime),
                                                  (type, old) => CreateServiceDefine(serviceType, instance, null, null, lifeTime));

            return(services);
        }
Esempio n. 19
0
 /// <summary>
 /// Register service with service type and implement instance.
 /// </summary>
 /// <typeparam name="TServiceType">Service type.</typeparam>
 /// <param name="services">Service definition collection.</param>
 /// <param name="instance">Implement instance.</param>
 public static IServiceDefinitionCollection RegisterService <TServiceType>(this IServiceDefinitionCollection services, object instance, ServiceLifeTime lifeTime)
 {
     return(services.RegisterService(typeof(TServiceType), instance, lifeTime));
 }
Esempio n. 20
0
 public ServieceDescriptor(Type type, ServiceLifeTime lifeTime)
 {
     ServiceType = type;
     LifeTime    = lifeTime;
 }