Пример #1
0
        /// <summary>
        /// Returns all services of the type from the container.
        /// </summary>
        /// <param name="serviceType">Type of the returnable services.</param>
        /// <param name="getEnabled">If true then returns only enabled services.</param>
        /// <returns>Collection of services.</returns>
        public StiServiceContainer GetServices(Type serviceType, bool getEnabled, bool callBeforeGetService)
        {
            if (callBeforeGetService && BeforeGetService != null)
            {
                var args = new StiServiceActionEventArgs(StiServiceActionType.Get);
                BeforeGetService(serviceType, args);
                if (args.Processed)
                {
                    var serviceContainer = new StiServiceContainer();
                    if (args.Services != null)
                    {
                        foreach (var service in args.Services)
                        {
                            serviceContainer.Add(service);
                        }
                    }
                    return(serviceContainer);
                }
            }

            if (!noTypes)
            {
                StiServiceContainer services = typesHashtable[serviceType] as StiServiceContainer;
                if (services != null)
                {
                    StiServiceContainer serv = new StiServiceContainer();
                    lock (services)
                    {
                        foreach (StiService service in services)
                        {
                            if (service.ServiceEnabled || (!getEnabled))
                            {
                                serv.Add(service);
                            }
                        }
                    }
                    return(serv);
                }
                else
                {
                    return(new StiServiceContainer());
                }
            }
            else
            {
                StiServiceContainer services = new StiServiceContainer(true);

                foreach (StiService service in List)
                {
                    if (service.ServiceType == serviceType && (service.ServiceEnabled || (!getEnabled)))
                    {
                        services.Add(service);
                    }
                }
                return(services);
            }
        }
Пример #2
0
        /// <summary>
        /// Adds service to the container.
        /// </summary>
        /// <param name="service">Service.</param>
        public void Add(StiService service, bool callBeforeGetService)
        {
            if (service.ServiceType != null)
            {
                if (callBeforeGetService && BeforeGetService != null)
                {
                    var args = new StiServiceActionEventArgs(StiServiceActionType.Add)
                    {
                        Services = new List <StiService> {
                            service
                        }
                    };
                    BeforeGetService(service.ServiceType, args);
                    if (args.Processed)
                    {
                        return;
                    }
                }

                service.PackService();

                lock (List)
                {
                    lock (keysHashtable)
                    {
                        string key = GetStringFromService(service);
                        if (keysHashtable[key] == null)
                        {
                            keysHashtable.Add(key, service);
                            List.Add(service);

                            if (!noTypes)
                            {
                                lock (typesHashtable)
                                {
                                    StiServiceContainer services = typesHashtable[service.ServiceType] as StiServiceContainer;

                                    if (services == null)
                                    {
                                        services = new StiServiceContainer(true);
                                        typesHashtable.Add(service.ServiceType, services);
                                    }
                                    lock (services)
                                    {
                                        services.Add(service);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #3
0
 /// <summary>
 /// Gets first service of the type from the container.
 /// </summary>
 /// <param name="serviceType">Service type.</param>
 /// <returns>Service.</returns>
 public StiService GetService(Type serviceType, bool getEnabled, bool callBeforeGetService)
 {
     if (callBeforeGetService && BeforeGetService != null)
     {
         var args = new StiServiceActionEventArgs(StiServiceActionType.Get);
         BeforeGetService(serviceType, args);
         if (args.Processed)
         {
             return(args.Services != null?args.Services.FirstOrDefault() : null);
         }
     }
     if (!noTypes)
     {
         StiServiceContainer services = typesHashtable[serviceType] as StiServiceContainer;
         if (services != null && services.Count > 0)
         {
             foreach (StiService service in services)
             {
                 if (service.ServiceEnabled || (!getEnabled))
                 {
                     return(service);
                 }
             }
             return(null);
         }
         return(null);
     }
     else
     {
         foreach (StiService service in List)
         {
             if (service.ServiceType == serviceType && (service.ServiceEnabled || (!getEnabled)))
             {
                 return(service);
             }
         }
         return(null);
     }
 }