コード例 #1
0
ファイル: InjectionAttribute.cs プロジェクト: JolyneStone/Rye
 public InjectionAttribute(ServiceLifetime lifetime = ServiceLifetime.Scoped,
                           InjectionPolicy policy   = InjectionPolicy.Append,
                           params Type[] serviceTypes)
 {
     ServiceTypes = serviceTypes;
     Lifetime     = lifetime;
     Policy       = policy;
 }
コード例 #2
0
 public InjectionAttribute(Type serviceType,
                           ServiceLifetime lifetime = ServiceLifetime.Scoped,
                           InjectionPolicy policy   = InjectionPolicy.Append)
 {
     ServiceType = serviceType;
     Lifetime    = lifetime;
     Policy      = policy;
 }
コード例 #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);
        }
コード例 #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());
        }
コード例 #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());
        }
コード例 #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);
        }
コード例 #7
0
ファイル: InjectionAttribute.cs プロジェクト: JolyneStone/Rye
 public InjectionAttribute(InjectionPolicy policy)
 {
     Policy = policy;
 }
コード例 #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;
            }
        }