Ejemplo n.º 1
0
        public static ILifecycle GetLifecycle(InstanceScope scope)
        {
            switch (scope)
            {
                case InstanceScope.PerRequest:
                    return null;

                case InstanceScope.Singleton:
                    return new SingletonLifecycle();

                case InstanceScope.HttpContext:
                    return new HttpContextLifecycle();

                case InstanceScope.ThreadLocal:
                    return new ThreadLocalStorageLifecycle();

                case InstanceScope.Hybrid:
                    return new HybridLifecycle();

                case InstanceScope.HttpSession:
                    return new HttpSessionLifecycle();

                case InstanceScope.HybridHttpSession:
                    return new HybridSessionLifecycle();

                case InstanceScope.Unique:
                    return new UniquePerRequestLifecycle();
            }

            throw new ArgumentOutOfRangeException("scope");
        }
Ejemplo n.º 2
0
        public static ILifecycle GetLifecycle(InstanceScope scope)
        {
            switch (scope)
            {
            case InstanceScope.PerRequest:
                return(null);

            case InstanceScope.Singleton:
                return(new SingletonLifecycle());

            case InstanceScope.HttpContext:
                return(new HttpContextLifecycle());

            case InstanceScope.ThreadLocal:
                return(new ThreadLocalStorageLifecycle());

            case InstanceScope.Hybrid:
                return(new HybridLifecycle());

            case InstanceScope.HttpSession:
                return(new HttpSessionLifecycle());

            case InstanceScope.HybridHttpSession:
                return(new HybridSessionLifecycle());

            case InstanceScope.Unique:
                return(new UniquePerRequestLifecycle());
            }

            throw new ArgumentOutOfRangeException("scope");
        }
        public object GetInstance(InstanceContext instanceContext, Message message)
        {
            var scope = new InstanceScope(_provider);

            instanceContext.Extensions.Add(scope);
            var instance = scope.ServiceProvider.GetService(_type);

            return(instance);
        }
Ejemplo n.º 4
0
 public GenericConnectionScannerWithScope(Type openType, InstanceScope instanceScope)
 {
     _openType      = openType;
     _instanceScope = instanceScope;
     if (!_openType.IsOpenGeneric())
     {
         throw new ApplicationException("This scanning convention can only be used with open generic types");
     }
 }
Ejemplo n.º 5
0
        private static InstanceScope findScope(XmlElement familyElement)
        {
            InstanceScope returnValue = InstanceScope.PerRequest;

            familyElement.ForAttributeValue(SCOPE,
                                            scope => { returnValue = (InstanceScope)Enum.Parse(typeof(InstanceScope), scope); });

            return(returnValue);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Register specified type to the container. Default scope is transient
        /// </summary>
        /// <typeparam name="TService">service type</typeparam>
        /// <param name="builder">container builder</param>
        /// <param name="scope">resolution scope</param>
        public static void RegisterType <TService>(
            this IObjectFactoryBuilder builder,
            InstanceScope scope = InstanceScope.Transient)
        {
            Contract.Requires(builder != null);

            builder.Add(new FactoryBuilderType {
                type = typeof(TService), Scope = scope, IsGeneric = false
            });
        }
Ejemplo n.º 7
0
        public void SetScopeTo(InstanceScope scope)
        {
            if (scope == InstanceScope.Transient)
            {
                _lifecycle = null;
                return;
            }

            _lifecycle = Lifecycles.GetLifecycle(scope);
        }
Ejemplo n.º 8
0
    public void SetScopeTo(InstanceScope scope)
    {
        if (scope == InstanceScope.Transient)
            {
                _lifecycle = null;
                return;
            }

            _lifecycle = Lifecycles.GetLifecycle(scope);
    }
Ejemplo n.º 9
0
 public void Register(Type from, Type to, InstanceScope scope)
 {
     if (scope == InstanceScope.Singleton)
     {
         this.container.RegisterType(from, to, new ContainerControlledLifetimeManager());
     }
     else
     {
         this.container.RegisterType(from, to);
     }
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Register specified type to the container as custom services. Default scope is transient
        /// </summary>
        /// <typeparam name="TService">service implementation</typeparam>
        /// <typeparam name="TAs1">service type</typeparam>
        /// <typeparam name="TAs2">additional service type</typeparam>
        /// <param name="builder">container builder</param>
        /// <param name="scope">resolution scope</param>
        public static void RegisterType <TService, TAs1, TAs2>(
            this IObjectFactoryBuilder builder,
            InstanceScope scope = InstanceScope.Transient)
            where TService : TAs1, TAs2
        {
            Contract.Requires(builder != null);

            builder.Add(new FactoryBuilderType {
                type = typeof(TService), Scope = scope, AsType = new[] { typeof(TAs1), typeof(TAs2) }, IsGeneric = false
            });
        }
        private void assertScopeLeadsToInterceptor(InstanceScope scope, Type interceptorType)
        {
            var att = new PluginFamilyAttribute("something");

            att.Scope = scope;

            var family = new PluginFamily(typeof(TypeThatDoesNotHaveCustomMementoSource));

            att.Configure(family);

            Assert.IsInstanceOfType(interceptorType, family.Lifecycle);
        }
Ejemplo n.º 12
0
 public DependencyDescriptor(Type type, Func <IDependencyResolver, object> resolveFn, RegistrationOptions registration, InstanceScope scope, bool instantResolution)
 {
     Type         = type;
     ResolveFn    = resolveFn;
     Registration = registration;
     if (!registration.AsTypes.Any())
     {
         registration.As(type);
     }
     Scope             = scope;
     InstantResolution = instantResolution;
 }
Ejemplo n.º 13
0
        /// <summary>
        /// Register specified type to the container as custom service with provided scope.
        /// </summary>
        /// <param name="factory">container scope</param>
        /// <param name="type">service type</param>
        /// <param name="asType">register as</param>
        /// <param name="scope">resolution scope</param>
        public static void RegisterType(this IObjectFactory factory, Type type, InstanceScope scope, params Type[] asType)
        {
            Contract.Requires(factory != null);
            Contract.Requires(type != null);

            var ofb = new ObjectFactoryBuilder();

            ofb.Add(new FactoryBuilderType {
                type = type, Scope = scope, AsType = asType
            });
            factory.Register(ofb);
        }
Ejemplo n.º 14
0
        public void ParseDefaultElement(XmlElement element)
        {
            var pluginTypePath = new TypePath(element.GetAttribute(PLUGIN_TYPE));


            _builder.ConfigureFamily(pluginTypePath, family =>
            {
                InstanceScope scope = findScope(element);
                family.SetScopeTo(scope);

                InstanceMemento memento = _mementoCreator.CreateMemento(element);
                family.AddDefaultMemento(memento);
            });
        }
        /// <summary>
        /// Registers a mapping from Source to Dest
        /// </summary>
        /// <typeparam name="TFrom"></typeparam>
        /// <typeparam name="TTo"></typeparam>
        public void Register(Type from, Type to, InstanceScope scope)
        {
            if (!from.IsAssignableFrom(to))
            {
                throw new ArgumentException(string.Format("{0} is not assignable from {1}", to.Name, from.Name));
            }

            if (to.IsInterface || to.IsAbstract)
            {
                throw new ArgumentException(string.Format("Cannot map {0} to {1}. You must specify a concrete type for TTo", to.Name, from.Name));
            }

            this.registrationMap.AddOrUpdate(from, new Registration(to, scope), (key, value) => new Registration(to, scope));
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Register specified type to the container as custom service with provided scope.
        /// </summary>
        /// <param name="builder">container builder</param>
        /// <param name="type">service type</param>
        /// <param name="asType">register as</param>
        /// <param name="scope">resolution scope</param>
        /// <param name="isGeneric">is generic type</param>
        public static void RegisterType(
            this IObjectFactoryBuilder builder,
            Type type,
            InstanceScope scope,
            bool isGeneric,
            params Type[] asType)
        {
            Contract.Requires(builder != null);
            Contract.Requires(type != null);

            builder.Add(new FactoryBuilderType {
                type = type, Scope = scope, AsType = asType, IsGeneric = isGeneric
            });
        }
        public void Register(Type from, Type to, InstanceScope scope)
        {
            var builder = new ContainerBuilder();

            if (scope == InstanceScope.Singleton)
            {
                builder.RegisterType(to).As(from).SingleInstance();
            }
            else
            {
                builder.RegisterType(to).As(from);
            }

            builder.Update(container);
        }
Ejemplo n.º 18
0
        public void ParseFamily(XmlElement familyElement)
        {
            TypePath typePath = TypePath.CreateFromXmlNode(familyElement);

            _builder.ConfigureFamily(typePath, family =>
            {
                family.DefaultInstanceKey =
                    familyElement.GetAttribute(DEFAULT_KEY_ATTRIBUTE);

                InstanceScope scope = findScope(familyElement);
                family.SetScopeTo(scope);

                attachMementoSource(family, familyElement);
                attachPlugins(family, familyElement);
                attachInterceptors(family, familyElement);
                attachInstances(family, familyElement, _builder);
            });
        }
Ejemplo n.º 19
0
        public void Register(Type from, Type to, InstanceScope scope)
        {
            if (!from.IsAssignableFrom(to))
            {
                throw new ArgumentException("To must be assignable to From");
            }

            if (!this.kernel.GetBindings(from).Any())
            {
                if (scope == InstanceScope.Singleton)
                {
                    this.kernel.Bind(from).To(to).InSingletonScope();
                }
                else
                {
                    this.kernel.Bind(from).To(to).InTransientScope();
                }
            }
        }
Ejemplo n.º 20
0
        public static IEnumerable <T> Cache <T>(this IEnumerable <T> self)
        {
            using (InstanceScope <List <T> > .Create(() => new List <T>(), out var items))
            {
                try
                {
                    items.AddRange(self);

                    foreach (var item in items)
                    {
                        yield return(item);
                    }
                }
                finally
                {
                    items.Clear();
                }
            }
        }
Ejemplo n.º 21
0
        public static IEnumerable <T> GetComponentsEnumerable <T>(this GameObject self)
            where T : class
        {
            using (InstanceScope <List <T> > .Create(() => new List <T>(), out var components))
            {
                try
                {
                    self.GetComponents(components);

                    foreach (var component in components)
                    {
                        yield return(component);
                    }
                }
                finally
                {
                    components.Clear();
                }
            }
        }
Ejemplo n.º 22
0
        public static IEnumerable <T> GetComponentsInParentEnumerable <T>(this GameObject self, bool includeInactive = false)
            where T : class
        {
            using (InstanceScope <List <T> > .Create(() => new List <T>(), out var components))
            {
                try
                {
                    self.GetComponentsInParent(includeInactive, components);

                    foreach (var component in components)
                    {
                        yield return(component);
                    }
                }
                finally
                {
                    components.Clear();
                }
            }
        }
Ejemplo n.º 23
0
        public void Register(Type from, Type to, InstanceScope scope)
        {
            if (!from.IsAssignableFrom(to))
            {
                throw new ArgumentException("To must be assignable to From");
            }

            try
            {
                if (scope == InstanceScope.Singleton)
                {
                    this.container.Register(Component.For(from).ImplementedBy(to).LifestyleSingleton());
                }
                else
                {
                    this.container.Register(Component.For(from).ImplementedBy(to).LifestyleTransient());
                }
            }
            catch (ComponentRegistrationException)
            {
                // Castle Windsor sucks and doesn't allow a easy way to determine if a registration already exists
            }
        }
Ejemplo n.º 24
0
        public static IEnumerable <GameObject> GetRootGameObjectsEnumerable(this Scene self)
        {
            if (self.rootCount < 1)
            {
                yield break;
            }

            using (InstanceScope <List <GameObject> > .Create(() => new List <GameObject>(), out var gameObjects))
            {
                try
                {
                    self.GetRootGameObjects(gameObjects);

                    foreach (var gameObject in gameObjects)
                    {
                        yield return(gameObject);
                    }
                }
                finally
                {
                    gameObjects.Clear();
                }
            }
        }
Ejemplo n.º 25
0
 public void Register <TFrom, TTo>(InstanceScope scope)
     where TTo : TFrom
 {
     this.Register(typeof(TFrom), typeof(TTo), scope);
 }
Ejemplo n.º 26
0
 /// <summary>
 /// Register service using specified scope
 /// </summary>
 /// <param name="scope">instance scope</param>
 public ServiceAttribute(InstanceScope scope)
 {
     this.Scope = scope;
 }
Ejemplo n.º 27
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="lifecycle"></param>
 /// <returns></returns>
 public GenericFamilyExpression LifecycleIs(InstanceScope lifecycle)
 {
     return(alterAndContinue(family => family.SetScopeTo(lifecycle)));
 }
Ejemplo n.º 28
0
 public GenericFamilyExpression CacheBy(InstanceScope scope)
 {
     return(alterAndContinue(family => family.SetScopeTo(scope)));
 }
Ejemplo n.º 29
0
 /// <summary>
 /// Register plugins using specified scope
 /// </summary>
 /// <param name="scope">instance scope</param>
 public PluginAttribute(InstanceScope scope)
 {
     this.Scope = scope;
 }
Ejemplo n.º 30
0
        /// <summary>
        /// Register factory to the container builder with specified scope. Default scope is Transient.
        /// Service will be resolved from the factory using context scope.
        /// </summary>
        /// <typeparam name="T">implementation type</typeparam>
        /// <typeparam name="TAs1">as service</typeparam>
        /// <typeparam name="TAs2">as alternative service</typeparam>
        /// <param name="builder">container builder</param>
        /// <param name="func">factory to service</param>
        /// <param name="scope">factory scope</param>
        public static void RegisterFunc <T, TAs1, TAs2>(this IObjectFactoryBuilder builder, Func <IObjectFactory, T> func, InstanceScope scope = InstanceScope.Transient)
            where T : TAs1, TAs2
        {
            Contract.Requires(builder != null);
            Contract.Requires(func != null);

            builder.Add(new FactoryBuilderFunc {
                func = f => func(f), AsType = new[] { typeof(TAs1), typeof(TAs2) }, Scope = scope
            });
        }
Ejemplo n.º 31
0
        /// <summary>
        /// Register multiple types to the container with specified scope.
        /// </summary>
        /// <param name="factory">current scope</param>
        /// <param name="types">services</param>
        /// <param name="scope">resolution scope</param>
        public static void RegisterTypes(this IObjectFactory factory, IEnumerable <Type> types, InstanceScope scope)
        {
            Contract.Requires(factory != null);
            Contract.Requires(types != null);

            var ofb = new ObjectFactoryBuilder();

            foreach (var t in types)
            {
                ofb.Add(new FactoryBuilderType {
                    type = t, Scope = scope
                });
            }
            factory.Register(ofb);
        }
 public DefaultConventionScannerWithScope(InstanceScope instanceScope)
 {
     _instanceScope = instanceScope;
 }
Ejemplo n.º 33
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="lifecycle"></param>
 /// <returns></returns>
 public GenericFamilyExpression LifecycleIs(InstanceScope lifecycle)
 {
     return alterAndContinue(family => family.SetScopeTo(lifecycle));
 }
 public GenericFamilyExpression CacheBy(InstanceScope scope)
 {
     return alterAndContinue(family => family.Lifecycle = Lifecycles.GetLifecycle(scope));
 }
Ejemplo n.º 35
0
 /// <summary>
 /// Register plugins using specified scope
 /// </summary>
 /// <param name="scope">instance scope</param>
 public PluginAttribute(InstanceScope scope)
 {
     this.Scope = scope;
 }
        private void assertScopeLeadsToInterceptor(InstanceScope scope, Type interceptorType)
        {
            var att = new PluginFamilyAttribute("something");
            att.Scope = scope;

            var family = new PluginFamily(typeof (TypeThatDoesNotHaveCustomMementoSource));
            att.Configure(family);

            Assert.IsInstanceOfType(interceptorType, family.Lifecycle);
        }
Ejemplo n.º 37
0
 public GenericFamilyExpression CacheBy(InstanceScope scope)
 {
     return alterAndContinue(family => family.SetScopeTo(scope));
 }
Ejemplo n.º 38
0
        /// <summary>
        /// Register factory to the container with specified scope.
        /// Service will be resolved from the factory using context scope.
        /// </summary>
        /// <typeparam name="T">service type</typeparam>
        /// <param name="factory">current scope</param>
        /// <param name="func">factory to service</param>
        /// <param name="scope">factory scope</param>
        public static void RegisterFunc <T>(this IObjectFactory factory, Func <IObjectFactory, T> func, InstanceScope scope)
        {
            Contract.Requires(factory != null);
            Contract.Requires(func != null);

            var ofb = new ObjectFactoryBuilder();

            ofb.Add(new FactoryBuilderFunc {
                func = f => func(f), AsType = new[] { typeof(T) }, Scope = scope
            });
            factory.Register(ofb);
        }
 public static void SetScopeTo(this PluginFamily family, InstanceScope scope)
 {
     family.Lifecycle = Lifecycles.GetLifecycle(scope);
 }
Ejemplo n.º 40
0
 /// <summary>
 /// Register service using specified scope
 /// </summary>
 /// <param name="scope">instance scope</param>
 public ServiceAttribute(InstanceScope scope)
 {
     this.Scope = scope;
 }