Ejemplo n.º 1
0
    public void ServiceLocaterTest()
    {
        ServInjector.Clear();

        //Serviceを継承した場合は自動で登録される
        var serv1 = new TestService1();

        Assert.AreEqual(ServInjector.Resolve <ITestService>(), serv1);
        //DisposeでUnbindされる
        serv1.Dispose();
        Assert.IsNull(ServInjector.Resolve <ITestService>());
        //手動で登録してみる
        ServInjector.Bind <ITestService>(serv1);
        Assert.AreEqual(ServInjector.Resolve <ITestService>(), serv1);
        //別のサービスをバインドする
        var serv2 = new TestService2();

        ServInjector.Bind <ITestService>(serv2);
        Assert.AreNotEqual(ServInjector.Resolve <ITestService>(), serv1);
        Assert.AreEqual(ServInjector.Resolve <ITestService>(), serv2);
        //service1はバインドされていないので解放しても結果は同じ
        serv1.Dispose();
        Assert.AreNotEqual(ServInjector.Resolve <ITestService>(), serv1);
        Assert.AreEqual(ServInjector.Resolve <ITestService>(), serv2);
        //手動で解放してみる
        ServInjector.Unbind <ITestService>(serv2);
        Assert.IsNull(ServInjector.Resolve <ITestService>());
    }
        public void Fire_OneMatchingFact_FiresOnceAndCallsDependency()
        {
            //Arrange
            var  service1       = new TestService1();
            bool service1Called = false;

            service1.ServiceCalled += (sender, args) => service1Called = true;

            var  service2       = new TestService2();
            bool service2Called = false;

            service2.ServiceCalled += (sender, args) => service2Called = true;

            Session.DependencyResolver = new TestDependencyResolver(service1, service2);

            var fact = new FactType {
                TestProperty = "Valid Value 1"
            };

            Session.Insert(fact);

            //Act
            Session.Fire();

            //Assert
            AssertFiredOnce();
            Assert.True(service1Called);
            Assert.True(service2Called);
        }
        public void Fire_OneMatchingFact_CanResolveDependencyFromContext()
        {
            //Arrange
            var service1 = new TestService1();
            var service2 = new TestService2();

            Session.DependencyResolver = new TestDependencyResolver(service1, service2);

            var fact = new FactType {
                TestProperty = "Valid Value 1"
            };

            Session.Insert(fact);

            ITestService1 resolvedService1 = null;

            GetRuleInstance <TestRule>().Action = ctx =>
            {
                resolvedService1 = ctx.Resolve <ITestService1>();
            };

            //Act
            Session.Fire();

            //Assert
            AssertFiredOnce();
            Assert.Same(service1, resolvedService1);
        }
Ejemplo n.º 4
0
    public void InjectInterfaceTest()
    {
        ServInjector.Clear();
        var serv1 = new TestService1();

        Assert.AreEqual(ServInjector.Resolve <ITestService>(), serv1);
        //インジェクトして作成
        var client = ServInjector.Create <TestClient>();

        Assert.AreEqual(client.Service, serv1);

        //インジェクト関数でも追加できる
        ServInjector.Bind <ITestService>(new TestService2());
        ServInjector.Inject(client);
        Assert.AreEqual(client.Service.Name, typeof(TestService2).Name);

        //型を指定した場合、その型でInjectされる
        var nest = ServInjector.Create <TestClientNest>(typeof(TestClient));

        Assert.AreEqual(nest.Service.Name, typeof(TestService2).Name);
        Assert.AreNotEqual(nest.Service, nest.Service2);
        Assert.AreNotEqual(nest.Service, nest.Service3);

        //型を指定していないので継承先のプロパティ・メソッドにインジェクトされる
        ServInjector.Inject(nest);
        Assert.AreEqual(nest.Service, nest.Service2);
        Assert.AreEqual(nest.Service, nest.Service3);
    }
        public void RemoveServices()
        {
            IServiceContainer container = new ServiceContainer(null);
            TestService1      svc1      = new TestService1();
            TestService1      svc1_     = new TestService1();
            TestService2      svc2      = new TestService2();

            container.RegisterService(svc1);
            container.RegisterService(svc2);
            container.RegisterService("svc1_", svc1_);
            Assert.AreEqual(svc1, container.GetService <IService1>());
            Assert.AreEqual(svc1_, container.GetService <IService1>("svc1_"));
            Assert.AreEqual(svc2, container.GetService <IService2>());

            container.UnregisterService(svc1);
            Assert.AreEqual(svc1_, container.GetService <IService1>());
            Assert.AreEqual(svc1_, container.GetService <IService1>("svc1_"));
            Assert.AreEqual(svc2, container.GetService <IService2>());
            container.UnregisterService(svc1_);
            Assert.AreEqual(null, container.GetService <IService1>());
            Assert.AreEqual(null, container.GetService <IService1>("svc1_"));
            Assert.AreEqual(svc2, container.GetService <IService2>());
            container.UnregisterService(svc2);
            Assert.AreEqual(null, container.GetService <IService2>());
        }
Ejemplo n.º 6
0
 private void Method3(TestService1 service1)
 {
     if (service1 is null)
     {
         throw new ArgumentNullException(nameof(service1));
     }
     service1.Success = true;
 }
Ejemplo n.º 7
0
 public bool Method2(TestService1 service1)
 {
     if (service1 is null)
     {
         throw new ArgumentNullException(nameof(service1));
     }
     return(service1.Success = true);
 }
Ejemplo n.º 8
0
        public void GetServiceNotByInterfaceType()
        {
            IServiceContainer container = new ServiceContainer(null);
            TestService1      svc1      = new TestService1();

            container.RegisterService(svc1);
            container.GetService <TestService1>();
        }
        public void GetServiceNotByInterfaceType()
        {
            IServiceContainer container = new ServiceContainer(null);
            TestService1      svc1      = new TestService1();

            container.RegisterService(svc1);
            Assert.Throws <ArgumentException>(() => { container.GetService <TestService1>(); });
        }
        public void RegisterByNullKey()
        {
            var child   = new TestSupportServices();
            var service = new TestService1();

            child.ServiceContainer.RegisterService(null, service);
            Assert.AreEqual(service, child.ServiceContainer.GetService <IService1>());
        }
        public void WhenTryToRegisterMultipleTimesTheSameService_ThrowAnException()
        {
            var serviceLocator = new Services.ServiceLocator.ServiceLocator();
            var service        = new TestService1();

            serviceLocator.Register <ITestService>(service);

            Assert.Throws <ArgumentException>(() => serviceLocator.Register <ITestService>(service));
        }
        public void WhenRegisterAServiceAndTryToGetIt_ReturnTheRegisteredService()
        {
            var serviceLocator = new Services.ServiceLocator.ServiceLocator();
            var service        = new TestService1();

            serviceLocator.Register <ITestService>(service);

            Assert.AreEqual(service, serviceLocator.Get <ITestService>());
        }
        public void GetApplicationService_DoNotUseServicesInMergedDictionaries()
        {
            var defaultServiceContainer = new DefaultServiceContainer2();
            var service1         = new TestService1();
            var mergedDictionary = new ResourceDictionary();

            mergedDictionary.Add("testService2", service1);
            defaultServiceContainer.Resources.MergedDictionaries.Add(mergedDictionary);
            Assert.IsNull(defaultServiceContainer.GetService <IService1>());
        }
 public void AddServicesWithKey() {
     ServiceContainer container = new ServiceContainer(null);
     TestService1 svc1 = new TestService1();
     TestService1 svc1_ = new TestService1();
     container.RegisterService("svc1", svc1);
     container.RegisterService("svc1_", svc1_);
     Assert.AreEqual(svc1, container.GetService<IService1>());
     Assert.AreEqual(svc1, container.GetService<IService1>("svc1"));
     Assert.AreEqual(svc1_, container.GetService<IService1>("svc1_"));
 }
Ejemplo n.º 15
0
 public void Method1(TestService1 service1, TestService2 service2)
 {
     if (service1 is null)
     {
         throw new ArgumentNullException(nameof(service1));
     }
     if (service2 is null)
     {
         throw new ArgumentNullException(nameof(service2));
     }
     service1.Success = true;
 }
Ejemplo n.º 16
0
        public void AddServicesWithKey()
        {
            IServiceContainer container = new ServiceContainer(null);
            TestService1      svc1      = new TestService1();
            TestService1      svc1_     = new TestService1();

            container.RegisterService("svc1", svc1);
            container.RegisterService("svc1_", svc1_);
            Assert.AreEqual(svc1, container.GetService <IService1>());
            Assert.AreEqual(svc1, container.GetService <IService1>("svc1"));
            Assert.AreEqual(svc1_, container.GetService <IService1>("svc1_"));
        }
            public void TestKeylessServiceSearch(int message, bool parentHasKey, bool nestedHasKey, bool yieldToParent, bool preferLocal, bool assertActualIsNested)
            {
                IServiceContainer parentContainer = new ServiceContainer(null);
                IServiceContainer nestedContainer = new ServiceContainer(new ParentServiceContainerWrapper(parentContainer));
                TestService1      parentService   = new TestService1();

                RegisterService(parentHasKey, "p", parentService, false, parentContainer, UseEmptyStringAsParentServiceKey);
                TestService1 nestedService = new TestService1();

                RegisterService(nestedHasKey, "n", nestedService, yieldToParent, nestedContainer, UseEmptyStringAsLocalServiceKey);
                string messageString = message.ToString() + " " + ToString(UseEmptyStringAsParentServiceKey) + " " + ToString(UseEmptyStringAsLocalServiceKey);

                Assert.AreEqual(assertActualIsNested ? nestedService : parentService, nestedContainer.GetService <IService1>(preferLocal ? ServiceSearchMode.PreferLocal : ServiceSearchMode.PreferParents), messageString);
            }
        public void B235125_GetServiceFromSecondParent() {
            var parent1 = new TestSupportServices();
            var parent2 = new TestSupportServices();
            var child = new TestSupportServices();
            var service1 = new TestService1();
            var service2 = new TestService1();
            parent1.ServiceContainer.RegisterService(service1);
            parent2.ServiceContainer.RegisterService(service2);

            child.ParentViewModel = parent1;
            parent1.ParentViewModel = parent2;
            Assert.AreEqual(service1, child.ServiceContainer.GetService<IService1>());
            Assert.AreEqual(service2, child.ServiceContainer.GetService<IService1>(ServiceSearchMode.PreferParents));
        }
Ejemplo n.º 19
0
        public void RegisterInstanceAndResolveAllFromChild()
        {
            var instance = new TestService1();

            var parent = new ServiceContainer();
            parent.RegisterInstance(instance);

            var child = parent.OpenScope("child");

            var services = child.ResolveAll<ITestService>();

            Assert.IsNotEmpty(services);
            Assert.AreEqual(1, services.Count());
        }
Ejemplo n.º 20
0
        public void RegisterInstanceAndResolveFromChild()
        {
            var instance = new TestService1();

            var parent = new ServiceContainer();
            parent.RegisterInstance(instance);

            var child = parent.OpenScope("child");

            var childService = child.Resolve<ITestService>();

            Assert.IsNotNull(childService);
            Assert.IsInstanceOf<TestService1>(childService);
            Assert.AreEqual(instance, childService);
        }
Ejemplo n.º 21
0
        public void RegisterInstanceAndResolveAllFromChild()
        {
            var instance = new TestService1();

            var parent = new ServiceContainer();

            parent.RegisterInstance(instance);

            var child = parent.OpenScope("child");

            var services = child.ResolveAll <ITestService>();

            Assert.IsNotEmpty(services);
            Assert.AreEqual(1, services.Count());
        }
Ejemplo n.º 22
0
 public TestClass1(IServiceProvider serviceProvider, TestService1 service1, TestService2 service2)
 {
     if (serviceProvider is null)
     {
         throw new ArgumentNullException(nameof(serviceProvider));
     }
     if (service1 is null)
     {
         throw new ArgumentNullException(nameof(service1));
     }
     if (service2 is null)
     {
         throw new ArgumentNullException(nameof(service2));
     }
     service1.Success = true;
 }
        public void B235125_GetServiceFromSecondParentByKey()
        {
            var parent1  = new TestSupportServices();
            var parent2  = new TestSupportServices();
            var child    = new TestSupportServices();
            var service1 = new TestService1();
            var service2 = new TestService1();

            parent1.ServiceContainer.RegisterService("key", service1);
            parent2.ServiceContainer.RegisterService("key", service2);

            child.ParentViewModel   = parent1;
            parent1.ParentViewModel = parent2;
            Assert.AreEqual(service1, child.ServiceContainer.GetService <IService1>("key"));
            Assert.AreEqual(service2, child.ServiceContainer.GetService <IService1>("key", ServiceSearchMode.PreferParents));
        }
        public void ContainersLoopTest()
        {
            var child  = new TestSupportServices();
            var parent = new TestSupportServices();

            child.ParentViewModel  = parent;
            parent.ParentViewModel = child;
            var service1 = new TestService1();

            child.ServiceContainer.RegisterService(service1, true);
            AssertHelper.AssertThrows <Exception>(() => {
                child.ServiceContainer.GetService <IService1>();
            }, e => {
                Assert.AreEqual("A ServiceContainer should not be a direct or indirect parent for itself.", e.Message);
            });
        }
Ejemplo n.º 25
0
        public void RegisterInstanceAndResolveFromChild()
        {
            var instance = new TestService1();

            var parent = new ServiceContainer();

            parent.RegisterInstance(instance);

            var child = parent.OpenScope("child");

            var childService = child.Resolve <ITestService>();

            Assert.IsNotNull(childService);
            Assert.IsInstanceOf <TestService1>(childService);
            Assert.AreEqual(instance, childService);
        }
Ejemplo n.º 26
0
            private void Method3(IServiceProvider serviceProvider, TestService1 service1)
            {
                if (service1 is null)
                {
                    throw new ArgumentNullException(nameof(service1));
                }

                if (service1.Called)
                {
                    throw new InvalidOperationException("Invalid method called!");
                }

                service1.Called  = true;
                service1.Success = false;

                serviceProvider.Call(this, nameof(Method3), allowPrivate: true);
            }
        public void GetLastParent()
        {
            var root   = new TestSupportServices();
            var parent = new TestSupportServices();
            var child  = new TestSupportServices();

            parent.ParentViewModel = root;
            child.ParentViewModel  = parent;

            var rootSrv   = new TestService1();
            var parentSrv = new TestService1();
            var childSrv  = new TestService1();

            root.ServiceContainer.RegisterService(rootSrv);
            parent.ServiceContainer.RegisterService(parentSrv);
            child.ServiceContainer.RegisterService(childSrv);

            Assert.AreEqual(rootSrv, child.ServiceContainer.GetService <IService1>(ServiceSearchMode.PreferParents));
        }
        public void GetServiceFromParent() {
            var parent = new TestSupportServices();
            var child = new TestSupportServices();
            var service1 = new TestService1();
            child.ServiceContainer.RegisterService(service1);
            Assert.AreEqual(service1, child.ServiceContainer.GetService<IService1>());

            var service2 = new TestService2();
            parent.ServiceContainer.RegisterService(service2);
            Assert.AreEqual(null, child.ServiceContainer.GetService<IService2>());

            child.ParentViewModel = parent;
            Assert.AreEqual(service2, child.ServiceContainer.GetService<IService2>());

            Assert.AreEqual(null, child.ServiceContainer.GetService<IService2>(ServiceSearchMode.LocalOnly));
            var service2Local = new TestService2();
            child.ServiceContainer.RegisterService(service2Local);
            Assert.AreEqual(service2Local, child.ServiceContainer.GetService<IService2>(ServiceSearchMode.LocalOnly));
            Assert.AreEqual(service2, child.ServiceContainer.GetService<IService2>(ServiceSearchMode.PreferParents));
        }
Ejemplo n.º 29
0
    public void ServiceLocaterClearTest()
    {
        ServInjector.Clear();

        //複数のサービスを登録
        var serv1 = new TestService1();

        ServInjector.Bind <ITestService>(serv1);
        ServInjector.Bind <TestService1>(serv1);
        ServInjector.Bind <TestService2>(new TestService2());
        //取り出せる
        Assert.IsNotNull(ServInjector.Resolve <ITestService>());
        Assert.IsNotNull(ServInjector.Resolve <TestService1>());
        Assert.IsNotNull(ServInjector.Resolve <TestService2>());
        //Clearですべてにcacheが削除される
        ServInjector.Clear();
        Assert.IsNull(ServInjector.Resolve <ITestService>());
        Assert.IsNull(ServInjector.Resolve <TestService1>());
        Assert.IsNull(ServInjector.Resolve <TestService2>());
    }
        public void GetApplicationService()
        {
            var defaultServiceContainer = new DefaultServiceContainer2();
            var service2InApp           = new TestService2();

            defaultServiceContainer.Resources.Add("testService2", service2InApp);
            var service11 = new TestService1();
            var service12 = new TestService1();

            defaultServiceContainer.Resources.Add("testService11", service11);
            defaultServiceContainer.Resources.Add("testService12", service12);
            ServiceContainer.Default = defaultServiceContainer;
            var parent   = new TestSupportServices();
            var child    = new TestSupportServices();
            var service2 = new TestService2();

            child.ServiceContainer.RegisterService(service2);
            child.ParentViewModel = parent;
            Assert.AreEqual(service2, child.ServiceContainer.GetService <IService2>(ServiceSearchMode.PreferParents));
            Assert.AreEqual(service2, child.ServiceContainer.GetService <IService2>());
            Assert.IsNotNull(child.ServiceContainer.GetService <IService1>());
            Assert.AreEqual(service11, child.ServiceContainer.GetService <IService1>("testService11"));
        }
        public void GetServiceFromParent_InvalidParent()
        {
            var child = new TestSupportServices();

            var service2 = new TestService2();

            child.ServiceContainer.RegisterService(service2);
            Assert.AreEqual(service2, child.ServiceContainer.GetService <IService2>());
            Assert.AreEqual(null, child.ServiceContainer.GetService <IService1>());

            child.ParentViewModel = child;
            Assert.AreEqual(service2, child.ServiceContainer.GetService <IService2>());
            Assert.AreEqual(null, child.ServiceContainer.GetService <IService1>());

            var service1 = new TestService1();

            ServiceContainer.Default.RegisterService(service1);
            try {
                Assert.AreEqual(service1, child.ServiceContainer.GetService <IService1>());
            } finally {
                ServiceContainer.Default.Clear();
            }
            Assert.AreEqual(null, child.ServiceContainer.GetService <IService1>());
        }
        public void GetServiceFromParent()
        {
            var parent   = new TestSupportServices();
            var child    = new TestSupportServices();
            var service1 = new TestService1();

            child.ServiceContainer.RegisterService(service1);
            Assert.AreEqual(service1, child.ServiceContainer.GetService <IService1>());

            var service2 = new TestService2();

            parent.ServiceContainer.RegisterService(service2);
            Assert.AreEqual(null, child.ServiceContainer.GetService <IService2>());

            child.ParentViewModel = parent;
            Assert.AreEqual(service2, child.ServiceContainer.GetService <IService2>());

            Assert.AreEqual(null, child.ServiceContainer.GetService <IService2>(ServiceSearchMode.LocalOnly));
            var service2Local = new TestService2();

            child.ServiceContainer.RegisterService(service2Local);
            Assert.AreEqual(service2Local, child.ServiceContainer.GetService <IService2>(ServiceSearchMode.LocalOnly));
            Assert.AreEqual(service2, child.ServiceContainer.GetService <IService2>(ServiceSearchMode.PreferParents));
        }
 public void RegisterByNullKey() {
     var child = new TestSupportServices();
     var service = new TestService1();
     child.ServiceContainer.RegisterService(null, service);
     Assert.AreEqual(service, child.ServiceContainer.GetService<IService1>());
 }
Ejemplo n.º 34
0
 public TestService2(TestService1 service1)
 {
     Service1 = service1;
 }
        public void GetServiceFromParent_InvalidParent() {
            var child = new TestSupportServices();

            var service2 = new TestService2();
            child.ServiceContainer.RegisterService(service2);
            Assert.AreEqual(service2, child.ServiceContainer.GetService<IService2>());
            Assert.AreEqual(null, child.ServiceContainer.GetService<IService1>());

            child.ParentViewModel = child;
            Assert.AreEqual(service2, child.ServiceContainer.GetService<IService2>());
            Assert.AreEqual(null, child.ServiceContainer.GetService<IService1>());

            var service1 = new TestService1();
            ServiceContainer.Default.RegisterService(service1);
            try {
                Assert.AreEqual(service1, child.ServiceContainer.GetService<IService1>());
            } finally {
                ServiceContainer.Default.Clear();
            }
            Assert.AreEqual(null, child.ServiceContainer.GetService<IService1>());
        }
Ejemplo n.º 36
0
 public ComponentWithConstructorInjection(TestService1 property1, TestService2 property2)
 {
     Property1 = property1;
     Property2 = property2;
 }
Ejemplo n.º 37
0
 public TestService2(TestService1 service1)
 {
     Service1 = service1;
 }
 public void GetServiceNotByInterfaceType() {
     ServiceContainer container = new ServiceContainer(null);
     TestService1 svc1 = new TestService1();
     container.RegisterService(svc1);
     container.GetService<TestService1>();
 }
Ejemplo n.º 39
0
 public TestDependencyResolver(TestService1 service1, TestService2 service2)
 {
     _service1 = service1;
     _service2 = service2;
 }