public void Can_pass_parent_type_as_string_param_to_dependency() { var container = new Container(); container.Register <MyService>(); container.Register <ConnectionStringProvider>(Reuse.Singleton); container.Register <IConnectionStringProvider, ConnectionNamingConnectionStringProvider>( made: Made.Of(parameters: request => parameter => { if (parameter.ParameterType != typeof(string)) { return(null); } var targetType = request.Parent.ImplementationType; var targetName = string.Format("{0}.{1}", targetType.Namespace, targetType.Name); return(ParameterServiceInfo.Of(parameter) .WithDetails(ServiceDetails.Of(defaultValue: targetName))); })); var service = container.Resolve <MyService>(); var provider = service.ConnectionProvider as ConnectionNamingConnectionStringProvider; Assert.IsNotNull(provider); Assert.AreEqual("Me.MyService", provider.TargetName); }
public static Func <ParameterInfo, ParameterServiceInfo> GetServiceInfoFromImportAttribute(Request request) { return(parameter => { var import = (ImportAttribute)parameter.GetAttributes(typeof(ImportAttribute)).FirstOrDefault(); var details = import == null ? ServiceDetails.Default : ServiceDetails.Of(import.ContractType, import.ContractName); return ParameterServiceInfo.Of(parameter).WithDetails(details); }); }
private static Func <ParameterInfo, ParameterServiceInfo> GetImportedParameter(Request request) { return(parameter => { var serviceInfo = ParameterServiceInfo.Of(parameter); var attrs = parameter.GetAttributes().ToArray(); return attrs.Length == 0 ? serviceInfo : serviceInfo.WithDetails(GetFirstImportDetailsOrNull(parameter.ParameterType, attrs, request), request); }); }
public void Test_parameter() { var container = new Container(rules => rules .With(parameters: request => p => { var attr = p.GetCustomAttribute <ServiceKeyResolverAttribute>(); return(attr == null ? null : ParameterServiceInfo.Of(p) .WithDetails(ServiceDetails.Of(ConfigWrapper.GetValue(attr.Key)))); })); container.Register <IService, Service>(); var service = (Service)container.Resolve <IService>(); Assert.AreEqual("id: paths.baseUrl", service.BaseUrl); }
private static bool IsResolvableParameter(ParameterInfo parameter, Func <ParameterInfo, ParameterServiceInfo> parameterSelector, Request request) { var parameterServiceInfo = parameterSelector(parameter) ?? ParameterServiceInfo.Of(parameter); var parameterRequest = request.Push(parameterServiceInfo.WithDetails(ServiceDetails.IfUnresolvedReturnDefault, request)); if (parameterServiceInfo.Details.HasCustomValue) { var customValue = parameterServiceInfo.Details.CustomValue; return(customValue == null || customValue.GetType().IsAssignableTo(parameterRequest.ServiceType)); } var parameterFactory = request.Container.ResolveFactory(parameterRequest); return(parameterFactory != null && parameterFactory.GetExpressionOrDefault(parameterRequest) != null); }
public static Func <ParameterInfo, ParameterServiceInfo> GetServiceFromWithMetadataAttribute(Request request) { return(parameter => { var import = (ImportWithMetadataAttribute)parameter.GetAttributes(typeof(ImportWithMetadataAttribute)) .FirstOrDefault(); if (import == null) { return null; } var registry = request.Container; var serviceType = parameter.ParameterType; serviceType = registry.GetWrappedType(serviceType, request.RequiredServiceType); var metadata = import.Metadata; var factory = registry.GetAllServiceFactories(serviceType) .FirstOrDefault(kv => metadata.Equals(kv.Value.Setup.Metadata)) .ThrowIfNull(); return ParameterServiceInfo.Of(parameter).WithDetails(ServiceDetails.Of(serviceType, factory.Key)); }); }