private static void RegisterWithLifestyle_CustomerLifestyleSelectionBehavior_RegistersInstanceByThatLifestyle(
            Type serviceType,
            Action <Container, Lifestyle> registrationWithLifestyle)
        {
            // Arrange
            var expectedLifestyle    = Lifestyle.CreateCustom("Expected", creator => creator);
            var notExpectedLifestyle = Lifestyle.CreateCustom("Not Expected", creator => creator);

            var container = new Container();

            container.Options.LifestyleSelectionBehavior = new CustomLifestyleSelectionBehavior(notExpectedLifestyle);

            registrationWithLifestyle(container, expectedLifestyle);

            // Act
            var registration = container.GetRegistration(serviceType);

            Assert.IsNotNull(registration, "Registration not found for type " + serviceType.ToFriendlyName());

            Lifestyle actualLifestyle = registration.Lifestyle;

            // Assert
            Assert.AreSame(expectedLifestyle, actualLifestyle,
                           string.Format("{0} was expected, but {1} was registered.",
                                         expectedLifestyle.Name, actualLifestyle.Name));
        }
示例#2
0
        public void Verify_TwoRegistrationsForDifferentCustomLifestyleButAndSameImplementation_CausesAmbiguousLifestyleWarning()
        {
            // Arrange
            string expectedMessage = @"
                The registration for IFoo (Custom1) maps to the same implementation (FooBar)
                as the registration for IBar (Custom2) does"
                                     .TrimInside();

            var lifestyle1 = Lifestyle.CreateCustom("Custom1", creator => creator);
            var lifestyle2 = Lifestyle.CreateCustom("Custom2", creator => creator);

            var container = new Container();

            container.Register <IFoo, FooBar>(lifestyle1);
            container.Register <IBar, FooBar>(lifestyle2);

            container.Verify(VerificationOption.VerifyOnly);

            // Act
            var results = Analyzer.Analyze(container);

            // Assert
            Assert_ContainsDescription(results, expectedMessage);
            Assert_AllOfType <AmbiguousLifestylesDiagnosticResult>(results);
        }
        private static Lifestyle CreateExpirationLifestyle(TimeSpan timeout, bool sliding)
        {
            string name = sliding ? "Sliding" : "Absolute";

            return(Lifestyle.CreateCustom(name + " Expiration", instanceCreator =>
            {
                var syncRoot = new object();
                var expirationTime = DateTime.MinValue;
                object instance = null;

                return () =>
                {
                    lock (syncRoot)
                    {
                        if (expirationTime < DateTime.UtcNow)
                        {
                            instance = instanceCreator();

                            if (!sliding)
                            {
                                expirationTime = DateTime.UtcNow.Add(timeout);
                            }
                        }

                        if (sliding)
                        {
                            expirationTime = DateTime.UtcNow.Add(timeout);
                        }

                        return instance;
                    }
                };
            }));
        }
示例#4
0
        public RegisterDecoratorLifestylePredicateNonGeneric()
        {
            var container = new Container();

            var custom = Lifestyle.CreateCustom("custom", transientInstanceCreator => () => transientInstanceCreator());

            container.RegisterDecorator(typeof(ICommon), typeof(CommonImpl1), custom, context => true);
        }
示例#5
0
        public void Register_TwoRegistrationsForSameCustomLifestyleAndSameImplementation_DeduplicatesRegistration()
        {
            // Arrange
            var lifestyle = Lifestyle.CreateCustom("Custom1", creator => creator);

            var container = new Container();

            container.Register <IFoo, FooBar>(lifestyle);
            container.Register <IBar, FooBar>(lifestyle);

            // Act
            var producer1 = container.GetRegistration(typeof(IFoo));
            var producer2 = container.GetRegistration(typeof(IBar));

            // Assert
            Assert.AreSame(producer1.Registration, producer2.Registration,
                           "Registrations for the same custom lifestyle are expected to be de-duplicated/cached");
        }
        public void GetInstance_ForCustomLifestyledRegistration_CallsInstanceCreated()
        {
            // Arrange
            var actualContexts = new List <InstanceInitializationData>();

            var container = new Container();

            var custom =
                Lifestyle.CreateCustom("Custom", transientInstanceCreator => transientInstanceCreator);

            container.Register <RealTimeProvider, RealTimeProvider>(custom);

            container.RegisterInitializer(actualContexts.Add, TruePredicate);

            // Act
            container.GetInstance <RealTimeProvider>();

            // Assert
            Assert.AreEqual(1, actualContexts.Count);
        }
示例#7
0
        public void Register_TwoRegistrationsForDifferentCustomLifestyleButAndSameImplementation_DoesNotDeduplicateRegistrations()
        {
            // Arrange
            var lifestyle1 = Lifestyle.CreateCustom("Custom1", creator => creator);
            var lifestyle2 = Lifestyle.CreateCustom("Custom2", creator => creator);

            var container = new Container();

            container.Register <IFoo, FooBar>(lifestyle1);
            container.Register <IBar, FooBar>(lifestyle2);

            // Act
            var producer1 = container.GetRegistration(typeof(IFoo));
            var producer2 = container.GetRegistration(typeof(IBar));

            // Assert
            Assert.AreNotSame(producer1.Registration, producer2.Registration,
                              "Each registration should get its own registration, because we are effectively dealing with " +
                              "completely unrelated lifestyles. There are both 'custom' lifestyles, but might do caching " +
                              "completely differently.");
        }