/// <summary> /// /// </summary> /// <param name="resolver"></param> /// <param name="parameterType"></param> /// <param name="setOperationExpression"></param> /// <returns></returns> public Expression CreateParameterExpression(IDependencyResolverPolicy resolver, Type parameterType, Expression setOperationExpression) { // The intent of this is to create a parameter resolving expression block. The following // pseudo code will hopefully make it clearer as to what we're trying to accomplish (of course actual code // trumps comments): // object priorOperation = context.CurrentOperation; // SetCurrentOperation // var resolver = GetResolver([context], [paramType], [key]) // var dependencyResult = resolver.ResolveDependency([context]); // context.CurrentOperation = priorOperation; // dependencyResult ; // return item from Block var savedOperationExpression = Expression.Parameter(typeof(object)); var resolvedObjectExpression = Expression.Parameter(parameterType); return (Expression.Block( new[] { savedOperationExpression, resolvedObjectExpression }, SaveCurrentOperationExpression(savedOperationExpression), setOperationExpression, Expression.Assign( resolvedObjectExpression, GetResolveDependencyExpression(parameterType, resolver)), RestoreCurrentOperationExpression(savedOperationExpression), resolvedObjectExpression)); }
// TODO: Verify //[TestMethod] //public void SelectConstructorWithNoParameters() //{ // ConstructorInfo ctor = typeof(ClassWithSimpleConstructor).GetConstructor(new Type[0]); // SpecifiedConstructorSelectorPolicy policy = new SpecifiedConstructorSelectorPolicy(ctor, new InjectionParameterValue[0]); // BuilderContextMock builderContext = new BuilderContextMock(typeof(ClassWithSimpleConstructor)); // SelectedConstructor selectedCtor = policy.SelectConstructor(builderContext); // Assert.AreEqual(ctor, selectedCtor.Constructor); // Assert.AreEqual(0, selectedCtor.GetParameterKeys().Length); //} //[TestMethod] //public void SelectConstructorWith2Parameters() //{ // ConstructorInfo ctor = typeof(ClassWithConstructorParameters).GetConstructor(Types(typeof(int), typeof(string))); // SpecifiedConstructorSelectorPolicy policy = new SpecifiedConstructorSelectorPolicy(ctor, // new InjectionParameterValue[] // { // new InjectionParameter<int>(37), // new InjectionParameter<string>("abc") // }); // BuilderContextMock builderContext = new BuilderContextMock(typeof(ClassWithConstructorParameters)); // SelectedConstructor selectedCtor = policy.SelectConstructor(builderContext); // Assert.AreEqual(ctor, selectedCtor.Constructor); // Assert.AreEqual(2, selectedCtor.GetParameterKeys().Length); // string[] keys = selectedCtor.GetParameterKeys(); // Assert.AreEqual(2, keys.Length); // foreach (string key in keys) // { // AssertPolicyIsCorrect(key, builderContext); // } //} //[TestMethod] //public void CanSelectConcreteConstructorGivenGenericConstructor() //{ // ConstructorInfo ctor = typeof(LoggingCommand<>).GetConstructors()[0]; // SpecifiedConstructorSelectorPolicy policy = new SpecifiedConstructorSelectorPolicy( // ctor, // new InjectionParameterValue[] // { // new ResolvedParameter(typeof (ICommand<>), "concrete") // }); // BuilderContextMock ctx = new BuilderContextMock(); // ctx.BuildKey = typeof(LoggingCommand<User>); // SelectedConstructor result = policy.SelectConstructor(ctx); // ConstructorInfo expectedCtor = typeof(LoggingCommand<User>).GetConstructor(Types(typeof(ICommand<User>))); // Assert.AreSame(expectedCtor, result.Constructor); //} private void AssertPolicyIsCorrect(string key, IBuilderContext context) { IDependencyResolverPolicy policy = context.PersistentPolicies.Get <IDependencyResolverPolicy>(key); Assert.IsNotNull(policy); Assert.IsInstanceOfType(policy, typeof(LiteralValueDependencyResolverPolicy)); }
public void InjectionConstructorSetsResolverForInterfaceToLookupInContainer() { InjectionConstructor ctor = new InjectionConstructor("Logger", typeof(ILogger)); TestingBuilderContext context = new TestingBuilderContext(); context.BuildKey = typeof(GuineaPig); IPolicyList policies = context.PersistentPolicies; ctor.AddPolicies(typeof(GuineaPig), policies); IConstructorSelectorPolicy selector = policies.Get <IConstructorSelectorPolicy>( new NamedTypeBuildKey(typeof(GuineaPig))); SelectedConstructor selected = selector.SelectConstructor(context); string[] keys = selected.GetParameterKeys(); Assert.AreEqual(typeof(GuineaPig).GetConstructor(Sequence.Collect(typeof(string), typeof(ILogger))), selected.Constructor); Assert.AreEqual(2, keys.Length); IDependencyResolverPolicy policy = context.Policies.Get <IDependencyResolverPolicy>(keys[1]); Assert.IsTrue(policy is NamedTypeDependencyResolverPolicy); }
private void AssertExpectedValue(InjectionParameter parameter, Type expectedType, object expectedValue) { IDependencyResolverPolicy resolver = parameter.GetResolverPolicy(expectedType); object result = resolver.Resolve(null); Assert.AreEqual(expectedType, parameter.ParameterType); AssertExtensions.IsInstanceOfType(resolver, typeof(LiteralValueDependencyResolverPolicy)); Assert.AreEqual(expectedValue, result); }
/// <summary> /// Return a <see cref="IDependencyResolverPolicy"/> that can be used to give a value /// for the given desired dependency. /// </summary> /// <param name="context">Current build context.</param> /// <param name="dependencyType">Type of dependency desired.</param> /// <returns>a <see cref="IDependencyResolverPolicy"/> object if this override applies, null if not.</returns> public override IDependencyResolverPolicy GetResolver(IBuilderContext context, Type dependencyType) { IDependencyResolverPolicy result = null; if (dependencyType == typeToConstruct) { result = dependencyValue.GetResolverPolicy(dependencyType); } return(result); }
public void DependencyParameterCreatesExpectedResolver() { Type expectedType = typeof(ILogger); ResolvedParameter parameter = new ResolvedParameter <ILogger>(); IDependencyResolverPolicy resolver = parameter.GetResolverPolicy(expectedType); AssertExtensions.IsInstanceOfType(resolver, typeof(NamedTypeDependencyResolverPolicy)); Assert.AreEqual(expectedType, ((NamedTypeDependencyResolverPolicy)resolver).Type); Assert.IsNull(((NamedTypeDependencyResolverPolicy)resolver).Name); }
public void ResolvedParameterHandledNamedTypes() { Type expectedType = typeof(ILogger); string name = "special"; ResolvedParameter parameter = new ResolvedParameter(expectedType, name); IDependencyResolverPolicy resolver = parameter.GetResolverPolicy(expectedType); AssertExtensions.IsInstanceOfType(resolver, typeof(NamedTypeDependencyResolverPolicy)); Assert.AreEqual(expectedType, ((NamedTypeDependencyResolverPolicy)resolver).Type); Assert.AreEqual(name, ((NamedTypeDependencyResolverPolicy)resolver).Name); }
public void ObjectsConverterToInjectionParametersResolveCorrectly() { List <InjectionParameterValue> values = GetParameterValues(15); InjectionParameter parameter = (InjectionParameter)values[0]; Assert.AreEqual(typeof(int), parameter.ParameterType); IDependencyResolverPolicy policy = parameter.GetResolverPolicy(null); int result = (int)policy.Resolve(null); Assert.AreEqual(15, result); }
internal Expression GetResolveDependencyExpression(Type dependencyType, IDependencyResolverPolicy resolver) { return(Expression.Convert( Expression.Call( Expression.Call(null, GetResolverMethod, ContextParameter, Expression.Constant(dependencyType, typeof(Type)), Expression.Constant(resolver, typeof(IDependencyResolverPolicy))), ResolveDependencyMethod, ContextParameter), dependencyType)); }
private SelectedMethod CreateSelectedMethod(IBuilderContext context, IPolicyList resolverPolicyDestination, MethodInfo method) { SelectedMethod result = new SelectedMethod(method); foreach (ParameterInfo parameter in method.GetParameters()) { string key = Guid.NewGuid().ToString(); IDependencyResolverPolicy resolver = CreateResolver(parameter); resolverPolicyDestination.Set <IDependencyResolverPolicy>(resolver, key); DependencyResolverTrackerPolicy.TrackKey(context.PersistentPolicies, context.BuildKey, key); result.AddParameterKey(key); } return(result); }
private SelectedConstructor CreateSelectedConstructor(IBuilderContext context, ConstructorInfo ctor) { SelectedConstructor result = new SelectedConstructor(ctor); foreach (ParameterInfo param in ctor.GetParameters()) { string key = Guid.NewGuid().ToString(); IDependencyResolverPolicy policy = CreateResolver(param); context.PersistentPolicies.Set <IDependencyResolverPolicy>(policy, key); DependencyResolverTrackerPolicy.TrackKey(context.PersistentPolicies, context.BuildKey, key); result.AddParameterKey(key); } return(result); }
private static string GetValue(IBuilderContext context, IDependencyResolverPolicy resolverPolicy) { return(resolverPolicy.Resolve(context).ToString()); }
internal Expression GetResolveDependencyExpression(Type dependencyType, IDependencyResolverPolicy resolver) { return Expression.Convert( Expression.Call( Expression.Call(null, GetResolverMethod, ContextParameter, Expression.Constant(dependencyType, typeof(Type)), Expression.Constant(resolver, typeof(IDependencyResolverPolicy))), ResolveDependencyMethod, ContextParameter), dependencyType); }
/// <summary> /// Helper method used by generated IL to look up a dependency resolver based on the given key. /// </summary> /// <param name="context">Current build context.</param> /// <param name="dependencyType">Type of the dependency being resolved.</param> /// <param name="resolver">The configured resolver.</param> /// <returns>The found dependency resolver.</returns> public static IDependencyResolverPolicy GetResolver(IBuilderContext context, Type dependencyType, IDependencyResolverPolicy resolver) { Guard.ArgumentNotNull(context, "context"); var overridden = context.GetOverriddenResolver(dependencyType); return overridden ?? resolver; }
/// <summary> /// Constructs a new <see cref="ResourceDependencyResolverPolicy"/>. /// </summary> /// <param name="uriResolver">a resolver that returns a URI</param> /// <param name="many">true if there can be several resources; false if only one is expected</param> public ResourceDependencyResolverPolicy(IDependencyResolverPolicy uriResolver, bool many) { _uriResolver = uriResolver; _many = many; }
/// <summary> /// /// </summary> /// <param name="resolver"></param> /// <param name="parameterType"></param> /// <param name="setOperationExpression"></param> /// <returns></returns> public Expression CreateParameterExpression(IDependencyResolverPolicy resolver, Type parameterType, Expression setOperationExpression) { // The intent of this is to create a parameter resolving expression block. The following // pseudo code will hopefully make it clearer as to what we're trying to accomplish (of course actual code // trumps comments): // object priorOperation = context.CurrentOperation; // SetCurrentOperation // var resolver = GetResolver([context], [paramType], [key]) // var dependencyResult = resolver.ResolveDependency([context]); // context.CurrentOperation = priorOperation; // dependencyResult ; // return item from Block var savedOperationExpression = Expression.Parameter(typeof(object)); var resolvedObjectExpression = Expression.Parameter(parameterType); return Expression.Block( new[] { savedOperationExpression, resolvedObjectExpression }, SaveCurrentOperationExpression(savedOperationExpression), setOperationExpression, Expression.Assign( resolvedObjectExpression, GetResolveDependencyExpression(parameterType, resolver)), RestoreCurrentOperationExpression(savedOperationExpression), resolvedObjectExpression); }
public TypeToBeResolved(Type parameterType, IDependencyResolverPolicy resolver) : base(parameterType) { this.resolver = resolver; }
/// <summary> /// Helper method used by generated IL to look up a dependency resolver based on the given key. /// </summary> /// <param name="context">Current build context.</param> /// <param name="dependencyType">Type of the dependency being resolved.</param> /// <param name="resolver">The configured resolver.</param> /// <returns>The found dependency resolver.</returns> public static IDependencyResolverPolicy GetResolver(IBuilderContext context, Type dependencyType, IDependencyResolverPolicy resolver) { var overridden = (context ?? throw new ArgumentNullException(nameof(context))).GetOverriddenResolver(dependencyType); return(overridden ?? resolver); }
public TypeToBeResolved(Type parameterType, IDependencyResolverPolicy resolver) : base(parameterType) { this.resolver = resolver; }
private static void AssertPolicyIsCorrect(IDependencyResolverPolicy policy) { Assert.NotNull(policy); AssertExtensions.IsInstanceOfType(policy, typeof(LiteralValueDependencyResolverPolicy)); }
private static void AssertPolicyIsCorrect(IDependencyResolverPolicy policy) { Assert.NotNull(policy); AssertExtensions.IsInstanceOfType(policy, typeof(LiteralValueDependencyResolverPolicy)); }
/// <summary> /// Adds the parameter resolver. Resolvers are assumed /// to be in the order of the parameters to the member. /// </summary> /// <param name="newResolver">The new resolver.</param> public void AddParameterResolver(IDependencyResolverPolicy newResolver) { parameterResolvers.Add(newResolver); }
public InjectedObjectSelectorPolicy(IDependencyResolverPolicy resolverPolicy) { this.resolverPolicy = resolverPolicy; }
public InjectedObjectConfigurationExtension(IDependencyResolverPolicy resolverPolicy) { this.resolverPolicy = resolverPolicy; }
/// <summary> /// Constructs a new <see cref="ResourceDependencyResolverPolicy"/>. /// </summary> /// <param name="uriResolver">a resolver that returns a URI</param> /// <param name="many">true if there can be several resources; false if only one is expected</param> public ResourceDependencyResolverPolicy(IDependencyResolverPolicy uriResolver, bool many) { _uriResolver = uriResolver; _many = many; }
public InjectedObjectConfigurationExtension(IDependencyResolverPolicy resolverPolicy) { this.resolverPolicy = resolverPolicy; }
/// <summary> /// Adds the parameter resolver. Resolvers are assumed /// to be in the order of the parameters to the member. /// </summary> /// <param name="newResolver">The new resolver.</param> public void AddParameterResolver(IDependencyResolverPolicy newResolver) { parameterResolvers.Add(newResolver); }
public InjectedObjectSelectorPolicy(IDependencyResolverPolicy resolverPolicy) { this.resolverPolicy = resolverPolicy; }
private SelectedProperty CreateSelectedProperty(PropertyInfo property) { IDependencyResolverPolicy resolver = this.CreateResolver(property); return(new SelectedProperty(property, resolver)); }
/// <summary> /// Create an instance of <see cref="SelectedProperty"/> /// with the given <see cref="PropertyInfo"/> and key. /// </summary> /// <param name="property">The property.</param> /// <param name="resolver"></param> public SelectedProperty(PropertyInfo property, IDependencyResolverPolicy resolver) { this.property = property; this.resolver = resolver; }
private object ResolveValue(IPolicyList policies, string key) { IDependencyResolverPolicy resolver = policies.Get <IDependencyResolverPolicy>(key); return(resolver.Resolve(null)); }
public TestSinglePropertySelectorPolicy(IDependencyResolverPolicy resolverPolicy) { this.resolverPolicy = resolverPolicy; }
/// <summary> /// Helper method used by generated IL to look up a dependency resolver based on the given key. /// </summary> /// <param name="context">Current build context.</param> /// <param name="dependencyType">Type of the dependency being resolved.</param> /// <param name="resolver">The configured resolver.</param> /// <returns>The found dependency resolver.</returns> public static IDependencyResolverPolicy GetResolver(IBuilderContext context, Type dependencyType, IDependencyResolverPolicy resolver) { Guard.ArgumentNotNull(context, "context"); var overridden = context.GetOverriddenResolver(dependencyType); return(overridden ?? resolver); }
public TestSingleArgumentMethodSelectorPolicy(IDependencyResolverPolicy resolverPolicy) { this.resolverPolicy = resolverPolicy; }
public TestSingleArgumentConstructorSelectorPolicy(IDependencyResolverPolicy parameterResolverPolicy) { this.parameterResolverPolicy = parameterResolverPolicy; }
/// <summary> /// Create an instance of <see cref="SelectedProperty"/> /// with the given <see cref="PropertyInfo"/> and key. /// </summary> /// <param name="property">The property.</param> /// <param name="resolver"></param> public SelectedProperty(PropertyInfo property, IDependencyResolverPolicy resolver) { Property = property; Resolver = resolver; }
private SelectedProperty CreateSelectedProperty(IBuilderContext context, PropertyInfo property) { IDependencyResolverPolicy resolver = this.CreateResolver(context, property); return(new SelectedProperty(property, resolver)); }