Example #1
0
        public static void RegisterRepositoryTypes(UnityContainer container)
        {
            /* Register types into the container */
            container.RegisterType<IDataService, DataService>();

            //container.RegisterType<IDataService, DummyDataService>();

            /* Register the Logger to be used by all calls from the container */
            container.RegisterType<LogHelper, LogHelper>(new ContainerControlledLifetimeManager());

            /* Register our data access helper to be used uniquely for each thread call from container */
            container.RegisterType<IDataAccessor, DataAccessor>(new PerThreadLifetimeManager());

            /* Register our matching rule and call handlers (AKA Aspects) */
            container.RegisterType<IMatchingRule, AllMatchingRule>(MatchingRule);
            container.RegisterType<ICallHandler, ExceptionCallHandler>(ExceptionCallHandler.Key);
            container.RegisterType<ICallHandler, UnitOfWorkCallHandler>(UnitOfWorkCallHandler.Key);
            container.RegisterType<ICallHandler, AuthenticationCallHandler>(AuthenticationCallHandler.Key);
            container.RegisterType<ICallHandler, CommandCallHandler>(CommandCallHandler.Key);
            container.RegisterType<ICallHandler, LogCallHandler>(LogCallHandler.Key);

            /* Create a new policy and reference the matching rule and call handler by name */
            container.AddNewExtension<Interception>();
            container.Configure<Interception>()
                .AddPolicy(PolicyName)
                .AddMatchingRule(MatchingRule)
                .AddCallHandler(ExceptionCallHandler.Key)
                .AddCallHandler(UnitOfWorkCallHandler.Key)
                //.AddCallHandler(LogCallHandler.Key)
                .AddCallHandler(AuthenticationCallHandler.Key)
                .AddCallHandler(CommandCallHandler.Key);

            /* Make IDataService interface elegible for interception */
            container.Configure<Interception>().SetInterceptorFor(typeof(IDataService), new TransparentProxyInterceptor());
        }
Example #2
0
        public void InjectClassWithTwoConstructors()
        {
            int myInt = 37;
            string myStr = "Test";

            IUnityContainer container = new UnityContainer();

            //constructor without params
            container.Configure<InjectedMembers>().ConfigureInjectionFor<TestClass>(new InjectionConstructor());

            TestClass withOutCon = container.Resolve<TestClass>();
            Assert.IsFalse(withOutCon.StringConstructorCalled);
            Assert.IsFalse(withOutCon.IntConstructorCalled);
            //constructor with one param
            container.Configure<InjectedMembers>()
                .ConfigureInjectionFor<TestClass>("First",
                    new InjectionConstructor(myInt));

            TestClass myTestClass = container.Resolve<TestClass>("First");

            Assert.IsFalse(myTestClass.StringConstructorCalled);
            Assert.IsTrue(myTestClass.IntConstructorCalled);

            //constructor with one param
            container.Configure<InjectedMembers>()
               .ConfigureInjectionFor<TestClass>("Second",
                   new InjectionConstructor(myStr));

            TestClass myTestClass1 = container.Resolve<TestClass>("Second");
            
            Assert.IsFalse(myTestClass1.IntConstructorCalled);
            Assert.IsTrue(myTestClass1.StringConstructorCalled);
        }
Example #3
0
    static void UsingPIABWithContainer()
    {
      ConfigureLogger();
      using (var container = new UnityContainer())
      {

        container.AddNewExtension<Interception>();
        container.RegisterType<InterceptableTenantStore>(new Interceptor<TransparentProxyInterceptor>(), new InterceptionBehavior<PolicyInjectionBehavior>());

        container.Configure<Interception>().AddPolicy("logging")
             .AddMatchingRule<MemberNameMatchingRule>(
                new InjectionConstructor(
                new InjectionParameter("Save")))
             .AddCallHandler<LogCallHandler>(
                new ContainerControlledLifetimeManager(),
                new InjectionConstructor(
                  9001, true, false,
                  "This is before the method call",
                  "This is after the method call", false, false, true, 10, 1));


        var tenantStore = container.Resolve<InterceptableTenantStore>();

        // Use the interceptable type.
        Console.WriteLine("*** Invoking the Save method ***");
        tenantStore.Save();
        Console.WriteLine("*** Invoking the Modify method ***");
        tenantStore.Modify();
      }
    }
        public void Can_call_method_on_concrete_after_build_up()
        {
            var container = new UnityContainer();
            container.Configure(x => x.AfterBuildingUp<StartableService1>().Call((c, s) => s.Start()));

            Assert.That(container.Resolve<StartableService1>().StartWasCalled);
        }
        public void Can_register_named_singleton_instance()
        {
            var container = new UnityContainer();
            container.Configure(x => x.Register<IBarService, BarService>().WithName("name").AsSingleton());

            Assert.That(container.Resolve<IBarService>("name"), Is.SameAs(container.Resolve<IBarService>("name")));
        }
 public void ConfigureContainerbyAddingExteralPolicy()
 {
     IUnityContainer container = new UnityContainer();
     container.AddNewExtension<Interception>();
     container.Configure<Interception>().AddPolicy("myRDP");
     List<InjectionPolicy> policy = new List<InjectionPolicy>(container.ResolveAll<InjectionPolicy>());
     Assert.AreEqual("myRDP", policy[1].Name);
 }
        public void Can_resolve_the_container_without_registering_it()
        {
            var container = new UnityContainer();

            container.Configure(x => { });

            Assert.That(container.Resolve<IUnityContainer>(), Is.SameAs(container));
        }
Example #8
0
 public void ConfigureAndRegisterInstance()
 {
     IUnityContainer container = new UnityContainer().AddNewExtension<StaticFactoryExtension>();
     IStaticFactoryConfiguration config = container.Configure<IStaticFactoryConfiguration>();
     object obj1 = container.RegisterInstance<IStaticFactoryConfiguration>(config);
     Assert.IsNotNull(config);
     Assert.IsNotNull(obj1);
 }
        public void Should_return_container()
        {
            var container = new UnityContainer();

            var result = container.Configure(x => { });

            Assert.That(result, Is.SameAs(container));
        }
        public void Can_initalize_container_with_one_registry()
        {
            var container = new UnityContainer();

            container.Configure(x => x.AddRegistry<FooRegistry>());

            Assert.That(container.Resolve<IFooService>(), Is.InstanceOf<FooService>());
        }
        public void ValidationCallHandlerThrowsArgumentValidationExceptionIfValidationFailsFromConfiguration()
        {
            IUnityContainer factory = new UnityContainer().AddNewExtension<Interception>();
            factory.Configure<Interception>().SetDefaultInterceptorFor<ValidationFixtureTarget>(new TransparentProxyInterceptor());
            AddValidationPolicy(factory, string.Empty, SpecificationSource.Configuration, new TypeMatchingRule("ValidationFixtureTarget"));
            ValidationFixtureTarget target = factory.Resolve<ValidationFixtureTarget>();

            target.AcceptTest(new TestObject(false, true));
        }
        public void Can_register_a_func_with_parameters()
        {
            var container = new UnityContainer();

            var myService = new BarService();
            container.Configure(x => x.Register<Func<int, IBarService>>(c => i => myService));

            Assert.That(container.Resolve<Func<int, IBarService>>()(1), Is.SameAs(myService));
        }
        public void Finds_all_registries()
        {
            var container = new UnityContainer();

            container.Configure(x => x.AddRegistry<MyRegistry>());

            Assert.That(container.Resolve<IFooService>(), Is.InstanceOf<FooService>());
            Assert.That(container.Resolve<IBarService>(), Is.InstanceOf<BarService>());
        }
        public void Can_register_singleton()
        {
            var container = new UnityContainer();

            container.Configure(x => x.Register<IBarService, BarService>().AsSingleton());

            Assert.That(container.Resolve<IBarService>(), Is.InstanceOf<BarService>());
            Assert.That(container.Resolve<IBarService>(), Is.SameAs(container.Resolve<IBarService>()));
        }
        public void ValidationCallHandlerDoesNothingIfValidationPasses()
        {
            IUnityContainer factory = new UnityContainer().AddNewExtension<Interception>();
            factory.Configure<Interception>().SetDefaultInterceptorFor<ValidationFixtureTarget>(new TransparentProxyInterceptor());
            AddValidationPolicy(factory, string.Empty, SpecificationSource.Attributes, new TypeMatchingRule("ValidationFixtureTarget"));
            ValidationFixtureTarget target = factory.Resolve<ValidationFixtureTarget>();

            target.AcceptTest(new TestObject(false, false));
        }
        public void ConfigureContainerbyAddingPolicyHanlderRule()
        {
            ICallHandler myHandler1 = new CallCountHandler();
            IMatchingRule myMatchingRule1 = new AlwaysMatchingRule();

            IUnityContainer container = new UnityContainer();
            container.AddNewExtension<Interception>();
            container.Configure<Interception>().
                AddPolicy("myRDP").
                AddCallHandler(myHandler1).
                AddMatchingRule(myMatchingRule1);
            container.Configure<Interception>().
                SetInterceptorFor<InterceptionClass>("interceptionclass", new TransparentProxyInterceptor());

            InterceptionClass ic = container.Resolve<InterceptionClass>("interceptionclass");
            ic.MethodA();
            Assert.AreEqual(1, ((CallCountHandler)myHandler1).CallCount);
        }
        public void Can_register_using_factory_delegate()
        {
            var container = new UnityContainer();

            var myService = new BarService();
            container.Configure(x => x.Register<IBarService>(c => myService));

            Assert.That(container.Resolve<IBarService>(), Is.SameAs(myService));
        }
        public void Can_register_transient_type()
        {
            var container = new UnityContainer();

            container.Configure(x => x.Register<IBarService, BarService>());

            Assert.That(container.Resolve<IBarService>(), Is.InstanceOf<BarService>());
            Assert.That(container.Resolve<IBarService>(), Is.Not.SameAs(container.Resolve<IBarService>()));
        }
        public void ShouldRethrowFromNoOpPolicy()
        {
            IUnityContainer factory = new UnityContainer().AddNewExtension<Interception>();
            factory.Configure<Interception>().SetDefaultInterceptorFor<TargetType>(new TransparentProxyInterceptor());
            AddExceptionHandlingConfiguration(factory);
            AddExceptionPolicy(factory, "No-Op Policy", new TypeMatchingRule("TargetType"));

            TargetType target = factory.Resolve<TargetType>();
            target.ThrowFromFunctionWithReturnValue();
        }
        public void ShouldTranslateException()
        {
            IUnityContainer factory = new UnityContainer().AddNewExtension<Interception>();
            factory.Configure<Interception>().SetDefaultInterceptorFor<TargetType>(new TransparentProxyInterceptor());
            AddExceptionHandlingConfiguration(factory);
            AddExceptionPolicy(factory, "Translate Exceptions", new TypeMatchingRule("TargetType"));

            TargetType target = factory.Resolve<TargetType>();
            target.WillThrowException();
        }
 public void ConfigureContainerbyAddingEmptyHandlerRuleThrowException()
 {
     IUnityContainer container = new UnityContainer();
     container.AddNewExtension<Interception>();
     container.Configure<Interception>().
         AddPolicy("myRDP").
         AddCallHandler(String.Empty).
         AddMatchingRule(String.Empty);
     List<InjectionPolicy> policy = new List<InjectionPolicy>(container.ResolveAll<InjectionPolicy>());
 }
Example #22
0
 public void Ext_CanGetConfigurationWithoutGenericMethod()
 {
     MockContainerExtension extension = new MockContainerExtension();
     IUnityContainer container = new UnityContainer()
         .AddExtension(extension);
     IMockConfiguration config = (IMockConfiguration)container.Configure(typeof(IMockConfiguration));
     
     Assert.AreSame(extension, config);
     Assert.AreSame(container, config.Container);
 }
Example #23
0
        public void Ext_CanGetConfigurationInterfaceFromExtension()
        {
            MockContainerExtension extension = new MockContainerExtension();
            IUnityContainer container = new UnityContainer()
                .AddExtension(extension);
            IMockConfiguration config = container.Configure<IMockConfiguration>();

            Assert.AreSame(extension, config);
            Assert.AreSame(container, config.Container);
        }
        public void Can_call_method_on_interface_after_build_up()
        {
            var container = new UnityContainer();
            container.Configure(x =>
            {
                x.Register<IStartable, StartableService1>();
                x.AfterBuildingUp<IStartable>().Call((c, s) => s.Start());
            });

            Assert.That(container.Resolve<IStartable>().StartWasCalled);
        }
 public void ConfigureContainerbyAddingExteralPolicyHandlerRule()
 {
     IUnityContainer container = new UnityContainer();
     container.AddNewExtension<Interception>();
     container.Configure<Interception>().
         AddPolicy("myRDP").
         AddCallHandler("myHandler1").
         AddMatchingRule("myMatchingRule1")
         .Interception.Container.RegisterType<ICallHandler, CallCountHandler>("myHandler1")
         .RegisterType<IMatchingRule, AlwaysMatchingRule>("myMatchingRule1");
 }
        public void ShouldThrowWhenSwallowingExceptionFromNonVoidMethod()
        {
            IUnityContainer factory = new UnityContainer().AddNewExtension<Interception>();
            factory.Configure<Interception>().SetDefaultInterceptorFor<TargetType>(new TransparentProxyInterceptor());
            AddExceptionHandlingConfiguration();
            AddExceptionPolicy(factory, "Swallow Exceptions", new TypeMatchingRule("TargetType"));

            TargetType target = factory.Resolve<TargetType>();
            target.ThrowFromFunctionWithReturnValue();
            Assert.Fail("An exception should have been thrown");
        }
        public void Can_set_property_after_building_up()
        {
            var container = new UnityContainer();
            container.Configure(x =>
            {
                x.Register<ILogger, NullLogger>();
                x.AfterBuildingUp<FooService>().Call((c, s) => s.Logger = c.Resolve<ILogger>());
            });

            Assert.That(container.Resolve<FooService>().Logger, Is.Not.Null);
        }
        public void Registers_with_default_naming_convention()
        {
            var container = new UnityContainer();

            container.Configure(x => x.Scan(scan =>
            {
                scan.AssemblyContaining<NamedService>();
                scan.WithNamingConvention();
            }));

            Assert.That(container.Resolve<INamedService>(), Is.InstanceOf<NamedService>());
        }
        public void Can_add_same_registry_twice_without_breaking_anything()
        {
            var container = new UnityContainer();

            container.Configure(x =>
            {
                x.AddRegistry<FooRegistry>();
                x.AddRegistry<FooRegistry>();
            });

            Assert.That(container.Resolve<IFooService>(), Is.InstanceOf<FooService>());
        }
        public void Can_override_the_default_naming_convention()
        {
            var container = new UnityContainer();

            container.Configure(x => x.Scan(scan =>
            {
                scan.AssemblyContaining<FooRegistry>();
                scan.WithAddAllConvention().TypesImplementing<IHaveManyImplementations>().WithName(t => "test");
            }));

            Assert.That(container.Resolve<IHaveManyImplementations>("test"), Is.Not.Null);
        }