Пример #1
0
        public void TestRegisterReplace()
        {
            // check before insert
            Assert.NotNull(ServiceRegister);
            Assert.Empty(ServiceRegister.Registrations);

            // insert
            var instance = new DummyDerivedClass();

            ServiceRegister.RegisterInstance <DummyBaseClass>(instance);

            Assert.NotEmpty(ServiceRegister.Registrations);
            Assert.Single(ServiceRegister.Registrations);

            // try resolve registration
            var registration = ServiceRegister.FindRegistration <DummyBaseClass>();

            Assert.NotNull(registration);
            Assert.Same(instance, registration.Create(null));

            // replace registration
            var otherInstance = new DummyOtherDerivedClass();

            ServiceRegister.RegisterInstance <DummyBaseClass>(otherInstance, ServiceRegistrationMode.Replace);

            // try resolve other registration
            var otherRegistration = ServiceRegister.FindRegistration <DummyBaseClass>();

            Assert.NotEmpty(ServiceRegister.Registrations);
            Assert.Single(ServiceRegister.Registrations);
            Assert.NotNull(otherRegistration);
            Assert.Same(otherInstance, otherRegistration.Create(null));
        }
Пример #2
0
        public void TestRegisterDuplicateInstance()
        {
            // check before insert
            Assert.NotNull(ServiceRegister);
            Assert.Empty(ServiceRegister.Registrations);

            // insert
            var instance = new DummyDerivedClass();

            ServiceRegister.RegisterInstance <DummyBaseClass>(instance);

            Assert.NotEmpty(ServiceRegister.Registrations);
            Assert.Single(ServiceRegister.Registrations);

            // insert other (should throw exception)
            Assert.Throws <RegistrationException>(
                () => ServiceRegister.RegisterInstance <DummyBaseClass>(instance));
        }