Пример #1
0
        private EventFactorySingleton()
        {
            this.eventHandlers = new Dictionary <string, IList <IEvent> >();

            var eventTypes = ClassHarvester.GetConcreteSubclassesOf <IEvent>();

            foreach (Type eventType in eventTypes)
            {
                var eventInterfaceTypes = eventType.GetInterfaces()
                                          .Where(x => typeof(IEvent).IsAssignableFrom(x) && x != typeof(IEvent));

                if (eventInterfaceTypes.Count() == 0)
                {
                    throw new EventFactorySingletonException("Cannot retrieve event interface types of the event handler type.");
                }

                var instance = (IEvent)Activator.CreateInstance(eventType);

                foreach (var eventInterfaceType in eventInterfaceTypes)
                {
                    if (!this.eventHandlers.ContainsKey(eventInterfaceType.FullName))
                    {
                        this.eventHandlers[eventInterfaceType.FullName] = new List <IEvent>();
                    }

                    this.eventHandlers[eventInterfaceType.FullName].Add(instance);
                }
            }
        }
        private CacheSingleton()
        {
            var cacheProviderTypes = ClassHarvester.GetConcreteSubclassesOf <ICacheProvider>();

            if (cacheProviderTypes.Count() != 1)
            {
                throw new CachingException("Exactly one cache provider must exist.");
            }

            this.cacheProvider = (ICacheProvider)Activator.CreateInstance(cacheProviderTypes.ElementAt(0));
        }
        private HelperFactorySingleton()
        {
            this.helperInstances = new Dictionary <string, object>();

            var helperTypes = ClassHarvester.GetConcreteSubclassesOf <IHelper>();

            foreach (Type helperType in helperTypes)
            {
                if (helperType.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic).Any())
                {
                    throw new HelperFactorySingletonException($"Invalid dependent class: '{helperType.FullName}'");
                }

                var helperTypeCtors = helperType.GetConstructors(BindingFlags.Instance | BindingFlags.Public);

                if (helperTypeCtors.Length != 1)
                {
                    throw new HelperFactorySingletonException($"Invalid dependent class: '{helperType.FullName}'");
                }

                var parameterlessCtor = helperTypeCtors.SingleOrDefault(x => x.GetParameters().Length == 0);

                if (parameterlessCtor == null)
                {
                    throw new HelperFactorySingletonException($"Invalid dependent class: '{helperType.FullName}'");
                }

                try
                {
                    object instance = Activator.CreateInstance(helperType);

                    if (instance == null)
                    {
                        throw new HelperFactorySingletonException($"Invalid dependent class: '{helperType.FullName}'");
                    }

                    this.helperInstances.Add(helperType.FullName, instance);
                }
                catch (Exception ex)
                {
                    throw new HelperFactorySingletonException($"Invalid dependent class: '{helperType.FullName}'", ex);
                }
            }
        }
        public void RegisterRoutes(RouteCollection routes)
        {
            var routeProviderTypes = ClassHarvester.GetConcreteSubclassesOf <IRouteProvider>();
            var routeProviders     = new List <IRouteProvider>();

            foreach (var routeProviderType in routeProviderTypes)
            {
                var routeProviderInstance = Activator.CreateInstance(routeProviderType);

                if (routeProviderInstance != null)
                {
                    routeProviders.Add(routeProviderInstance as IRouteProvider);
                }
            }

            routeProviders = routeProviders.OrderByDescending(x => x.Priority).ToList();

            routeProviders.ForEach(rp => rp.RegisterRoutes(routes));
        }