Beispiel #1
0
 public InjectionAttribute(ServiceLifetime lifetime = ServiceLifetime.Scoped,
                           InjectionPolicy policy   = InjectionPolicy.Append,
                           params Type[] serviceTypes)
 {
     ServiceTypes = serviceTypes;
     Lifetime     = lifetime;
     Policy       = policy;
 }
 public InjectionAttribute(Type serviceType,
                           ServiceLifetime lifetime = ServiceLifetime.Scoped,
                           InjectionPolicy policy   = InjectionPolicy.Append)
 {
     ServiceType = serviceType;
     Lifetime    = lifetime;
     Policy      = policy;
 }
Beispiel #3
0
        public void ShouldHaveNoHandlersWhenPolicyDoesntMatch()
        {
            IMatchingRule[] rules     = { };
            IUnityContainer container = CreateConfiguredContainer();

            InjectionPolicy p = CreatePolicy(container, rules);

            MethodImplementationInfo thisMember = GetMethodImplInfo <PolicyFixture>("ShouldHaveNoHandlersWhenPolicyDoesntMatch");
            List <ICallHandler>      memberHandlers
                = new List <ICallHandler>(p.GetHandlersFor(thisMember, container));

            Assert.AreEqual(0, memberHandlers.Count);
        }
Beispiel #4
0
        public void ShouldGetHandlersInOrderWithGetHandlersFor()
        {
            IMatchingRule[] rules     = { new MemberNameMatchingRule("ShouldGetHandlersInOrderWithGetHandlersFor") };
            IUnityContainer container = CreateConfiguredContainer();

            InjectionPolicy p = CreatePolicy(container, rules);

            MethodImplementationInfo member = GetMethodImplInfo <PolicyFixture>("ShouldGetHandlersInOrderWithGetHandlersFor");

            List <ICallHandler> expectedHandlers = new List <ICallHandler>(container.ResolveAll <ICallHandler>());
            List <ICallHandler> actualHandlers   = new List <ICallHandler>(p.GetHandlersFor(member, container));

            CollectionAssertExtensions.AreEqual(
                expectedHandlers,
                actualHandlers,
                new TypeComparer());
        }
Beispiel #5
0
        public void ShouldBeAbleToMatchPropertyGet()
        {
            IMatchingRule[] rules     = { new MemberNameMatchingRule("get_Balance") };
            IUnityContainer container = CreateConfiguredContainer();

            InjectionPolicy p = CreatePolicy(container, rules);

            PropertyInfo             balanceProperty = typeof(MockDal).GetProperty("Balance");
            MethodImplementationInfo getMethod       = new MethodImplementationInfo(null, balanceProperty.GetGetMethod());

            List <ICallHandler> expectedHandlers = new List <ICallHandler>(container.ResolveAll <ICallHandler>());
            List <ICallHandler> actualHandlers   = new List <ICallHandler>(p.GetHandlersFor(getMethod, container));

            CollectionAssertExtensions.AreEqual(
                expectedHandlers,
                actualHandlers,
                new TypeComparer());
        }
Beispiel #6
0
        /// <summary>
        /// Intercepts an exported value.
        /// </summary>
        /// <param name="value">The value to be intercepted.</param>
        /// <returns>Intercepted value.</returns>
        public object Intercept(object value)
        {
            var interfaces           = value.GetType().GetInterfaces();
            var proxyInterface       = interfaces.FirstOrDefault();
            var additionalInterfaces = interfaces.Skip(1).ToArray();

            CurrentInterceptionRequest request = new CurrentInterceptionRequest(interceptor, proxyInterface, value.GetType());

            InjectionPolicy[]       policies  = new InjectionPolicy[] { new AttributeDrivenPolicy() };
            PolicyInjectionBehavior behaviour = new PolicyInjectionBehavior(request, policies, null);

            var proxy = Microsoft.Practices.Unity.InterceptionExtension.Intercept.ThroughProxyWithAdditionalInterfaces(
                proxyInterface,
                value,
                interceptor,
                new[] { behaviour },
                additionalInterfaces);

            return(proxy);
        }
Beispiel #7
0
 public InjectionAttribute(InjectionPolicy policy)
 {
     Policy = policy;
 }
Beispiel #8
0
        private static void AddService(IServiceCollection serviceCollection, Type serviceType, Type implementationType, ServiceLifetime lifetime, InjectionPolicy policy)
        {
            var descriptor = new ServiceDescriptor(serviceType, implementationType, lifetime);

            switch (policy)
            {
            case InjectionPolicy.Append:
                serviceCollection.TryAddEnumerable(descriptor);
                break;

            case InjectionPolicy.Skip:
                serviceCollection.TryAdd(descriptor);
                break;

            case InjectionPolicy.Replace:
                serviceCollection.Replace(descriptor);
                break;
            }
        }