How to use PropertyInjectionDirective class of Telerik.JustMock.AutoMock.Ninject.Planning.Directives package

Best JustMockLite code snippet using Telerik.JustMock.AutoMock.Ninject.Planning.Directives.PropertyInjectionDirective

Run JustMockLite automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

PropertyReflectionStrategy.cs

Source: PropertyReflectionStrategy.cs Github

copy
1#region License
2// 
3// Author: Nate Kohari <[email protected]>
4// Copyright (c) 2007-2010, Enkari, Ltd.
5// 
6// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
7// See the file LICENSE.txt for details.
8// 
9#endregion
10#region Using Directives
11using System;
12using System.Reflection;
13using Telerik.JustMock.AutoMock.Ninject.Components;
14using Telerik.JustMock.AutoMock.Ninject.Infrastructure;
15using Telerik.JustMock.AutoMock.Ninject.Injection;
16using Telerik.JustMock.AutoMock.Ninject.Planning.Directives;
17using Telerik.JustMock.AutoMock.Ninject.Selection;
18#endregion
19
20namespace Telerik.JustMock.AutoMock.Ninject.Planning.Strategies
21{
22    /// <summary>
23    /// Adds directives to plans indicating which properties should be injected during activation.
24    /// </summary>
25    public class PropertyReflectionStrategy : NinjectComponent, IPlanningStrategy
26    {
27        /// <summary>
28        /// Gets the selector component.
29        /// </summary>
30        public ISelector Selector { get; private set; }
31
32        /// <summary>
33        /// Gets the injector factory component.
34        /// </summary>
35        public IInjectorFactory InjectorFactory { get; set; }
36
37        /// <summary>
38        /// Initializes a new instance of the <see cref="PropertyReflectionStrategy"/> class.
39        /// </summary>
40        /// <param name="selector">The selector component.</param>
41        /// <param name="injectorFactory">The injector factory component.</param>
42        public PropertyReflectionStrategy(ISelector selector, IInjectorFactory injectorFactory)
43        {
44            Ensure.ArgumentNotNull(selector, "selector");
45            Ensure.ArgumentNotNull(injectorFactory, "injectorFactory");
46
47            Selector = selector;
48            InjectorFactory = injectorFactory;
49        }
50
51        /// <summary>
52        /// Adds a <see cref="PropertyInjectionDirective"/> to the plan for each property
53        /// that should be injected.
54        /// </summary>
55        /// <param name="plan">The plan that is being generated.</param>
56        public void Execute(IPlan plan)
57        {
58            Ensure.ArgumentNotNull(plan, "plan");
59
60            foreach (PropertyInfo property in Selector.SelectPropertiesForInjection(plan.Type))
61                plan.Add(new PropertyInjectionDirective(property, InjectorFactory.Create(property)));
62        }
63    }
64}
Full Screen

PropertyInjectionStrategy.cs

Source: PropertyInjectionStrategy.cs Github

copy
1#region License
2// 
3// Author: Nate Kohari <[email protected]>
4// Copyright (c) 2007-2010, Enkari, Ltd.
5// 
6// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
7// See the file LICENSE.txt for details.
8// 
9#endregion
10#region Using Directives
11using System;
12using System.Collections.Generic;
13using System.Linq;
14using System.Reflection;
15using Telerik.JustMock.AutoMock.Ninject.Infrastructure;
16using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Introspection;
17using Telerik.JustMock.AutoMock.Ninject.Infrastructure.Language;
18using Telerik.JustMock.AutoMock.Ninject.Injection;
19using Telerik.JustMock.AutoMock.Ninject.Parameters;
20using Telerik.JustMock.AutoMock.Ninject.Planning.Directives;
21using Telerik.JustMock.AutoMock.Ninject.Planning.Targets;
22#endregion
23
24namespace Telerik.JustMock.AutoMock.Ninject.Activation.Strategies
25{
26    /// <summary>
27    /// Injects properties on an instance during activation.
28    /// </summary>
29    public class PropertyInjectionStrategy : ActivationStrategy
30    {
31        private const BindingFlags DefaultFlags = BindingFlags.Public | BindingFlags.Instance;
32
33        private BindingFlags Flags
34        {
35            get
36            {
37                #if !NO_LCG && !SILVERLIGHT
38                return Settings.InjectNonPublic ? (DefaultFlags | BindingFlags.NonPublic) : DefaultFlags;
39                #else
40                return DefaultFlags;
41                #endif
42            }
43        }
44
45        /// <summary>
46        /// Gets the injector factory component.
47        /// </summary>
48        public IInjectorFactory InjectorFactory { get; set; }
49
50        /// <summary>
51        /// Initializes a new instance of the <see cref="PropertyInjectionStrategy"/> class.
52        /// </summary>
53        /// <param name="injectorFactory">The injector factory component.</param>
54        public PropertyInjectionStrategy(IInjectorFactory injectorFactory)
55        {
56            this.InjectorFactory = injectorFactory;
57        }
58
59        /// <summary>
60        /// Injects values into the properties as described by <see cref="PropertyInjectionDirective"/>s
61        /// contained in the plan.
62        /// </summary>
63        /// <param name="context">The context.</param>
64        /// <param name="reference">A reference to the instance being activated.</param>
65        public override void Activate(IContext context, InstanceReference reference)
66        {
67            Ensure.ArgumentNotNull(context, "context");
68            Ensure.ArgumentNotNull(reference, "reference");
69
70            var propertyValues = context.Parameters.OfType<IPropertyValue>().ToList();
71
72            foreach (var directive in context.Plan.GetAll<PropertyInjectionDirective>())
73            {
74                object value = this.GetValue(context, directive.Target, propertyValues);
75                directive.Injector(reference.Instance, value);
76            }
77
78            this.AssignProperyOverrides(context, reference, propertyValues);
79        }
80
81        /// <summary>
82        /// Applies user supplied override values to instance properties.
83        /// </summary>
84        /// <param name="context">The context.</param>
85        /// <param name="reference">A reference to the instance being activated.</param>
86        /// <param name="propertyValues">The parameter override value accessors.</param>
87        private void AssignProperyOverrides(IContext context, InstanceReference reference, IList<IPropertyValue> propertyValues)
88        {
89            var properties = reference.Instance.GetType().GetProperties(this.Flags);
90            foreach (var propertyValue in propertyValues)
91            {
92                string propertyName = propertyValue.Name;
93                var propertyInfo = properties.FirstOrDefault(property => string.Equals(property.Name, propertyName, StringComparison.Ordinal));
94
95                if (propertyInfo == null)
96                {
97                    throw new ActivationException(ExceptionFormatter.CouldNotResolvePropertyForValueInjection(context.Request, propertyName));
98                }
99
100                var target = new PropertyInjectionDirective(propertyInfo, this.InjectorFactory.Create(propertyInfo));
101                object value = this.GetValue(context, target.Target, propertyValues);
102                target.Injector(reference.Instance, value);
103            }
104        }
105
106        /// <summary>
107        /// Gets the value to inject into the specified target.
108        /// </summary>
109        /// <param name="context">The context.</param>
110        /// <param name="target">The target.</param>
111        /// <param name="allPropertyValues">all property values of the current request.</param>
112        /// <returns>The value to inject into the specified target.</returns>
113        private object GetValue(IContext context, ITarget target, IEnumerable<IPropertyValue> allPropertyValues)
114        {
115            Ensure.ArgumentNotNull(context, "context");
116            Ensure.ArgumentNotNull(target, "target");
117
118            var parameter = allPropertyValues.SingleOrDefault(p => p.Name == target.Name);
119            return parameter != null ? parameter.GetValue(context, target) : target.ResolveWithin(context);
120        }
121    }
122}
Full Screen

PropertyInjectionDirective.cs

Source: PropertyInjectionDirective.cs Github

copy
1#region License
2// 
3// Author: Nate Kohari <[email protected]>
4// Copyright (c) 2007-2010, Enkari, Ltd.
5// 
6// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
7// See the file LICENSE.txt for details.
8// 
9#endregion
10#region Using Directives
11using System;
12using System.Reflection;
13using Telerik.JustMock.AutoMock.Ninject.Injection;
14using Telerik.JustMock.AutoMock.Ninject.Planning.Targets;
15#endregion
16
17namespace Telerik.JustMock.AutoMock.Ninject.Planning.Directives
18{
19    /// <summary>
20    /// Describes the injection of a property.
21    /// </summary>
22    public class PropertyInjectionDirective : IDirective
23    {
24        /// <summary>
25        /// Gets or sets the injector that will be triggered.
26        /// </summary>
27        public PropertyInjector Injector { get; private set; }
28
29        /// <summary>
30        /// Gets or sets the injection target for the directive.
31        /// </summary>
32        public ITarget Target { get; private set; }
33
34        /// <summary>
35        /// Initializes a new instance of the <see cref="PropertyInjectionDirective"/> class.
36        /// </summary>
37        /// <param name="member">The member the directive describes.</param>
38        /// <param name="injector">The injector that will be triggered.</param>
39        public PropertyInjectionDirective(PropertyInfo member, PropertyInjector injector)
40        {
41            Injector = injector;
42            Target = CreateTarget(member);
43        }
44
45        /// <summary>
46        /// Creates a target for the property.
47        /// </summary>
48        /// <param name="propertyInfo">The property.</param>
49        /// <returns>The target for the property.</returns>
50        protected virtual ITarget CreateTarget(PropertyInfo propertyInfo)
51        {
52            return new PropertyTarget(propertyInfo);
53        }
54    }
55}
56
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used methods in PropertyInjectionDirective

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)