public void CanSetUpAPolicyWithExternallyConfiguredRulesAndHandlers() { IUnityContainer container = new UnityContainer(); container.AddNewExtension <Interception>(); container .Configure <Interception>() .AddPolicy("policy1") .AddMatchingRule("rule1") .AddCallHandler("handler1") .AddCallHandler("handler2").Interception.Container .RegisterType <IMatchingRule, AlwaysMatchingRule>("rule1") .RegisterType <ICallHandler, GlobalCountCallHandler>( "handler1", new InjectionConstructor("handler1")) .RegisterType <ICallHandler, GlobalCountCallHandler>( "handler2", new InjectionConstructor("handler2"), new InjectionProperty("Order", 10)); GlobalCountCallHandler.Calls.Clear(); container .Configure <Interception>() .SetInterceptorFor <Wrappable>("wrappable", new VirtualMethodInterceptor()); Wrappable wrappable1 = container.Resolve <Wrappable>("wrappable"); wrappable1.Method2(); Assert.AreEqual(1, GlobalCountCallHandler.Calls["handler1"]); Assert.AreEqual(1, GlobalCountCallHandler.Calls["handler2"]); }
public void CanSetUpAPolicyWithExternallyConfiguredRulesAndHandlers() { IUnityContainer container = new UnityContainer(); container.AddNewExtension<Interception>(); container .Configure<Interception>() .AddPolicy("policy1") .AddMatchingRule("rule1") .AddCallHandler("handler1") .AddCallHandler("handler2").Interception.Container .RegisterType<IMatchingRule, AlwaysMatchingRule>("rule1") .RegisterType<ICallHandler, GlobalCountCallHandler>( "handler1", new InjectionConstructor("handler1")) .RegisterType<ICallHandler, GlobalCountCallHandler>( "handler2", new InjectionConstructor("handler2"), new InjectionProperty("Order", 10)); GlobalCountCallHandler.Calls.Clear(); container .Configure<Interception>() .SetInterceptorFor<Wrappable>("wrappable", new VirtualMethodInterceptor()); Wrappable wrappable1 = container.Resolve<Wrappable>("wrappable"); wrappable1.Method2(); Assert.AreEqual(1, GlobalCountCallHandler.Calls["handler1"]); Assert.AreEqual(1, GlobalCountCallHandler.Calls["handler2"]); }
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); }
public void CanSetUpAPolicyWithNonGenericInjectedRulesAndHandlers() { IUnityContainer container = new UnityContainer(); container.AddNewExtension <Interception>(); container .Configure <Interception>() .AddPolicy("policy1") .AddMatchingRule(typeof(AlwaysMatchingRule)) .AddCallHandler( typeof(GlobalCountCallHandler), new InjectionConstructor("handler1")) .AddCallHandler( typeof(GlobalCountCallHandler), new InjectionConstructor("handler2"), new InjectionProperty("Order", 10)); GlobalCountCallHandler.Calls.Clear(); container .Configure <Interception>() .SetInterceptorFor <Wrappable>("wrappable", new TransparentProxyInterceptor()); Wrappable wrappable1 = container.Resolve <Wrappable>("wrappable"); wrappable1.Method2(); Assert.AreEqual(1, GlobalCountCallHandler.Calls["handler1"]); Assert.AreEqual(1, GlobalCountCallHandler.Calls["handler2"]); }
public void CanSetUpAPolicyWithLifetimeManagedNamedInjectedRulesAndHandlers() { IUnityContainer container = new UnityContainer(); container.AddNewExtension<Interception>(); container .Configure<Interception>() .AddPolicy("foo") .AddMatchingRule<AlwaysMatchingRule>( "rule1", new ContainerControlledLifetimeManager()) .AddCallHandler<CallCountHandler>( "handler1", (LifetimeManager)null) .AddCallHandler<CallCountHandler>( "handler2", new ContainerControlledLifetimeManager(), new InjectionProperty("Order", 10)); GlobalCountCallHandler.Calls.Clear(); container .Configure<Interception>() .SetInterceptorFor<Wrappable>("wrappable", new TransparentProxyInterceptor()); Wrappable wrappable1 = container.Resolve<Wrappable>("wrappable"); wrappable1.Method2(); CallCountHandler handler1 = (CallCountHandler)container.Resolve<ICallHandler>("handler1"); CallCountHandler handler2 = (CallCountHandler)container.Resolve<ICallHandler>("handler2"); Assert.AreEqual(0, handler1.CallCount); // not lifetime maanaged Assert.AreEqual(1, handler2.CallCount); // lifetime managed }
public void CanSetUpAPolicyWithGivenRulesAndHandlers() { IUnityContainer container = new UnityContainer(); container.AddNewExtension <Interception>(); IMatchingRule rule1 = new AlwaysMatchingRule(); ICallHandler handler1 = new CallCountHandler(); container .Configure <Interception>() .AddPolicy("policy1") .AddMatchingRule(rule1) .AddCallHandler(handler1); container .Configure <Interception>() .SetInterceptorFor <Wrappable>("wrappable", new VirtualMethodInterceptor()); Wrappable wrappable1 = container.Resolve <Wrappable>("wrappable"); wrappable1.Method2(); Assert.AreEqual(1, ((CallCountHandler)handler1).CallCount); }
public void AddingTheInterceptionExtensionIsRequiredToExposeInterceptionFunctionality() { IUnityContainer uc = new UnityContainer(); Assert.IsNull(uc.Configure <Interception>()); uc.AddNewExtension <Interception>(); Assert.IsNotNull(uc.Configure <Interception>()); }
public static void PolicyExample() { Console.Clear(); using (var container = new UnityContainer()) { container.AddNewExtension <Microsoft.Practices.Unity.InterceptionExtension.Interception>(); container.RegisterType <IKundeBi, KundeBi>( new InterceptionBehavior <PolicyInjectionBehavior>(), new Interceptor <InterfaceInterceptor>()); container.RegisterType <IProduktBi, ProduktBi>( new InterceptionBehavior <PolicyInjectionBehavior>(), new Interceptor <InterfaceInterceptor>()); container.RegisterType <IConfigBi, ConfigBi>( new InterceptionBehavior <PolicyInjectionBehavior>(), new Interceptor <InterfaceInterceptor>()); container.Configure <Microsoft.Practices.Unity.InterceptionExtension.Interception>() .AddPolicy("log") .AddMatchingRule <AssemblyMatchingRule>( new InjectionConstructor( new InjectionParameter("PolicyInjection"))) .AddCallHandler <LogHandler>( new ContainerControlledLifetimeManager(), new InjectionConstructor(), new InjectionProperty("Order", 1)); container.Configure <Microsoft.Practices.Unity.InterceptionExtension.Interception>() .AddPolicy("test") .AddMatchingRule <MemberNameMatchingRule>( new InjectionConstructor(new[] { "Load*" }, true)) .AddMatchingRule <NamespaceMatchingRule>( new InjectionConstructor("PolicyInjection.Bi", true)) .AddCallHandler <TestHandler>( new ContainerControlledLifetimeManager(), new InjectionConstructor(), new InjectionProperty("Order", 2)); var kundeBi = container.Resolve <IKundeBi>(); kundeBi.LoadKunde(1); kundeBi.SaveKunde(1); var productBi = container.Resolve <IProduktBi>(); productBi.LoadProdukt("P1"); productBi.SaveProdukt("P1"); var configBi = container.Resolve <IConfigBi>(); configBi.LoadConfig(); configBi.SaveConfig(); Console.ReadLine(); } }
public static IServiceProvider ConfigureServices(IServiceCollection services) { var unity = new UnityContainer(); unity.Configure(services); return(new ServiceProvider(unity)); }
public void UnityCanResolveEnumerableOfLazyTypesRegisteredInUnityTest() { // Setup var unityContainer = new UnityContainer(); var assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); // Add composition support for unity unityContainer.AddExtension(new CompositionIntegration(true)); unityContainer.Configure <CompositionIntegration>().Catalogs.Add(assemblyCatalog); UnityComponent1.InstanceCount = 0; unityContainer.RegisterType <IUnityComponent, UnityComponent1>(); unityContainer.RegisterType <IUnityComponent, UnityComponent2>("component2"); var collectionOfLazyUnityComponents = unityContainer.Resolve <IEnumerable <Lazy <IUnityComponent> > >(); Assert.That(collectionOfLazyUnityComponents, Is.Not.Null); Assert.That(UnityComponent1.InstanceCount, Is.EqualTo(0)); var list = new List <Lazy <IUnityComponent> >(collectionOfLazyUnityComponents); Assert.That(UnityComponent1.InstanceCount, Is.EqualTo(0)); Assert.That(list[0].Value, Is.Not.Null); Assert.That(list[1].Value, Is.Not.Null); Assert.That(UnityComponent1.InstanceCount, Is.EqualTo(1)); Assert.That(list.Count, Is.EqualTo(2)); }
public void UnityCanResolveEnumerableOfTypesRegisteredInUnityAndMefTest() { // Setup var unityContainer = new UnityContainer(); var assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); MixedComponent1.InstanceCount = 0; MixedComponent2.InstanceCount = 0; MixedComponent5.InstanceCount = 0; // Add composition support for unity unityContainer.AddExtension(new CompositionIntegration(true)); unityContainer.Configure <CompositionIntegration>().Catalogs.Add(assemblyCatalog); unityContainer.RegisterType <IMixedComponent, MixedComponent1>("component1"); unityContainer.RegisterType <IMixedComponent, MixedComponent2>("component2"); unityContainer.RegisterType <IMixedComponent, MixedComponent3>(); var collectionOfLazyUnityComponents = unityContainer.Resolve <IEnumerable <IMixedComponent> >(); Assert.That(collectionOfLazyUnityComponents, Is.Not.Null); Assert.That(MixedComponent1.InstanceCount, Is.EqualTo(1)); Assert.That(MixedComponent2.InstanceCount, Is.EqualTo(1)); Assert.That(MixedComponent5.InstanceCount, Is.EqualTo(1)); var list = new List <IMixedComponent>(collectionOfLazyUnityComponents); Assert.That(list.Count, Is.EqualTo(5)); Assert.That(list.OfType <MixedComponent1>().Count(), Is.EqualTo(1)); Assert.That(list.OfType <MixedComponent2>().Count(), Is.EqualTo(1)); Assert.That(list.OfType <MixedComponent3>().Count(), Is.EqualTo(1)); Assert.That(list.OfType <MixedComponent4>().Count(), Is.EqualTo(1)); Assert.That(list.OfType <MixedComponent5>().Count(), Is.EqualTo(1)); }
public static void EnterpriseLibraryExample() { Console.Clear(); using (var container = new UnityContainer()) { var configurationSource = ConfigurationSourceFactory.Create(); var logWriterFactory = new LogWriterFactory(configurationSource); Logger.SetLogWriter(logWriterFactory.Create()); container.AddNewExtension <Microsoft.Practices.Unity.InterceptionExtension.Interception>(); container.RegisterType <IKundeBi, KundeBi>( new InterceptionBehavior <PolicyInjectionBehavior>(), new Interceptor <InterfaceInterceptor>()); container.Configure <Microsoft.Practices.Unity.InterceptionExtension.Interception>() .AddPolicy("log") .AddMatchingRule <AssemblyMatchingRule>( new InjectionConstructor( new InjectionParameter("PolicyInjection"))) .AddCallHandler <LogCallHandler>( new ContainerControlledLifetimeManager(), new InjectionConstructor(9001, true, true, "start", "finish", true, false, true, 10, 1)); var kundeBi = container.Resolve <IKundeBi>(); kundeBi.LoadKunde(1); kundeBi.SaveKunde(1); Console.ReadLine(); } }
public void ConfigureContainerwithNullPolicy() { IUnityContainer container = new UnityContainer(); container.AddNewExtension <Interception>(); container.Configure <Interception>().AddPolicy(null); }
public void ConfigureContainerwithNoPolicy() { IUnityContainer container = new UnityContainer(); container.AddNewExtension <Interception>(); container.Configure <Interception>().AddPolicy(String.Empty); }
public void UnityCanResolveMefComponentRegisteredUsingAddExportedValueTest() { MefSingletonComponent.Counter = 0; // Setup var unityContainer = new UnityContainer(); var assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); // Register catalog and types unityContainer.RegisterCatalog(assemblyCatalog); var compositionContainer = unityContainer.Configure <CompositionIntegration>().CompositionContainer; var batch = new CompositionBatch(); var singletonComponent = new MefSingletonComponent(); batch.AddExportedValue(singletonComponent); compositionContainer.Compose(batch); var singletonComponent1 = compositionContainer.GetExport <MefSingletonComponent>().Value; Assert.That(MefSingletonComponent.Counter, Is.EqualTo(1)); Assert.That(singletonComponent1, Is.SameAs(singletonComponent)); var singletonComponent2 = unityContainer.Resolve <MefSingletonComponent>(); Assert.That(MefSingletonComponent.Counter, Is.EqualTo(1)); Assert.That(singletonComponent2, Is.SameAs(singletonComponent)); }
// AOP with Unity //https://www.davidbreyer.com/programming/2015/07/01/aop-using-unity-interceptors-in-a-web-api-project/ static void Main(string[] args) { using (var container = new UnityContainer().AddNewExtension <Interception>()) { //container.RegisterType<IRepository<Customer>, Repository<Customer>>(new HierarchicalLifetimeManager(), new Interceptor<InterfaceInterceptor>(), new InterceptionBehavior<DumpInterceptor>()); container.RegisterType <IRepository <Customer>, Repository <Customer> >(); //new ContainerControlledLifetimeManager() container.Configure <Interception>().SetInterceptorFor <IRepository <Customer> >(new TransparentProxyInterceptor()); var customerRepository = container.Resolve <IRepository <Customer> >(); var customer = new Customer { Id = 1, Name = "Customer 1", Address = "Address 1" }; var temp = customerRepository.GetById(123); customerRepository.Add(customer); customerRepository.Update(customer); customerRepository.Delete(customer); } Console.WriteLine("\r\nEnd program\r\n***"); Console.ReadLine(); }
public void CanConfigureRemotingInterceptionOnInterface() { IUnityContainer container = new UnityContainer(); container.AddNewExtension<Interception>(); container.Configure<Interception>().SetInterceptorFor<Interface>(new TransparentProxyInterceptor()); }
public void TestDirectAOPWithPIAB() { var container = new UnityContainer(); container.RegisterType <IEmployeeBusinessObject, EmployeeBusinessObject>(new InjectionConstructor()); container.AddNewExtension <Interception>(); container.AddNewExtension <EnterpriseLibraryCoreExtension>(); container.Configure <Interception>().SetInterceptorFor <IEmployeeBusinessObject>( new TransparentProxyInterceptor()); // Get Policy Injection Settings from the Configuration IConfigurationSource configSource = ConfigurationSourceFactory.Create(); var policyInjectionsettings = (PolicyInjectionSettings)configSource.GetSection(PolicyInjectionSettings.SectionName); if (policyInjectionsettings != null) { policyInjectionsettings.ConfigureContainer(container, configSource); } var ebo = container.Resolve <IEmployeeBusinessObject>(); Assert.NotNull(ebo); }
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(); } }
/* protected void Application_Stop() * { * StopTasks(); * // SqlDependency.Stop(GetSpecialistWebConnectionString()); * * }*/ protected virtual void InitializeContainer() { if (Container == null) { Container = new UnityContainer(); UnityRegistrator.RegisterControls(Container, typeof(HomeController).Assembly); UnityRegistrator.RegisterServices(Container); Container .RegisterType <ISimplePageVMService, SimplePageVMService>() .RegisterInstance(typeof(IMetaDataProvider), MetaDataProvider) .RegisterType <IAnnounceService, AnnounceService>() .RegisterType <ISectionVMService, SectionVMService>() .RegisterType <ICourseVMService, CourseVMService>(); Container.Configure <Interception>() .SetDefaultInterceptorFor <CourseVMService>( new VirtualMethodInterceptor()) .SetDefaultInterceptorFor <SuperJobService>( new VirtualMethodInterceptor()); ValidatorRegistrator.Register(Container); ControllerBuilder.Current. SetControllerFactory(typeof(UnityControllerFactory)); } }
public void CanAddExtensionWithNonDefaultConstructor() { IUnityContainer container = new UnityContainer(); container.AddNewExtension<ContainerExtensionWithNonDefaultConstructor>(); var extension = container.Configure(typeof (ContainerExtensionWithNonDefaultConstructor)); Assert.IsNotNull(extension); }
private void Setup(TfsDriveParameter dynamicParameters) { Container = new UnityContainer(); Container.AddNewExtension <Interception>(); //Container.RegisterType<IContainer, TfsDrive>(new TransientLifetimeManager(), new InterceptionBehavior<PolicyInjectionBehavior>(), new Interceptor<InterfaceInterceptor>()); Container.RegisterType <IContainer, TfsDrive>(new TransientLifetimeManager()); Container.RegisterType <CachingLifetimeManager>(new ContainerControlledLifetimeManager()); if (null != dynamicParameters.DataService) { Container.RegisterInstance(dynamicParameters.DataService); } else { Container.RegisterType <ITfsDataService, TfsRestDataService>(new ContainerControlledLifetimeManager(), new InjectionConstructor( new InjectionParameter <string>(dynamicParameters.Url), new InjectionParameter <string>(dynamicParameters.AccessToken) ), new InterceptionBehavior <PolicyInjectionBehavior>(), new Interceptor <InterfaceInterceptor>()); } PolicyDefinition loggingPolicy = Container.Configure <Interception>().AddPolicy("logging"); loggingPolicy.AddMatchingRule <AssemblyMatchingRule>(new InjectionConstructor(new InjectionParameter(GetType().Assembly.FullName))); loggingPolicy.AddCallHandler <LoggingCallHandler>(new ContainerControlledLifetimeManager(), new InjectionConstructor()); loggingPolicy.AddCallHandler <PerformanceMeasurementHandler>(new ContainerControlledLifetimeManager(), new InjectionConstructor()); loggingPolicy.AddCallHandler <CachingCallHandler>(Container.Resolve <CachingLifetimeManager>(), new InjectionConstructor()); TfsDataService = Container.Resolve <ITfsDataService>(); }
public void ContainerCanWireEvents() { IUnityContainer container = new UnityContainer() .AddNewExtension <SimpleEventBrokerExtension>(); ClipboardManager clipboard = container.Resolve <ClipboardManager>(); EventBroker broker = container.Configure <ISimpleEventBrokerConfiguration>().Broker; List <string> registeredEvents = new List <string>(broker.RegisteredEvents); registeredEvents.Sort(); List <string> expectedEvents = new List <string>(new string[] { "cut", "copy", "paste", "clipboard data available" }); expectedEvents.Sort(); CollectionAssert.AreEqual(expectedEvents, registeredEvents); }
public void ConfigureInjectionForThrowsIfTypeIsNull() { IUnityContainer container = new UnityContainer(); container.Configure <InjectedMembers>() .ConfigureInjectionFor(null); }
public void UnityCanResolveEnumerableOfLazyTypeRegisteredInMefWithStronglyTypedMetadataTest() { //throw new NotImplementedException(); // Setup var unityContainer = new UnityContainer(); var assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); // Add composition support for unity unityContainer.AddExtension(new CompositionIntegration(false)); unityContainer.Configure <CompositionIntegration>().Catalogs.Add(assemblyCatalog); var lazyMefComponent = unityContainer.Resolve <IEnumerable <Lazy <IPartWithStronglyTypedMetadata, IMyStronglyTypedMetadataAttribute> > >().ToArray(); Assert.That(lazyMefComponent, Is.Not.Null); Assert.That(lazyMefComponent[0].Value, Is.Not.Null); Assert.That(lazyMefComponent[0].Metadata, Is.Not.Null); Assert.That(lazyMefComponent[0].Value.GetType(), Is.EqualTo(typeof(StronglyTypedHelloWorldDispatcher))); }
public void UnityCanResolveLazyTypeRegisteredInMefWithoutItsMetadataTest() { // Setup var unityContainer = new UnityContainer(); var assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly()); // Add composition support for unity unityContainer.AddExtension(new CompositionIntegration(false)); unityContainer.Configure <CompositionIntegration>().Catalogs.Add(assemblyCatalog); var lazyMefComponent = unityContainer.Resolve <Lazy <IPartWithTextMetadata> >(); Assert.That(lazyMefComponent, Is.Not.Null); Assert.That(lazyMefComponent.Value, Is.Not.Null); Assert.That(lazyMefComponent, Is.Not.Null); }
public void CanConfigureDefaultRemotingInterceptionOnMBRO() { IUnityContainer container = new UnityContainer(); container.AddNewExtension<Interception>(); container.Configure<Interception>() .SetDefaultInterceptorFor<Wrappable>(new TransparentProxyInterceptor()); }
public void Should_return_container() { var container = new UnityContainer(); var result = container.Configure(x => { }); Assert.That(result, Is.SameAs(container)); }
public void ConfiguringConstructorThatTakesOpenGenericTypeDoesNotThrow() { IUnityContainer container = new UnityContainer(); container.Configure <InjectedMembers>() .ConfigureInjectionFor(typeof(LoggingCommand <>), new InjectionConstructor(new ResolvedParameter(typeof(ICommand <>), "concrete"))); }
public void Can_resolve_the_container_without_registering_it() { var container = new UnityContainer(); container.Configure(x => { }); Assert.That(container.Resolve <IUnityContainer>(), 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 static void RegisterContainer() { var container = new UnityContainer(); container.Configure <ProductViewModelService>(); DependencyResolver.SetResolver(new UnityDependencyResolver(container)); }
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 CanConfigureRemotingInterceptionOnInterface() { IUnityContainer container = new UnityContainer(); container.AddNewExtension <Interception>(); container.Configure <Interception>().SetInterceptorFor <Interface>(new TransparentProxyInterceptor()); }
public void CanGetExtensionConfigurationFromContainer() { IUnityContainer container = new UnityContainer() .AddNewExtension<StaticFactoryExtension>(); IStaticFactoryConfiguration config = container.Configure<IStaticFactoryConfiguration>(); Assert.IsNotNull(config); }
public void CanLookupExtensionByClassName() { MockContainerExtension extension = new MockContainerExtension(); IUnityContainer container = new UnityContainer(); container.AddExtension(extension); MockContainerExtension result = container.Configure<MockContainerExtension>(); Assert.AreSame(extension, result); }
public void 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); }
public void 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 SettingUpRuleWithEmptyNameThrows() { IUnityContainer container = new UnityContainer(); container.AddNewExtension<Interception>(); try { container.Configure<Interception>().AddPolicy(string.Empty); Assert.Fail("should have thrown"); } catch (ArgumentException) { } }
public void ConfiguringRemotingInterceptionOnNonMBROTypeThrows() { IUnityContainer container = new UnityContainer(); container.AddNewExtension<Interception>(); try { container.Configure<Interception>() .SetInterceptorFor<WrappableThroughInterface>(new TransparentProxyInterceptor()); Assert.Fail("Call to SetInjectorFor<T>() should have thrown"); } catch (ArgumentException) { // expected exception } }
public void CanSetUpAnEmptyRule() { // there is no visible effect for this, but it should still be resolved. IUnityContainer container = new UnityContainer(); container.AddNewExtension<Interception>(); // empty container .Configure<Interception>() .AddPolicy("policy1"); List<InjectionPolicy> policies = new List<InjectionPolicy>(container.ResolveAll<InjectionPolicy>()); Assert.AreEqual(2, policies.Count); Assert.IsInstanceOfType(policies[0], typeof(AttributeDrivenPolicy)); Assert.IsInstanceOfType(policies[1], typeof(RuleDrivenPolicy)); Assert.AreEqual("policy1", policies[1].Name); }
public void CanSetUpInterceptorThroughInjectionMember() { CallCountHandler handler = new CallCountHandler(); IUnityContainer container = new UnityContainer(); container.AddNewExtension<Interception>(); container.Configure<Interception>() .AddPolicy("policy") .AddMatchingRule<AlwaysMatchingRule>() .AddCallHandler(handler); container.RegisterType<IInterface, BaseClass>( "test", new Interceptor<InterfaceInterceptor>(), new InterceptionBehavior<PolicyInjectionBehavior>()); IInterface instance = container.Resolve<IInterface>("test"); instance.DoSomething("1"); Assert.AreEqual(1, handler.CallCount); }
public void SettingUpAPolicyWithANullRuleElementThrows() { IUnityContainer container = new UnityContainer(); container.AddNewExtension<Interception>(); try { container .Configure<Interception>() .AddPolicy("policy1") .AddMatchingRule(typeof(AlwaysMatchingRule)) .AddMatchingRule((string)null) .AddCallHandler(new CallCountHandler()); Assert.Fail("Should have thrown"); } catch (ArgumentException) { } }
public void CanSetUpAPolicyWithLifetimeManagedNamedInjectedRulesAndHandlers() { IUnityContainer container = new UnityContainer(); container.AddNewExtension<Interception>(); container .Configure<Interception>() .AddPolicy("policy1") .AddMatchingRule<AlwaysMatchingRule>( "rule1", new ContainerControlledLifetimeManager()) .AddCallHandler<CallCountHandler>( "handler1", (LifetimeManager)null) .AddCallHandler<CallCountHandler>( "handler2", new ContainerControlledLifetimeManager(), new InjectionProperty("Order", 10)); GlobalCountCallHandler.Calls.Clear(); container .Configure<Interception>() .SetInterceptorFor<Wrappable>("wrappable", new TransparentProxyInterceptor()); Wrappable wrappable1 = container.Resolve<Wrappable>("wrappable"); wrappable1.Method2(); CallCountHandler handler1 = (CallCountHandler)container.Resolve<ICallHandler>("handler1"); CallCountHandler handler2 = (CallCountHandler)container.Resolve<ICallHandler>("handler2"); Assert.AreEqual(0, handler1.CallCount); // not lifetime maanaged Assert.AreEqual(1, handler2.CallCount); // lifetime managed }
public void CanSetUpAPolicyWithNamedInjectedRulesAndHandlers() { IUnityContainer container = new UnityContainer(); container.AddNewExtension<Interception>(); container .Configure<Interception>() .AddPolicy("policy1") .AddMatchingRule<AlwaysMatchingRule>("rule1") .AddCallHandler<GlobalCountCallHandler>( "handler1", new InjectionConstructor("handler1")) .AddCallHandler<GlobalCountCallHandler>( "handler2", new InjectionConstructor("handler2"), new InjectionProperty("Order", 10)); GlobalCountCallHandler.Calls.Clear(); container .Configure<Interception>() .SetInterceptorFor<Wrappable>("wrappable", new TransparentProxyInterceptor()); Wrappable wrappable1 = container.Resolve<Wrappable>("wrappable"); wrappable1.Method2(); GlobalCountCallHandler handler1 = (GlobalCountCallHandler)container.Resolve<ICallHandler>("handler1"); GlobalCountCallHandler handler2 = (GlobalCountCallHandler)container.Resolve<ICallHandler>("handler2"); Assert.AreEqual(1, GlobalCountCallHandler.Calls["handler1"]); Assert.AreEqual(1, GlobalCountCallHandler.Calls["handler2"]); Assert.AreEqual(0, handler1.Order); Assert.AreEqual(10, handler2.Order); }
public void CanSetUpAPolicyWithGivenRulesAndHandlersTypesWithGenerics() { IUnityContainer container = new UnityContainer(); container.AddNewExtension<Interception>(); container .Configure<Interception>() .AddPolicy("policy1") .AddMatchingRule<AlwaysMatchingRule>() .AddCallHandler<GlobalCountCallHandler>(); GlobalCountCallHandler.Calls.Clear(); container .Configure<Interception>() .SetInterceptorFor<Wrappable>("wrappable", new TransparentProxyInterceptor()); Wrappable wrappable1 = container.Resolve<Wrappable>("wrappable"); wrappable1.Method2(); Assert.AreEqual(1, GlobalCountCallHandler.Calls["default"]); }
public void CanSetUpAPolicyWithGivenRulesAndHandlers() { IUnityContainer container = new UnityContainer(); container.AddNewExtension<Interception>(); IMatchingRule rule1 = new AlwaysMatchingRule(); ICallHandler handler1 = new CallCountHandler(); container .Configure<Interception>() .AddPolicy("policy1") .AddMatchingRule(rule1) .AddCallHandler(handler1); container .Configure<Interception>() .SetInterceptorFor<Wrappable>("wrappable", new TransparentProxyInterceptor()); Wrappable wrappable1 = container.Resolve<Wrappable>("wrappable"); wrappable1.Method2(); Assert.AreEqual(1, ((CallCountHandler)handler1).CallCount); }
private IUnityContainer CreateContainer(string globalCallHandlerName) { IUnityContainer container = new UnityContainer(); container.AddNewExtension<Interception>(); container.RegisterInstance<IMatchingRule>( "alwaystrue", new AlwaysMatchingRule()); container.RegisterInstance<ICallHandler>( "globalCountHandler", new GlobalCountCallHandler(globalCallHandlerName)); container.Configure<Interception>() .AddPolicy("policy") .AddMatchingRule("alwaystrue") .AddCallHandler("globalCountHandler"); return container; }
public void AttributeDrivenPolicyIsAddedByDefault() { GlobalCountCallHandler.Calls.Clear(); IUnityContainer container = new UnityContainer(); container.AddNewExtension<Interception>(); container.RegisterType<Interface, WrappableThroughInterfaceWithAttributes>(); container .Configure<Interception>() .SetInterceptorFor<Interface>(new TransparentProxyInterceptor()); Interface wrappedOverInterface = container.Resolve<Interface>(); wrappedOverInterface.Method(); Assert.AreEqual(1, GlobalCountCallHandler.Calls["WrappableThroughInterfaceWithAttributes-Method"]); }