Esempio n. 1
0
        /// <summary>
        /// Returns services which are in the specified assembly.
        /// </summary>
        /// <param name="a">Assembly.</param>
        /// <returns>Services.</returns>
        public static StiServiceContainer GetServicesFromAssembly(Assembly a)
        {
            var serviceContainer = new StiServiceContainer();

            if (a != null)
            {
                var  types       = GetTypesFromAssembly(a);
                Type typeService = typeof(StiService);
                foreach (Type type in types)
                {
                    if (StiTypeFinder.FindType(type, typeService) && (!type.IsAbstract))
                    {
                        try
                        {
                            var service = StiActivator.CreateObject(type) as StiService;
                            if (service != null && service.ServiceType != null)
                            {
                                serviceContainer.Add(service);
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            }
            return(serviceContainer);
        }
Esempio n. 2
0
 /// <summary>
 /// Removes service from the container.
 /// </summary>
 /// <param name="services">Services.</param>
 public void Remove(StiServiceContainer services)
 {
     foreach (StiService service in services)
     {
         Remove(service);
     }
 }
Esempio n. 3
0
        public void Insert(int index, StiService service)
        {
            lock (List)
            {
                service.PackService();
                List.Insert(index, service);

                lock (keysHashtable)
                {
                    string key = GetStringFromService(service);
                    if (keysHashtable[key] == null)
                    {
                        keysHashtable.Add(key, 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);
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Adds services to the container.
 /// </summary>
 /// <param name="services">Services.</param>
 public void AddRange(StiServiceContainer services)
 {
     foreach (StiService service in services)
     {
         Add(service);
     }
 }
Esempio n. 5
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);
            }
        }
Esempio n. 6
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);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Gets a service container that contains all standard services.
        /// </summary>
        /// <returns>Service container that contains all standard services.</returns>
        public static StiServiceContainer GetStandardServices()
        {
            var services = new StiServiceContainer();

            foreach (string assemblyName in standardAssemblies)
            {
                try
                {
                    var srv = GetServicesFromAssembly(assemblyName);
                    if (srv != null)
                    {
                        services.AddRange(srv);
                    }
                }
                catch
                {
                }
            }
            return(services);
        }
Esempio n. 8
0
        /// <summary>
        /// Removes service from the container.
        /// </summary>
        /// <param name="service">Service.</param>
        public void Remove(StiService service)
        {
            lock (List)
            {
                List.Remove(service);
                keysHashtable.Remove(GetStringFromService(service));

                if (!noTypes)
                {
                    lock (typesHashtable)
                    {
                        StiServiceContainer services = typesHashtable[service.ServiceType] as StiServiceContainer;
                        lock (services)
                        {
                            services.Remove(service);
                        }
                    }
                }
            }
        }
Esempio n. 9
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);
     }
 }
Esempio n. 10
0
        /// <summary>
        /// Removes services of the type from the container.
        /// </summary>
        /// <param name="serviceType">Service type.</param>
        public void Remove(Type serviceType)
        {
            lock (List)
            {
                lock (keysHashtable)
                {
                    StiServiceContainer cont = GetServices(serviceType);
                    foreach (StiService service in cont)
                    {
                        List.Remove(service);
                        keysHashtable.Remove(GetStringFromService(service));
                    }

                    if (!noTypes)
                    {
                        lock (typesHashtable)
                        {
                            typesHashtable.Remove(serviceType);
                        }
                    }
                }
            }
        }
Esempio n. 11
0
        private void BuildTypes()
        {
            if (!noTypes)
            {
                lock (typesHashtable)
                {
                    typesHashtable.Clear();
                    foreach (StiService service in List)
                    {
                        StiServiceContainer services = typesHashtable[service.ServiceType] as StiServiceContainer;

                        if (services == null)
                        {
                            services = new StiServiceContainer(true);
                            typesHashtable.Add(service.ServiceType, services);
                        }
                        lock (services)
                        {
                            services.Add(service);
                        }
                    }
                }
            }
        }