Beispiel #1
0
        /// <summary>
        /// Instantiates the service type using the given delegate.
        /// </summary>
        /// <param name="request">The <see cref="IFactoryRequest"/> that describes the service that needs to be created.</param>
        /// <returns>The service instance.</returns>
        public object CreateInstance(IFactoryRequest request)
        {
            object result = null;

            try
            {
                object     target         = _targetDelegate.Target;
                MethodInfo method         = _targetDelegate.Method;
                int        argCount       = request.Arguments.Length;
                int        methodArgCount = method.GetParameters().Count();

                if (argCount != methodArgCount)
                {
                    throw new ArgumentException("Parameter Count Mismatch");
                }

                result = _targetDelegate.DynamicInvoke(request.Arguments);
            }
            catch (TargetInvocationException ex)
            {
                // Unroll the exception
                throw ex.InnerException;
            }

            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// A method that creates a service instance as a singleton.
        /// </summary>
        /// <param name="request">The <see cref="IFactoryRequest"/> instance that describes the requested service.</param>
        /// <returns>A service instance as a singleton.</returns>
        public override T CreateInstance(IFactoryRequest request)
        {
            var key = new { request.ServiceName, request.ServiceType, request.Container };

            if (_instances.ContainsKey(key))
            {
                return(_instances[key]);
            }

            lock (_lock)
            {
                if (_instances.ContainsKey(key))
                {
                    return(_instances[key]);
                }

                T result = _createInstance(request);
                if (result != null)
                {
                    _instances[key] = result;
                }
            }

            return(_instances[key]);
        }
Beispiel #3
0
        /// <summary>
        /// Generates the <see cref="Action{T}"/> delegate that will emit
        /// the necessary <see cref="IInvocationInfo"/> information.
        /// </summary>
        /// <param name="request">The <see cref="IFactoryRequest"/> instance that describes the requested service type.</param>
        /// <returns>A delegate that can emit the necessary <see cref="IInvocationInfo"/> context that will allow other developers to infer information about the method currently being executed.</returns>
        public object CreateInstance(IFactoryRequest request)
        {
            var container = request.Container;
            Action <MethodDefinition> result =
                method =>
            {
                var body = method.Body;

                // Add the IInvocationInfo
                // instance only once
                var localAlreadyExists = (from VariableDefinition local in body.Variables
                                          where local.Name == "___invocationInfo___"
                                          select local).Count() > 0;

                if (localAlreadyExists)
                {
                    return;
                }

                var variable = method.AddLocal <IInvocationInfo>();
                variable.Name = "___invocationInfo___";

                var emitInfo = (IEmitInvocationInfo)container.GetService(typeof(IEmitInvocationInfo));
                emitInfo.Emit(method, method, variable);
            };

            return(result);
        }
        /// <summary>
        /// Generates the <see cref="Action{T1, T2}"/> instance that will
        /// weave the target type.
        /// </summary>
        /// <param name="request">The <see cref="IFactoryRequest"/> that describes the service request.</param>
        /// <returns>The <see cref="Action{T1, T2}"/> instance that will weave the target type.</returns>
        public object CreateInstance(IFactoryRequest request)
        {
            var container = request.Container;
            Action<string, TypeDefinition> result =
                (weaverName, type) =>
                {
                    // Get the method weaver instance that matches the weaverName
                    var methodWeaver = (IHostWeaver<TypeDefinition>)container.GetService(weaverName, typeof(IHostWeaver<TypeDefinition>));

                    // Wrap it in a type weaver
                    var typeWeaver = (ITypeWeaver)container.GetService("AutoMethodWeaver", typeof(ITypeWeaver), methodWeaver);

                    var module = type.Module;
                    if (!typeWeaver.ShouldWeave(type))
                        return;

                    // Modify the host module
                    typeWeaver.ImportReferences(module);
                    typeWeaver.AddAdditionalMembers(module);

                    // Weave the type itself
                    typeWeaver.Weave(type);
                };

            return result;
        }
Beispiel #5
0
        /// <summary>
        /// Generates the <see cref="Action{T1, T2}"/> instance that will
        /// weave the target type.
        /// </summary>
        /// <param name="request">The <see cref="IFactoryRequest"/> that describes the service request.</param>
        /// <returns>The <see cref="Action{T1, T2}"/> instance that will weave the target type.</returns>
        public object CreateInstance(IFactoryRequest request)
        {
            IServiceContainer container            = request.Container;
            Action <string, TypeDefinition> result =
                (weaverName, type) =>
            {
                // Get the method weaver instance that matches the weaverName
                var methodWeaver =
                    (IHostWeaver <TypeDefinition>)
                    container.GetService(weaverName, typeof(IHostWeaver <TypeDefinition>));

                // Wrap it in a type weaver
                var typeWeaver =
                    (ITypeWeaver)container.GetService("AutoMethodWeaver", typeof(ITypeWeaver), methodWeaver);

                ModuleDefinition module = type.Module;
                if (!typeWeaver.ShouldWeave(type))
                {
                    return;
                }

                // Modify the host module
                typeWeaver.ImportReferences(module);
                typeWeaver.AddAdditionalMembers(module);

                // Weave the type itself
                typeWeaver.Weave(type);
            };


            return(result);
        }
        /// <summary>
        /// Generates the <see cref="Action{T}"/> delegate that will emit
        /// the necessary <see cref="IInvocationInfo"/> information.
        /// </summary>
        /// <param name="request">The <see cref="IFactoryRequest"/> instance that describes the requested service type.</param>
        /// <returns>A delegate that can emit the necessary <see cref="IInvocationInfo"/> context that will allow other developers to infer information about the method currently being executed.</returns>
        public object CreateInstance(IFactoryRequest request)
        {
            IServiceContainer container = request.Container;
            Action<MethodDefinition> result =
                method =>
                    {
                        MethodBody body = method.Body;

                        // Add the IInvocationInfo
                        // instance only once
                        bool localAlreadyExists = (from VariableDefinition local in body.Variables
                                                   where local.Name == "___invocationInfo___"
                                                   select local).Count() > 0;

                        if (localAlreadyExists)
                            return;

                        VariableDefinition variable = method.AddLocal<IInvocationInfo>();
                        variable.Name = "___invocationInfo___";

                        var emitInfo = (IEmitInvocationInfo) container.GetService(typeof (IEmitInvocationInfo));
                        emitInfo.Emit(method, method, variable);
                    };

            return result;
        }
Beispiel #7
0
        /// <summary>
        /// Instantiates the service type using the given delegate.
        /// </summary>
        /// <param name="request">The <see cref="IFactoryRequest"/> that describes the service that needs to be created.</param>
        /// <returns>The service instance.</returns>
        public object CreateInstance(IFactoryRequest request)
        {
            object result = null;

            try
            {
                var target         = _targetDelegate.Target;
                var method         = _targetDelegate.Method;
                var argCount       = request.Arguments.Length;
                var methodArgCount = method.GetParameters().Count();

                if (argCount != methodArgCount)
                {
                    Console.WriteLine("Parameter Count Mismatch");
                }
                result = _targetDelegate.DynamicInvoke(request.Arguments);
                //result = method.Invoke(target, request.Arguments);
            }
            catch (TargetInvocationException ex)
            {
                // Unroll the exception
                throw ex.InnerException;
            }

            return(result);
        }
        public object CreateInstance(IFactoryRequest request)
        {
            Type serviceType  = request.ServiceType;
            Type typeArgument = serviceType.GetGenericArguments()[0];
            Type resultType   = typeof(SampleGenericImplementation <>).MakeGenericType(typeArgument);

            return(Activator.CreateInstance(resultType));
        }
Beispiel #9
0
        /// <summary>
        /// Instantiates the actual factory instance and uses it to instantiate the target service type.
        /// </summary>
        /// <param name="request">The <see cref="IFactoryRequest"/> that describes the service to be created.</param>
        /// <returns>A valid service instance.</returns>
        public object CreateInstance(IFactoryRequest request)
        {
            // Create the factory if necessary
            if (_realFactory == null)
                _realFactory = _getFactory(request);

            return _realFactory == null ? null : _realFactory.CreateInstance(request);
        }
        public object CreateInstance(IFactoryRequest request)
        {
            Type serviceType = request.ServiceType;
            Type typeArgument = serviceType.GetGenericArguments()[0];
            Type resultType = typeof (SampleGenericImplementation<>).MakeGenericType(typeArgument);

            return Activator.CreateInstance(resultType);
        }
Beispiel #11
0
        /// <summary>
        /// Creates a service instance using the given <paramref name="factoryRequest"/> and <see cref="IFactory"/> instance.
        /// </summary>
        /// <param name="factoryRequest">The <see cref="IFactoryRequest"/> instance that describes the context of the service request.</param>
        /// <param name="factory">The <see cref="IFactory"/> instance that will be used to instantiate the service type.</param>
        /// <returns>A valid service instance.</returns>
        public virtual object CreateFrom(IFactoryRequest factoryRequest, IFactory factory)
        {
            object instance = null;

            // Generate the service instance
            if (factory != null)
                instance = factory.CreateInstance(factoryRequest);

            return instance;
        }
Beispiel #12
0
        /// <summary>
        /// Instantiates the actual factory instance and uses it to instantiate the target service type.
        /// </summary>
        /// <param name="request">The <see cref="IFactoryRequest"/> that describes the service to be created.</param>
        /// <returns>A valid service instance.</returns>
        public object CreateInstance(IFactoryRequest request)
        {
            // Create the factory if necessary
            if (_realFactory == null)
            {
                _realFactory = _getFactory(request);
            }

            return(_realFactory == null ? null : _realFactory.CreateInstance(request));
        }
        public object CreateInstance(IFactoryRequest request)
        {
            if (string.IsNullOrEmpty(request.ServiceName))
                throw new ArgumentNullException("ServiceName");

            var myClass = new MyClass<string>();
            myClass.Value = request.ServiceName;

            return myClass;
        }
Beispiel #14
0
        T IFactory <T> .CreateInstance(IFactoryRequest request)
        {
            var factory = _factory as IFactory <T>;

            if (factory == null)
            {
                return(default(T));
            }

            return(factory.CreateInstance(request));
        }
Beispiel #15
0
        /// <summary>
        ///     Creates a service instance using the given <paramref name="factoryRequest" /> and <see cref="IFactory" /> instance.
        /// </summary>
        /// <param name="factoryRequest">
        ///     The <see cref="IFactoryRequest" /> instance that describes the context of the service
        ///     request.
        /// </param>
        /// <param name="factory">The <see cref="IFactory" /> instance that will be used to instantiate the service type.</param>
        /// <returns>A valid service instance.</returns>
        public virtual object CreateFrom(IFactoryRequest factoryRequest, IFactory factory)
        {
            object instance = null;

            // Generate the service instance
            if (factory != null)
            {
                instance = factory.CreateInstance(factoryRequest);
            }

            return(instance);
        }
Beispiel #16
0
        public object CreateInstance(IFactoryRequest request)
        {
            if (string.IsNullOrEmpty(request.ServiceName))
            {
                throw new ArgumentNullException("ServiceName");
            }

            var myClass = new MyClass <string>();

            myClass.Value = request.ServiceName;

            return(myClass);
        }
Beispiel #17
0
        /// <summary>
        /// Overridden. Uses the strongly-typed factory
        /// to create the service instance every time
        /// the <see cref="IFactory.CreateInstance"/> method
        /// is called.
        /// </summary>
        /// <param name="request">The <see cref="IFactoryRequest"/> instance that describes the requested service.</param>
        /// <returns>An object instance that represents the service to be created. This cannot be <c>null</c>.</returns>
        public object CreateInstance(IFactoryRequest request)
        {
            if (_factory == null)
            {
                return(default(T));
            }

            var factory = _factory as IFactory <T>;

            if (factory == null)
            {
                return(default(T));
            }

            return(factory.CreateInstance(request));
        }
Beispiel #18
0
        /// <summary>
        ///     Instantiates the service type using the actual factory.
        /// </summary>
        /// <param name="request">The <see cref="IFactoryRequest" /> instance that describes the service to be created.</param>
        /// <returns></returns>
        public T CreateInstance(IFactoryRequest request)
        {
            if (_getFactory == null)
            {
                throw new NotImplementedException();
            }

            var factory = _getFactory(request) as IFactory <T>;

            if (factory == null)
            {
                return(default(T));
            }

            return(factory.CreateInstance(request));
        }
        /// <summary>
        /// Generates <see cref="Action{T1, T2}"/> instances
        /// that apply a specific method weaver (with the name given in the first delegate parameter)
        /// to every type in every module of an <see cref="AssemblyDefinition"/> instance.
        /// </summary>
        /// <param name="request">The <see cref="IFactoryRequest"/> that describes the service request.</param>
        /// <returns>An action delegate that will apply a specific method weaver to all the types in the given assembly.</returns>
        public object CreateInstance(IFactoryRequest request)
        {
            var container = request.Container;
            Action<string, AssemblyDefinition> result =
                (weaverName, assembly) =>
                {
                    // Create the lambda that can modify the target types
                    var weaveWith = (Action<string, TypeDefinition>)container.GetService("TypeWeaver", typeof(Action<string, TypeDefinition>));
                    var mainModule = assembly.MainModule;

                    foreach (TypeDefinition type in mainModule.Types)
                    {
                        // Use the method weaver on the target type
                        weaveWith(weaverName, type);
                    }
                };

            return result;
        }
        /// <summary>
        /// Generates <see cref="Action{T1, T2}"/> instances
        /// that apply a specific method weaver (with the name given in the first delegate parameter)
        /// to every type in every module of an <see cref="AssemblyDefinition"/> instance.
        /// </summary>
        /// <param name="request">The <see cref="IFactoryRequest"/> that describes the service request.</param>
        /// <returns>An action delegate that will apply a specific method weaver to all the types in the given assembly.</returns>
        public object CreateInstance(IFactoryRequest request)
        {
            var container = request.Container;
            Action <string, AssemblyDefinition> result =
                (weaverName, assembly) =>
            {
                // Create the lambda that can modify the target types
                var weaveWith  = (Action <string, TypeDefinition>)container.GetService("TypeWeaver", typeof(Action <string, TypeDefinition>));
                var mainModule = assembly.MainModule;

                foreach (TypeDefinition type in mainModule.Types)
                {
                    // Use the method weaver on the target type
                    weaveWith(weaverName, type);
                }
            };

            return(result);
        }
        /// <summary>
        /// Creates the service instance using the given <see cref="IFactoryRequest"/>
        /// instance. Every service instance created from this factory will
        /// only be created once per thread.
        /// </summary>
        /// <param name="request">The <see cref="IFactoryRequest"/> instance that describes the requested service.</param>
        /// <returns>A a service instance as thread-wide singleton.</returns>
        public override T CreateInstance(IFactoryRequest request)
        {
            var threadId = Thread.CurrentThread.ManagedThreadId;

            var result = default(T);

            lock (_storage)
            {
                // Create the service instance only once
                if (!_storage.ContainsKey(threadId))
                {
                    _storage[threadId] = _createInstance(request);
                }

                result = _storage[threadId];
            }

            return(result);
        }
Beispiel #22
0
        /// <summary>
        /// Instantiates the service type using the given delegate.
        /// </summary>
        /// <param name="request">The <see cref="IFactoryRequest"/> that describes the service that needs to be created.</param>
        /// <returns>The service instance.</returns>
        public object CreateInstance(IFactoryRequest request)
        {
            object result = null;
            try
            {
                var target = _targetDelegate.Target;
                var method = _targetDelegate.Method;
                var argCount = request.Arguments.Length;
                var methodArgCount = method.GetParameters().Count();

                if (argCount != methodArgCount)
                    Console.WriteLine("Parameter Count Mismatch");
                result = _targetDelegate.DynamicInvoke(request.Arguments);
                //result = method.Invoke(target, request.Arguments);
            }
            catch (TargetInvocationException ex)
            {
                // Unroll the exception
                throw ex.InnerException;
            }

            return result;
        }
        /// <summary>
        /// Instantiates the service type using the given delegate.
        /// </summary>
        /// <param name="request">The <see cref="IFactoryRequest"/> that describes the service that needs to be created.</param>
        /// <returns>The service instance.</returns>
        public object CreateInstance(IFactoryRequest request)
        {
            object result = null;
            try
            {
                object target = _targetDelegate.Target;
                MethodInfo method = _targetDelegate.Method;
                int argCount = request.Arguments.Length;
                int methodArgCount = method.GetParameters().Count();

                if (argCount != methodArgCount)
                    throw new ArgumentException("Parameter Count Mismatch");

                result = _targetDelegate.DynamicInvoke(request.Arguments);
            }
            catch (TargetInvocationException ex)
            {
                // Unroll the exception
                throw ex.InnerException;
            }

            return result;
        }
Beispiel #24
0
 public object CreateInstance(IFactoryRequest request)
 {
     return(new SampleClass());
 }
Beispiel #25
0
 T IFactory <T> .CreateInstance(IFactoryRequest request)
 {
     return(_factoryMethod(request));
 }
 public object CreateInstance(IFactoryRequest request)
 {
     return "42";
 }
 /// <summary>
 /// Instantiates an object reference using the given factory method.
 /// </summary>
 /// <param name="request">The <see cref="IFactoryRequest"/> instance that describes the requested service.</param>
 /// <returns>A non-null object reference that represents the service type.</returns>
 public object CreateInstance(IFactoryRequest request)
 {
     return _factoryMethod(request);
 }
Beispiel #28
0
 public object CreateInstance(IFactoryRequest request)
 {
     return new SampleClass();
 }
Beispiel #29
0
 public object CreateInstance(IFactoryRequest request)
 {
     return("42");
 }
Beispiel #30
0
 /// <summary>
 /// This method creates a new service instance every time
 /// it is invoked.
 /// </summary>
 /// <param name="request">The <see cref="IFactoryRequest"/> instance that describes the requested service.</param>
 /// <returns>A non-null object reference.</returns>
 public override T CreateInstance(IFactoryRequest request)
 {
     return(_createInstance(request));
 }
Beispiel #31
0
 /// <summary>
 /// Instantiates an object reference using the given factory method.
 /// </summary>
 /// <param name="request">The <see cref="IFactoryRequest"/> instance that describes the requested service.</param>
 /// <returns>A non-null object reference that represents the service type.</returns>
 public object CreateInstance(IFactoryRequest request)
 {
     return(_factoryMethod(request));
 }
Beispiel #32
0
 /// <summary>
 /// Returns the object instance that given when the <see cref="CreatorFromInstance"/> class instance was initialized.
 /// </summary>
 /// <param name="factoryRequest">The <see cref="IFactoryRequest"/> instance that describes the context of the service request.</param>
 /// <param name="factory">The <see cref="IFactory"/> instance that will be used to instantiate the service type.</param>
 /// <returns>A valid service instance.</returns>
 public object CreateFrom(IFactoryRequest factoryRequest, IFactory factory)
 {
     return(_instance);
 }
Beispiel #33
0
        object IFactory.CreateInstance(IFactoryRequest request)
        {
            IFactory <T> thisFactory = this;

            return(thisFactory.CreateInstance(request));
        }
Beispiel #34
0
 /// <summary>
 /// Creates a service instance using the given container.
 /// </summary>
 /// <param name="request">The <see cref="IFactoryRequest"/> instance that describes the requested service.</param>
 /// <returns>An object instance that represents the service to be created. This cannot be <c>null</c>.</returns>
 object IFactory.CreateInstance(IFactoryRequest request)
 {
     return(CreateInstance(request));
 }
Beispiel #35
0
 public ISampleService CreateInstance(IFactoryRequest request)
 {
     return(new SampleClass());
 }
Beispiel #36
0
 /// <summary>
 /// Returns the object instance that given when the <see cref="CreatorFromInstance"/> class instance was initialized.
 /// </summary>
 /// <param name="factoryRequest">The <see cref="IFactoryRequest"/> instance that describes the context of the service request.</param>
 /// <param name="factory">The <see cref="IFactory"/> instance that will be used to instantiate the service type.</param>
 /// <returns>A valid service instance.</returns>
 public object CreateFrom(IFactoryRequest factoryRequest, IFactory factory)
 {
     return _instance;
 }
Beispiel #37
0
 /// <summary>
 /// Creates a service instance using the given container.
 /// </summary>
 /// <remarks>
 /// <see cref="IFactory"/> developers can inherit from this class
 /// instead of having to write their own custom factories
 /// from scratch. This should cut down on some of the boilerplate
 /// code necessary to get a factory class up and running.
 /// </remarks>
 /// <param name="request">The <see cref="IFactoryRequest"/> instance that describes the requested service.</param>
 /// <returns>An object instance that represents the service to be created. This cannot be <c>null</c>.</returns>
 public abstract T CreateInstance(IFactoryRequest request);