/// <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); }
/// <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]); }
/// <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; }
/// <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; }
/// <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)); }
/// <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); }
/// <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; }
/// <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; }
T IFactory <T> .CreateInstance(IFactoryRequest request) { var factory = _factory as IFactory <T>; if (factory == null) { return(default(T)); } return(factory.CreateInstance(request)); }
/// <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); }
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); }
/// <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)); }
/// <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); }
/// <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; }
public object CreateInstance(IFactoryRequest request) { return(new SampleClass()); }
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); }
public object CreateInstance(IFactoryRequest request) { return new SampleClass(); }
public object CreateInstance(IFactoryRequest request) { return("42"); }
/// <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)); }
/// <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)); }
/// <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); }
object IFactory.CreateInstance(IFactoryRequest request) { IFactory <T> thisFactory = this; return(thisFactory.CreateInstance(request)); }
/// <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)); }
public ISampleService CreateInstance(IFactoryRequest request) { return(new SampleClass()); }
/// <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; }
/// <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);