コード例 #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");
        }
コード例 #2
0
ファイル: Lifecycles.cs プロジェクト: hasaki/ragetanks
        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);
        }
コード例 #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");
     }
 }
コード例 #5
0
ファイル: FamilyParser.cs プロジェクト: hasaki/ragetanks
        private static InstanceScope findScope(XmlElement familyElement)
        {
            InstanceScope returnValue = InstanceScope.PerRequest;

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

            return(returnValue);
        }
コード例 #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
            });
        }
コード例 #7
0
ファイル: PluginFamily.cs プロジェクト: stgwilli/structuremap
        public void SetScopeTo(InstanceScope scope)
        {
            if (scope == InstanceScope.Transient)
            {
                _lifecycle = null;
                return;
            }

            _lifecycle = Lifecycles.GetLifecycle(scope);
        }
コード例 #8
0
    public void SetScopeTo(InstanceScope scope)
    {
        if (scope == InstanceScope.Transient)
            {
                _lifecycle = null;
                return;
            }

            _lifecycle = Lifecycles.GetLifecycle(scope);
    }
コード例 #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);
     }
 }
コード例 #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
            });
        }
コード例 #11
0
        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);
        }
コード例 #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;
 }
コード例 #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);
        }
コード例 #14
0
ファイル: FamilyParser.cs プロジェクト: hasaki/ragetanks
        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);
            });
        }
コード例 #15
0
        /// <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));
        }
コード例 #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
            });
        }
コード例 #17
0
        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);
        }
コード例 #18
0
ファイル: FamilyParser.cs プロジェクト: hasaki/ragetanks
        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);
            });
        }
コード例 #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();
                }
            }
        }
コード例 #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();
                }
            }
        }
コード例 #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();
                }
            }
        }
コード例 #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();
                }
            }
        }
コード例 #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
            }
        }
コード例 #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();
                }
            }
        }
コード例 #25
0
 public void Register <TFrom, TTo>(InstanceScope scope)
     where TTo : TFrom
 {
     this.Register(typeof(TFrom), typeof(TTo), scope);
 }
コード例 #26
0
ファイル: ServiceAttribute.cs プロジェクト: zhxjdwh/revenj
 /// <summary>
 /// Register service using specified scope
 /// </summary>
 /// <param name="scope">instance scope</param>
 public ServiceAttribute(InstanceScope scope)
 {
     this.Scope = scope;
 }
コード例 #27
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="lifecycle"></param>
 /// <returns></returns>
 public GenericFamilyExpression LifecycleIs(InstanceScope lifecycle)
 {
     return(alterAndContinue(family => family.SetScopeTo(lifecycle)));
 }
コード例 #28
0
 public GenericFamilyExpression CacheBy(InstanceScope scope)
 {
     return(alterAndContinue(family => family.SetScopeTo(scope)));
 }
コード例 #29
0
ファイル: PluginAttribute.cs プロジェクト: zhxjdwh/revenj
 /// <summary>
 /// Register plugins using specified scope
 /// </summary>
 /// <param name="scope">instance scope</param>
 public PluginAttribute(InstanceScope scope)
 {
     this.Scope = scope;
 }
コード例 #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
            });
        }
コード例 #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);
        }
コード例 #32
0
 public DefaultConventionScannerWithScope(InstanceScope instanceScope)
 {
     _instanceScope = instanceScope;
 }
コード例 #33
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="lifecycle"></param>
 /// <returns></returns>
 public GenericFamilyExpression LifecycleIs(InstanceScope lifecycle)
 {
     return alterAndContinue(family => family.SetScopeTo(lifecycle));
 }
コード例 #34
0
 public GenericFamilyExpression CacheBy(InstanceScope scope)
 {
     return alterAndContinue(family => family.Lifecycle = Lifecycles.GetLifecycle(scope));
 }
コード例 #35
0
ファイル: PluginAttribute.cs プロジェクト: nutrija/revenj
 /// <summary>
 /// Register plugins using specified scope
 /// </summary>
 /// <param name="scope">instance scope</param>
 public PluginAttribute(InstanceScope scope)
 {
     this.Scope = scope;
 }
コード例 #36
0
        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);
        }
コード例 #37
0
 public GenericFamilyExpression CacheBy(InstanceScope scope)
 {
     return alterAndContinue(family => family.SetScopeTo(scope));
 }
コード例 #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);
        }
コード例 #39
0
 public static void SetScopeTo(this PluginFamily family, InstanceScope scope)
 {
     family.Lifecycle = Lifecycles.GetLifecycle(scope);
 }
コード例 #40
0
ファイル: ServiceAttribute.cs プロジェクト: nutrija/revenj
 /// <summary>
 /// Register service using specified scope
 /// </summary>
 /// <param name="scope">instance scope</param>
 public ServiceAttribute(InstanceScope scope)
 {
     this.Scope = scope;
 }