Пример #1
0
        public void Ninject_Supports_Resolving_All_Aspects_From_Container()
        {
            var container = new NinjectAspectContainer();

            SnapConfiguration.For(container).Configure(c =>
            {
                c.IncludeNamespace("SnapTests.*");
                c.Bind <FirstInterceptor>().To <FirstAttribute>();
                c.Bind <SecondInterceptor>().To <SecondAttribute>();
                c.AllAspects().KeepInContainer();
            });

            container.Kernel.Bind <IOrderedCode>().To <OrderedCode>();
            container.Kernel.Bind <FirstInterceptor>().ToConstant(new FirstInterceptor("first_kept_in_container"));
            container.Kernel.Bind <SecondInterceptor>().ToConstant(new SecondInterceptor("second_kept_in_container"));

            var orderedCode = container.Kernel.Get <IOrderedCode>();

            orderedCode.RunInOrder();

            // both interceptors are resolved from container
            CollectionAssert.AreEquivalent(
                OrderedCode.Actions,
                new[] { "first_kept_in_container", "second_kept_in_container" });
        }
Пример #2
0
        public void When_resolving_services_from_container_SNAP_should_load_dynamicproxygenassebmly2_in_appdomain_only_once()
        {
            var container = new ServiceContainer();

            SnapConfiguration.For(new LinFuAspectContainer(container)).Configure(c =>
            {
                c.IncludeNamespaceOf <IBadCode>();
                c.Bind <SecondInterceptor>().To <SecondAttribute>();
                c.Bind <FirstInterceptor>().To <FirstAttribute>();
                c.Bind <HandleErrorInterceptor>().To <HandleErrorAttribute>();
            });

            container.AddService(typeof(IBadCode), typeof(BadCode));
            container.AddService(typeof(IOrderedCode), typeof(OrderedCode));

            var orderedCode = container.GetService <IOrderedCode>();
            var badCode     = container.GetService <IBadCode>();

            orderedCode.RunInOrder();
            Assert.AreEqual("First", OrderedCode.Actions[1]);
            Assert.AreEqual("Second", OrderedCode.Actions[0]);

            Assert.DoesNotThrow(badCode.GiddyUp);

            var dynamicProxyGenerationAssemblies = AppDomain.CurrentDomain
                                                   .GetAssemblies()
                                                   .Where(assembly => assembly.GetName().Name == "DynamicProxyGenAssembly2")
                                                   .ToList();

            Assert.That(dynamicProxyGenerationAssemblies.Count, Is.EqualTo(2));
            // both signed and unsigned.
            Assert.IsNotNull(dynamicProxyGenerationAssemblies.FirstOrDefault(a => a.GetName().GetPublicKey().Length > 0));
            Assert.IsNotNull(dynamicProxyGenerationAssemblies.FirstOrDefault(a => a.GetName().GetPublicKey().Length == 0));
        }
Пример #3
0
        public void LinFu_Supports_Resolving_Only_Selected_Aspects_From_Container()
        {
            var container = new ServiceContainer();

            SnapConfiguration.For(new LinFuAspectContainer(container)).Configure(c =>
            {
                c.IncludeNamespace("SnapTests.*");
                c.Bind <FirstInterceptor>().To <FirstAttribute>();
                c.Bind <SecondInterceptor>().To <SecondAttribute>();
                c.Aspects(typeof(FirstInterceptor)).KeepInContainer();
            });

            container.AddService(typeof(IOrderedCode), typeof(OrderedCode));
            container.AddService(typeof(FirstInterceptor), new FirstInterceptor("first_kept_in_container"));
            container.AddService(typeof(SecondInterceptor), new SecondInterceptor("second_kept_in_container"));

            var orderedCode = container.GetService <IOrderedCode>();

            orderedCode.RunInOrder();

            // first interceptor is resolved from container, while second one is via new()
            CollectionAssert.AreEquivalent(
                OrderedCode.Actions,
                new[] { "first_kept_in_container", "Second" });
        }
Пример #4
0
        public void Autofac_Supports_Resolving_Only_Selected_Aspects_From_Container()
        {
            var builder = new ContainerBuilder();

            SnapConfiguration.For(new AutofacAspectContainer(builder)).Configure(c =>
            {
                c.IncludeNamespace("SnapTests.*");
                c.Bind <FirstInterceptor>().To <FirstAttribute>();
                c.Bind <SecondInterceptor>().To <SecondAttribute>();
                c.Aspects(typeof(FirstInterceptor)).KeepInContainer();
            });

            builder.Register(r => new OrderedCode()).As <IOrderedCode>();
            builder.Register(r => new FirstInterceptor("first_kept_in_container"));
            builder.Register(r => new SecondInterceptor("second_kept_in_container"));

            using (var container = builder.Build())
            {
                var orderedCode = container.Resolve <IOrderedCode>();
                orderedCode.RunInOrder();

                // first interceptor is resolved from container, while second one is via new()
                CollectionAssert.AreEquivalent(
                    OrderedCode.Actions,
                    new[] { "first_kept_in_container", "Second" });
            }
        }
Пример #5
0
        public void When_resolving_services_from_container_SNAP_should_load_dynamicproxygenassebmly2_in_appdomain_only_once()
        {
            SnapConfiguration.For <StructureMapAspectContainer>(c =>
            {
                c.IncludeNamespaceOf <IBadCode>();
                c.Bind <SecondInterceptor>().To <SecondAttribute>();
                c.Bind <FirstInterceptor>().To <FirstAttribute>();
                c.Bind <HandleErrorInterceptor>().To <HandleErrorAttribute>();
            });

            ObjectFactory.Configure(c => c.For <IOrderedCode>().Use <OrderedCode>());
            ObjectFactory.Configure(c => c.For <IBadCode>().Use <BadCode>());
            var orderedCode = ObjectFactory.GetInstance <IOrderedCode>();
            var badCode     = ObjectFactory.GetInstance <IBadCode>();

            orderedCode.RunInOrder();
            Assert.AreEqual("First", OrderedCode.Actions[1]);
            Assert.AreEqual("Second", OrderedCode.Actions[0]);

            Assert.DoesNotThrow(badCode.GiddyUp);

            var dynamicProxyGenerationAssemblies = AppDomain.CurrentDomain
                                                   .GetAssemblies()
                                                   .Where(assembly => assembly.GetName().Name == "DynamicProxyGenAssembly2")
                                                   .ToList();

            Assert.That(dynamicProxyGenerationAssemblies.Count, Is.EqualTo(2));
            Assert.IsNotNull(dynamicProxyGenerationAssemblies.FirstOrDefault(a => a.GetName().GetPublicKey().Length > 0));
            Assert.IsNotNull(dynamicProxyGenerationAssemblies.FirstOrDefault(a => a.GetName().GetPublicKey().Length == 0));
        }
Пример #6
0
        public void CastleWindsor_Supports_Resolving_Only_Selected_Aspects_From_Container()
        {
            var container = new WindsorContainer();

            SnapConfiguration.For(new CastleAspectContainer(container.Kernel)).Configure(c =>
            {
                c.IncludeNamespace("SnapTests.*");
                c.Bind <FirstInterceptor>().To <FirstAttribute>();
                c.Bind <SecondInterceptor>().To <SecondAttribute>();
                c.Aspects(typeof(FirstInterceptor)).KeepInContainer();
            });

            container.Register(Component.For(typeof(IOrderedCode)).ImplementedBy(typeof(OrderedCode)));
            container.Register(Component.For <FirstInterceptor>().Instance(new FirstInterceptor("first_kept_in_container")));
            container.Register(Component.For <SecondInterceptor>().Instance(new SecondInterceptor("second_kept_in_container")));

            var orderedCode = container.Resolve <IOrderedCode>();

            orderedCode.RunInOrder();

            // first interceptor is resolved from container, while second one is via new()
            CollectionAssert.AreEquivalent(
                OrderedCode.Actions,
                new[] { "first_kept_in_container", "Second" });
        }
Пример #7
0
        public void Ninject_Supports_Types_Without_Interfaces()
        {
            var container = new NinjectAspectContainer();

            SnapConfiguration.For(container).Configure(c =>
            {
                c.IncludeNamespace("SnapTests.Fakes*");
                c.Bind <HandleErrorInterceptor>().To <HandleErrorAttribute>
                    ();
            });

            container.Kernel.Bind <IDependency>().To <DummyDependency>();

            container.Kernel.Bind <TypeWithoutInterface>().To <TypeWithoutInterface>();
            var typeWithoutInterface = container.Kernel.Get <TypeWithoutInterface>();

            Assert.DoesNotThrow(typeWithoutInterface.Foo);
            Assert.IsTrue(typeWithoutInterface.GetType().Name.Equals("TypeWithoutInterfaceProxy"));

            container.Kernel.Bind <TypeWithInterfaceInBaseClass>().To <TypeWithInterfaceInBaseClass>();
            var typeWithInterfaceInBaseClass = container.Kernel.Get <TypeWithInterfaceInBaseClass>();

            Assert.DoesNotThrow(typeWithInterfaceInBaseClass.Foo);
            Assert.IsTrue(typeWithInterfaceInBaseClass.GetType().Name.Equals("TypeWithInterfaceInBaseClassProxy"));
        }
Пример #8
0
        public void Autofac_Supports_Types_Without_Interfaces()
        {
            var builder = new ContainerBuilder();

            SnapConfiguration.For(new AutofacAspectContainer(builder)).Configure(c =>
            {
                c.IncludeNamespace("SnapTests.Fakes*");
                c.Bind <HandleErrorInterceptor>().To <HandleErrorAttribute>();
            });

            builder.Register(r => new DummyDependency()).As <IDependency>();
            builder.RegisterType(typeof(TypeWithoutInterface));
            builder.RegisterType(typeof(TypeWithInterfaceInBaseClass));

            using (var container = builder.Build())
            {
                var typeWithoutInterface = container.Resolve <TypeWithoutInterface>();
                Assert.DoesNotThrow(typeWithoutInterface.Foo);
                Assert.IsTrue(typeWithoutInterface.GetType().Name.Equals("TypeWithoutInterfaceProxy"));

                var typeWithInterfaceInBaseClass = container.Resolve <TypeWithInterfaceInBaseClass>();
                Assert.DoesNotThrow(typeWithInterfaceInBaseClass.Foo);
                Assert.IsTrue(typeWithInterfaceInBaseClass.GetType().Name.Equals("TypeWithInterfaceInBaseClassProxy"));
            }
        }
 public static void Config(string includeNamespace)
 {
     SnapConfiguration.For<StructureMapAspectContainer>(c =>
     {
         c.IncludeNamespace(includeNamespace);
         c.Bind<CachingInterceptor>().To<CachingAttribute>();
     });
 }
        static SampleStructureMapAopConfig()
        {
            SnapConfiguration.For <StructureMapAspectContainer>(c =>
            {
                c.IncludeNamespace("ConsoleApplication1*");
                c.Bind <SampleInterceptor>().To <SampleAttribute>();
            });

            ObjectFactory.Configure(c => c.For <ISampleClass>().Use <SampleClass>());
        }
Пример #11
0
        public void Namespace_Inclusion_Checks_For_Wildcards()
        {
            var container = new MockAspectContainer();

            SnapConfiguration.For(container).Configure(c => c.IncludeNamespace("SnapTests*"));

            var config = container.AspectConfiguration;

            Assert.IsTrue(config.Namespaces[0].Equals("SnapTests*"));
        }
Пример #12
0
        public void Structuremap_Container_Succeeds_Without_Match()
        {
            SnapConfiguration.For <StructureMapAspectContainer>(c =>
            {
                c.IncludeNamespace("Does.Not.Work");
                c.Bind <HandleErrorInterceptor>().To <HandleErrorAttribute>();
            });

            ObjectFactory.Configure(c => c.For <IBadCode>().Use <BadCode>());
            Assert.DoesNotThrow(() => ObjectFactory.GetInstance <IBadCode>());
        }
Пример #13
0
 internal static void RegisterSnap(string nameSpace)
 {
     if (!nameSpace.IsNullOrEmpty() && ConfigReservedKeys.SNAP_CACHE.ConfigValue().ToBoolean())
     {
         SnapConfiguration.For <StructureMapAspectContainer>(c =>
         {
             c.IncludeNamespace(nameSpace);
             c.Bind <ServiceCacheInterceptor>().To <ServiceCacheAttribute>();
         });
     }
 }
Пример #14
0
        public void Structuremap_Container_Allow_Strict_Matching()
        {
            SnapConfiguration.For <StructureMapAspectContainer>(c =>
            {
                c.IncludeNamespace("SnapTests*");
                c.Bind <HandleErrorInterceptor>().To <HandleErrorAttribute>();
            });

            ObjectFactory.Configure(c => c.For <IBadCode>().Use <BadCode>());
            Assert.DoesNotThrow(() => ObjectFactory.GetInstance <IBadCode>());
        }
Пример #15
0
        static SampleAutofacAopConfig()
        {
            _builder = new ContainerBuilder();

            SnapConfiguration.For(new AutofacAspectContainer(_builder)).Configure(c =>
            {
                c.IncludeNamespace("ConsoleApplication1");
                c.Bind <SampleInterceptor>().To <SampleAttribute>();
            });

            _builder.Register(r => new SampleClass()).As <ISampleClass>();
        }
Пример #16
0
        public void Structuremap_Container_Ignores_Types_Without_Decoration()
        {
            SnapConfiguration.For <StructureMapAspectContainer>(c =>
            {
                c.IncludeNamespace("Snap.Tests*");
                c.Bind <FirstInterceptor>().To <FirstAttribute>();
            });

            ObjectFactory.Configure(c => c.For <INotInterceptable>().Use <NotInterceptable>());
            var code = ObjectFactory.GetInstance <INotInterceptable>();

            Assert.IsFalse(code.GetType().Name.Equals("INotInterceptableProxy"));
        }
Пример #17
0
        public void Namespace_Inclusion_Adds_Wildcards()
        {
            var container = new NinjectAspectContainer();

            SnapConfiguration.For(container).Configure(c =>
            {
                c.IncludeNamespace("SnapTests");
                c.Bind <HandleErrorInterceptor>().To <HandleErrorAttribute>();
            });

            container.Kernel.Bind <IBadCode>().To <BadCode>();
            Assert.DoesNotThrow(() => container.Kernel.Get <IBadCode>());
        }
Пример #18
0
        public void Type_Inclusion_Adds_Type_By_Generic()
        {
            var container = new NinjectAspectContainer();

            SnapConfiguration.For(container).Configure(c =>
            {
                c.IncludeType <IBadCode>();
                c.Bind <HandleErrorInterceptor>().To <HandleErrorAttribute>();
            });

            container.Kernel.Bind <IBadCode>().To <BadCode>();
            Assert.DoesNotThrow(() => container.Kernel.Get <IBadCode>());
        }
Пример #19
0
        public void Class_Interceptors_Does_Not_Run_With_Multicast()
        {
            SnapConfiguration.For <StructureMapAspectContainer>(c =>
            {
                c.IncludeNamespace("SnapTests.Fakes*");
                c.Bind <HandleErrorInterceptor>().To <PublicClassAttribute>();
            });

            ObjectFactory.Configure(c => c.For <BaseAbstractClass>().Use <TypeWithAbstractClass>());
            var code = ObjectFactory.GetInstance <BaseAbstractClass>();

            Assert.Throws <Exception>(code.CallFoo2);
        }
Пример #20
0
        public void Snap_Scans_Assemblies_For_Default_Conventions()
        {
            SnapConfiguration.For <StructureMapAspectContainer>(c =>
            {
                c.IncludeNamespace("SnapTest*");
                c.Scan(s => s.ThisAssembly().WithDefaults());
            });

            ObjectFactory.Configure(c => c.For <IBadCode>().Use <BadCode>());
            var code = ObjectFactory.GetInstance <IBadCode>();

            Assert.DoesNotThrow(code.GiddyUp);
        }
Пример #21
0
        public void LinFu_Should_Fail_To_Resolve_Component_Which_Is_Not_Registered_With_ServiceNotFoundException()
        {
            var container = new ServiceContainer();

            SnapConfiguration.For(new LinFuAspectContainer(container)).Configure(c =>
            {
                c.IncludeNamespace("SnapTests.*");
                c.Bind <FirstInterceptor>().To <FirstAttribute>();
                c.Bind <SecondInterceptor>().To <SecondAttribute>();
            });

            Assert.Throws <ServiceNotFoundException>(() => container.GetService <IOrderedCode>());
        }
Пример #22
0
        public void StructureMap_Container_Supports_Method_Aspects()
        {
            SnapConfiguration.For <StructureMapAspectContainer>(c =>
            {
                c.IncludeNamespace("SnapTests.Fakes*");
                c.Bind <HandleErrorInterceptor>().To <HandleErrorAttribute>();
            });

            ObjectFactory.Configure(c => c.For <IBadCode>().Use <BadCode>());
            var badCode = ObjectFactory.GetInstance <IBadCode>();

            Assert.DoesNotThrow(badCode.GiddyUp);
            Assert.IsTrue(badCode.GetType().Name.Equals("IBadCodeProxy"));
        }
Пример #23
0
        public void LinFu_Container_Allow_Strict_Matching()
        {
            var container = new ServiceContainer();

            container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "*.dll");

            SnapConfiguration.For(new LinFuAspectContainer(container)).Configure(c =>
            {
                c.IncludeNamespace("SnapTests.*");
                c.Bind <HandleErrorInterceptor>().To <HandleErrorAttribute>();
            });

            Assert.DoesNotThrow(() => container.GetService <IBadCode>());
        }
Пример #24
0
        public void SimpleInjector_Container_Fails_Without_Match()
        {
            var container = new SimpleInjectorAspectContainer(new Container());

            SnapConfiguration.For(container).Configure(c =>
            {
                c.IncludeNamespace("Does.Not.Work");
                c.Bind <HandleErrorInterceptor>().To <HandleErrorAttribute>
                    ();
            });

            container.Container.Register <IBadCode, BadCode>();
            Assert.Throws <ActivationException>(() => container.Container.GetInstance <IBadCode>());
        }
Пример #25
0
        public void LinFu_Container_Fails_Without_Match()
        {
            var container = new ServiceContainer();

            container.LoadFrom(AppDomain.CurrentDomain.BaseDirectory, "*.dll");

            SnapConfiguration.For(new LinFuAspectContainer(container)).Configure(c =>
            {
                c.IncludeNamespace("Does.Not.Work");
                c.Bind <HandleErrorInterceptor>().To <HandleErrorAttribute>();
            });

            Assert.Throws <NullReferenceException>(() => container.GetService <IBadCode>());
        }
Пример #26
0
        public void Ninject_Container_Allow_Strict_Matching()
        {
            var container = new NinjectAspectContainer();

            SnapConfiguration.For(container).Configure(c =>
            {
                c.IncludeNamespace("SnapTests.Fakes.IBadCode");
                c.Bind <HandleErrorInterceptor>().To <HandleErrorAttribute>
                    ();
            });

            container.Kernel.Bind <IBadCode>().To <BadCode>();
            Assert.DoesNotThrow(() => container.Kernel.Get <IBadCode>());
        }
Пример #27
0
        public void SimpleInjector_Container_Allow_Strict_Matching()
        {
            var container = new SimpleInjectorAspectContainer(new Container());

            SnapConfiguration.For(container).Configure(c =>
            {
                c.IncludeNamespace("SnapTests.Fakes.IBadCode");
                c.Bind <HandleErrorInterceptor>().To <HandleErrorAttribute>
                    ();
            });

            container.Container.Register <IBadCode, BadCode>();
            Assert.DoesNotThrow(() => container.Container.GetInstance <IBadCode>());
        }
Пример #28
0
        public void Namespace_Inclusion_Adds_Namespace_and_Nested_Namespaces_Of_A_Given_Type()
        {
            var container = new NinjectAspectContainer();

            SnapConfiguration.For(container).Configure(c =>
            {
                // AspectConfiguration have the same namespace prefix as IBadCode's namespace
                c.IncludeNamespaceOf <AspectConfiguration>(true);
                c.Bind <HandleErrorInterceptor>().To <HandleErrorAttribute>();
            });

            container.Kernel.Bind <IBadCode>().To <BadCode>();
            Assert.DoesNotThrow(() => container.Kernel.Get <IBadCode>());
        }
Пример #29
0
        public void Ninject_Container_Fails_Without_Match()
        {
            var container = new NinjectAspectContainer();

            SnapConfiguration.For(container).Configure(c =>
            {
                c.IncludeNamespace("Does.Not.Work");
                c.Bind <HandleErrorInterceptor>().To <HandleErrorAttribute>
                    ();
            });

            container.Kernel.Bind <IBadCode>().To <BadCode>();
            Assert.Throws <NullReferenceException>(() => container.Kernel.Get <IBadCode>());
        }
Пример #30
0
        public void Snap_Allows_Custom_Scanning_Conventions()
        {
            var scanner = new CustomPrefixScanner();

            SnapConfiguration.For <StructureMapAspectContainer>(c =>
            {
                c.IncludeNamespace("SnapTest*");
                c.Scan(s => s.ThisAssembly().With(scanner));
            });

            ObjectFactory.Configure(c => c.For <IBadCode>().Use <BadCode>());
            var code = ObjectFactory.GetInstance <IBadCode>();

            Assert.DoesNotThrow(code.GiddyUp);
        }