示例#1
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);
        }
示例#2
0
        /// <summary>
        /// Register instance to the container scope.
        /// Registered instance will be available in nested scopes too.
        /// </summary>
        /// <typeparam name="T">service</typeparam>
        /// <param name="factory">current scope</param>
        /// <param name="instance">provided instance</param>
        public static void RegisterInstance <T>(this IObjectFactory factory, T instance)
        {
            Contract.Requires(factory != null);
            Contract.Requires(instance != null);

            var ofb = new ObjectFactoryBuilder();

            ofb.Add(new FactoryBuilderInstance {
                instance = instance, AsType = typeof(T)
            });
            factory.Register(ofb);
        }
示例#3
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);
        }
示例#4
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);
        }
示例#5
0
        /// <summary>
        /// Register all interfaces and specified service for provided instance to the container.
        /// Services will be available in nested scopes too.
        /// </summary>
        /// <typeparam name="T">service type</typeparam>
        /// <param name="factory">current scope</param>
        /// <param name="instance">provided instance</param>
        public static void RegisterInterfaces <T>(this IObjectFactory factory, T instance)
        {
            Contract.Requires(factory != null);
            Contract.Requires(instance != null);

            var ofb        = new ObjectFactoryBuilder();
            var interfaces = instance.GetType().GetInterfaces();

            foreach (var i in interfaces)
            {
                ofb.Add(new FactoryBuilderInstance {
                    instance = instance, AsType = i
                });
            }
            if (typeof(T).IsInterface)
            {
                ofb.Add(new FactoryBuilderInstance {
                    instance = instance, AsType = typeof(T)
                });
            }
            factory.Register(ofb);
        }