private IEnumerable <Parameter> GetResolvedParameters(IComponentContext context, ConstructorInfo targetConstructor, IEnumerable <Parameter> prioritisedParameters) { var constructorParameters = targetConstructor.GetParameters(); var resolvedParameters = new Parameter[constructorParameters.Length]; for (int i = 0; i < constructorParameters.Length; ++i) { var pi = constructorParameters[i]; foreach (var param in prioritisedParameters) { Func <object> valueRetriever; if (param.CanSupplyValue(pi, _componentContext, out valueRetriever)) { var value = valueRetriever(); if (value.GetType().IsDefined(typeof(DynamicallyAttribute), false)) { resolvedParameters[i] = new PositionalParameter(i, GetService(pi.ParameterType)); } else { resolvedParameters[i] = param; } break; } } } return(resolvedParameters); }
/// <summary> /// Called indirectly from the EnableDynamicProxy extension method. /// Modifies a registration to support dynamic interception if needed, and act as a normal type otherwise. /// </summary> public void EnableDynamicProxy <TLimit, TConcreteReflectionActivatorData, TRegistrationStyle>( IRegistrationBuilder <TLimit, TConcreteReflectionActivatorData, TRegistrationStyle> registrationBuilder) where TConcreteReflectionActivatorData : ConcreteReflectionActivatorData { // associate this context. used later by static DynamicProxyContext.From() method. registrationBuilder.WithMetadata(ProxyContextKey, this); // put a shim in place. this will return constructors for the proxy class if it interceptors have been added. registrationBuilder.ActivatorData.ConstructorFinder = new ConstructorFinderWrapper( registrationBuilder.ActivatorData.ConstructorFinder, this); // add a proxy in advance,because ConstructorFinder event in autofac 4.0 later is fire before the autofac module AttachToComponentRegistration event. // but is not in autofac 4.0 before. // may be cause performance problems. AddProxy(registrationBuilder.ActivatorData.ImplementationType); // when component is being resolved, this even handler will place the array of appropriate interceptors as the first argument registrationBuilder.OnPreparing(e => { object value; if (e.Component.Metadata.TryGetValue(InterceptorServicesKey, out value)) { var interceptorServices = (IEnumerable <Service>)value; var interceptors = interceptorServices.Select(service => e.Context.ResolveService(service)).Cast <IInterceptor>().ToArray(); var parameter = new PositionalParameter(0, interceptors); e.Parameters = new[] { parameter }.Concat(e.Parameters).ToArray(); } }); }
public static Response FromJson(string cmdJson) { try { var clientCmd = ClientCommand.FromJson(cmdJson); var guid = new PositionalParameter(0, clientCmd.Guid); var name = new PositionalParameter(1, clientCmd.Name); var cmd = new PositionalParameter(2, clientCmd.Args); switch (clientCmd.Type) { case CommandType.eRunProc: return(ContainerOfModulesBase.Ioc.Resolve <RemoteRunProcCmd>(guid, name, cmd).Execute()); case CommandType.eStopProc: return(ContainerOfModulesBase.Ioc.Resolve <RemoteStopProcCmd>(guid, name, cmd).Execute()); case CommandType.eRunDbus: return(ContainerOfModulesBase.Ioc.Resolve <RemoteRunTmdsDbusCmd>(guid, name, cmd).Execute()); case CommandType.eStopDbus: return(ContainerOfModulesBase.Ioc.Resolve <RemoteStopTmdsDbusCmd>(guid, name, cmd).Execute()); } } catch (Exception e) { return(new CommandResponse(Guid.Empty, CommandType.eUndef, StatCodes.BAD_REQUEST, e.Message)); } return(new CommandResponse(Guid.Empty, CommandType.eUndef, StatCodes.NO_CONTENT, "Undefined command type")); }
/// <summary> /// Asserts the current service type has been registered with the specified constructor parameter. /// </summary> /// <param name="param">The parameter</param> public RegistrationAssertions WithParameter(PositionalParameter param) { var p = _parameters .OfType <PositionalParameter>() .FirstOrDefault(pp => pp.Position == param.Position); p.Should().NotBeNull($"Parameter should have been registered at position '{param.Position}'."); p?.Value.Should().BeEquivalentTo(param.Value, $"Parameter at position '{param.Position}' should have been registered with value '{param.Value}'."); return(this); }
public void RegisterGeneric(Type service, Type implementer, LifeStyle life = LifeStyle.Singleton, params string[] parameter) { var builder = new ContainerBuilder(); var registrationBuilder = builder.RegisterGeneric(implementer).As(service); if (life == LifeStyle.Singleton) { registrationBuilder.SingleInstance(); } if (parameter.Length > 0) { for (int i = 0; i < parameter.Length; i++) { var param = new PositionalParameter(i, parameter[i]); registrationBuilder.WithParameter(param); } } builder.Update(_container); }
public void ApplyToProductGetSuccess(ISellerApplication application, Type sutServiceType) { // arrange var givenApplicationResultId = 1; var selectInvoiceService = StartUp.Scope.Resolve <ISelectInvoiceService>(); var confidentialInvoiceService = StartUp.Scope.Resolve <IConfidentialInvoiceService>(); var businessLoansService = StartUp.Scope.Resolve <IBusinessLoansService>(); var routeException = new Exception("Product routed to wrong service"); var givenApplicationResult = new ApplicationResult(givenApplicationResultId); selectInvoiceService.SubmitApplicationFor(Arg.Any <string>(), Arg.Any <decimal>(), Arg.Any <decimal>()) .Returns(x => sutServiceType == typeof(ISelectInvoiceService) ? givenApplicationResultId : throw routeException); confidentialInvoiceService.SubmitApplicationFor(Arg.Any <CompanyDataRequest>(), Arg.Any <decimal>(), Arg.Any <decimal>(), Arg.Any <decimal>()) .Returns(x => sutServiceType == typeof(IConfidentialInvoiceService) ? givenApplicationResult : throw routeException); businessLoansService.SubmitApplicationFor(Arg.Any <CompanyDataRequest>(), Arg.Any <LoansRequest>()) .Returns(x => sutServiceType == typeof(IBusinessLoansService) ? givenApplicationResult : throw routeException); var productServiceReceiveMap = new Dictionary <Type, Action> { { typeof(SelectiveInvoiceDiscount), () => selectInvoiceService.ReceivedWithAnyArgs().SubmitApplicationFor("", 0, 0) }, { typeof(ConfidentialInvoiceDiscount), () => confidentialInvoiceService.ReceivedWithAnyArgs().SubmitApplicationFor(null, 0, 0, 0) }, { typeof(BusinessLoans), () => businessLoansService.ReceivedWithAnyArgs().SubmitApplicationFor(null, null) }, }; var parameter = new PositionalParameter(0, null); parameter = new PositionalParameter(0, StartUp.Scope.Resolve <IProductResolver>(parameter)); var productService = StartUp.Scope.Resolve <IProductApplicationService>(parameter); //act var applicationResult = productService.SubmitApplicationFor(application); //assert var receiveHandler = productServiceReceiveMap.FirstOrDefault(x => x.Key.IsAssignableFrom(application.Product.GetType())).Value; Assert.IsNotNull(receiveHandler, "Substitute receiveHandler is not assigned to a product"); receiveHandler(); Assert.AreEqual(givenApplicationResultId, applicationResult.ApplicationId); }
public void RegisterGeneric(Type service, Type implementer, LifeStyle life = LifeStyle.Singleton, params string[] parameter) { var builder = new ContainerBuilder(); var registrationBuilder = builder.RegisterGeneric(implementer).As(service).PropertiesAutowired().WithAttributeFiltering(); SetLifetimeScope(life, registrationBuilder); var component = ObjectContainer.ParseComponent(implementer); SetIntercepter(component.Interceptor, registrationBuilder); if (parameter.Length > 0) { for (var i = 0; i < parameter.Length; i++) { var param = new PositionalParameter(i, parameter[i]); registrationBuilder.WithParameter(param); } } builder.Update(Container); }
protected static IEnumerable <Parameter> GetConfiguredParams(ProxyGenerationOptions options, IInterceptor[] interceptors) { List <Parameter> @params = new List <Parameter>(); int position = 0; if (options.HasMixins) { @params.AddRange(options.MixinData.Mixins.Select(obj => (Parameter) new PositionalParameter(position++, obj))); } List <Parameter> list2 = @params; int position1 = position; int position2 = position1 + 1; PositionalParameter positionalParameter = new PositionalParameter(position1, interceptors); list2.Add(positionalParameter); if (options.Selector != null) { @params.Add(new PositionalParameter(position2, options.Selector)); } return(@params); }