Exemplo n.º 1
0
 public void WhenNoImplementationsRegistered_RegistrationsForServiceIncludeDynamicSources()
 {
     var registry = new ComponentRegistry();
     registry.AddRegistrationSource(new ObjectRegistrationSource());
     Assert.False(registry.Registrations.Where(
         r => r.Services.Contains(new TypedService(typeof(object)))).Any());
     Assert.Equal(1, registry.RegistrationsFor(new TypedService(typeof(object))).Count());
 }
 public void ConstructorSetsProperties()
 {
     var registry = new ComponentRegistry();
     var registration = Factory.CreateSingletonObjectRegistration();
     var args = new ComponentRegisteredEventArgs(registry, registration);
     Assert.Same(registry, args.ComponentRegistry);
     Assert.Same(registration, args.ComponentRegistration);
 }
Exemplo n.º 3
0
 public void AdaptingAGeneratedServiceYieldsASingleAdapter()
 {
     var registry = new ComponentRegistry();
     registry.AddRegistrationSource(new MetaRegistrationSource());
     registry.AddRegistrationSource(new CollectionRegistrationSource());
     var metaCollections = registry.RegistrationsFor(
         new TypedService(typeof(Meta<IEnumerable<object>>)));
     Assert.AreEqual(1, metaCollections.Count());
 }
Exemplo n.º 4
0
 public void AdaptingAnAdapterYieldsASingleAdapter()
 {
     var registry = new ComponentRegistry();
     registry.Register(RegistrationBuilder.ForType<object>().CreateRegistration());
     registry.AddRegistrationSource(new MetaRegistrationSource());
     registry.AddRegistrationSource(new GeneratedFactoryRegistrationSource());
     var metaCollections = registry.RegistrationsFor(
         new TypedService(typeof(Meta<Func<object>>)));
     Assert.AreEqual(1, metaCollections.Count());
 }
Exemplo n.º 5
0
        public void WhenRegistrationsAreMadeTheyDoNotAffectTheReadRegistry()
        {
            var read = new ComponentRegistry();
            var cow = new CopyOnWriteRegistry(read, () => new ComponentRegistry());
            var registration = RegistrationBuilder.ForType<object>().CreateRegistration();
            cow.Register(registration);

            var objectService = new TypedService(typeof (object));
            Assert.True(cow.IsRegistered(objectService));
            Assert.False(read.IsRegistered(objectService));
        }
Exemplo n.º 6
0
 public void AfterResolvingAdapterType_AddingAnAdapter_AddsAdaptingComponents()
 {
     var registry = new ComponentRegistry();
     registry.Register(RegistrationBuilder.ForType<object>().CreateRegistration());
     var adapterService = new TypedService(typeof(Func<object>));
     var pre = registry.RegistrationsFor(adapterService);
     Assert.AreEqual(0, pre.Count());
     registry.AddRegistrationSource(new GeneratedFactoryRegistrationSource());
     var post = registry.RegistrationsFor(adapterService);
     Assert.AreEqual(1, post.Count());
 }
Exemplo n.º 7
0
        /// <summary>
        /// Create a new <see cref="IComponentRegistry" /> with all the component registrations that have been made.
        /// </summary>
        /// <returns>A new component registry with the configured component registrations.</returns>
        public IComponentRegistry Build()
        {
            // Go through all our registrations and build the component pipeline for each one.
            foreach (var registration in _registeredServicesTracker.Registrations)
            {
                registration.BuildResolvePipeline(_registeredServicesTracker);
            }

            var componentRegistry = new ComponentRegistry(_registeredServicesTracker, Properties);

            return(componentRegistry);
        }
Exemplo n.º 8
0
        public void WhenMultipleProvidersOfServiceExist_DefaultRegistrationIsMostRecent()
        {
            var r1 = Factory.CreateSingletonObjectRegistration();
            var r2 = Factory.CreateSingletonObjectRegistration();

            var registry = new ComponentRegistry();

            registry.Register(r1);
            registry.Register(r2);

            IComponentRegistration defaultRegistration;
            Assert.True(registry.TryGetRegistration(new TypedService(typeof(object)), out defaultRegistration));
            Assert.Same(r2, defaultRegistration);
        }
Exemplo n.º 9
0
        public void WhenReadingTheWriteRegistryIsNotCreated()
        {
            var writeRegistryCreated = false;
            var read = new ComponentRegistry();
            var cow = new CopyOnWriteRegistry(read, () =>
            {
                writeRegistryCreated = true;
                return new ComponentRegistry();
            });

            IComponentRegistration unused;
            cow.TryGetRegistration(new TypedService(typeof (object)), out unused);

            Assert.False(writeRegistryCreated);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Create a new <see cref="IComponentRegistry" /> with all the component registrations that have been made.
        /// </summary>
        /// <returns>A new component registry with the configured component registrations.</returns>
        public IComponentRegistry Build()
        {
            // Mark our tracker as complete; no more adjustments to the registry will be made after this point.
            _registeredServicesTracker.Complete();

            // Go through all our registrations and build the component pipeline for each one.
            foreach (var registration in _registeredServicesTracker.Registrations)
            {
                registration.BuildResolvePipeline(_registeredServicesTracker);
            }

            var componentRegistry = new ComponentRegistry(_registeredServicesTracker, Properties);

            return(componentRegistry);
        }
Exemplo n.º 11
0
        public void AddingConcreteImplementationWhenAdapterImplementationsExist_AddsChainedAdapters()
        {
            var registry = new ComponentRegistry();
            registry.AddRegistrationSource(new GeneratedFactoryRegistrationSource());
            registry.AddRegistrationSource(new MetaRegistrationSource());
            registry.Register(RegistrationBuilder.ForType<object>().CreateRegistration());

            var chainedService = new TypedService(typeof(Meta<Func<object>>));

            var pre = registry.RegistrationsFor(chainedService);
            Assert.AreEqual(1, pre.Count());

            Func<object> func = () => new object();
            registry.Register(RegistrationBuilder.ForDelegate((c, p) => func).CreateRegistration());

            var post = registry.RegistrationsFor(chainedService);
            Assert.AreEqual(2, post.Count());
        }
Exemplo n.º 12
0
        /// <summary>
        /// Create a new container.
        /// </summary>
        internal Container()
        {
            ComponentRegistry = new ComponentRegistry();

            ComponentRegistry.Register(new ComponentRegistration(
                LifetimeScope.SelfRegistrationId,
                new DelegateActivator(typeof(LifetimeScope), (c, p) =>
                {
                    throw new InvalidOperationException(ContainerResources.SelfRegistrationCannotBeActivated);
                }),
                new CurrentScopeLifetime(),
                InstanceSharing.Shared,
                InstanceOwnership.ExternallyOwned,
                new Service[] { new TypedService(typeof(ILifetimeScope)), new TypedService(typeof(IComponentContext)) },
                new Dictionary<string, object>()));

            _rootLifetimeScope = new LifetimeScope(ComponentRegistry);
        }
Exemplo n.º 13
0
        public void AfterResolvingAdapter_AddingMoreAdaptees_AddsMoreAdapters()
        {
            var registry = new ComponentRegistry();
            registry.AddRegistrationSource(new MetaRegistrationSource());
            var metaService = new TypedService(typeof(Meta<object>));

            var first = RegistrationBuilder.ForType<object>().CreateRegistration();
            registry.Register(first);

            var meta1 = registry.RegistrationsFor(metaService);
            var firstMeta = meta1.First();

            var second = RegistrationBuilder.ForType<object>().CreateRegistration();
            registry.Register(second);

            var meta2 = registry.RegistrationsFor(metaService);

            Assert.That(meta2.Count(), Is.EqualTo(2));
            Assert.That(meta2.Contains(firstMeta));
            Assert.That(meta2.Select(m => m.Target), Is.EquivalentTo(new[] { first, second }));
        }
Exemplo n.º 14
0
        public void WhenRegistrationIsMade_ComponentRegisteredEventFired()
        {
            object eventSender = null;
            ComponentRegisteredEventArgs args = null;
            var eventCount = 0;

            var registry = new ComponentRegistry();
            registry.Registered += (sender, e) =>
            {
                eventSender = sender;
                args = e;
                ++eventCount;
            };

            var registration = Factory.CreateSingletonObjectRegistration();
            registry.Register(registration);

            Assert.Equal(1, eventCount);
            Assert.NotNull(eventSender);
            Assert.Same(registry, eventSender);
            Assert.NotNull(args);
            Assert.Same(registry, args.ComponentRegistry);
            Assert.Same(registration, args.ComponentRegistration);
        }
Exemplo n.º 15
0
        public void WhenASourceIsAddedToTheRegistry_TheSourceAddedEventIsRaised()
        {
            var registry = new ComponentRegistry();

            object sender = null;
            RegistrationSourceAddedEventArgs args = null;

            registry.RegistrationSourceAdded += (s, e) =>
            {
                sender = s;
                args = e;
            };

            var source = new ObjectRegistrationSource();
            registry.AddRegistrationSource(source);

            Assert.AreSame(registry, sender);
            Assert.AreSame(registry, args.ComponentRegistry);
            Assert.AreSame(source, args.RegistrationSource);
        }
 public void NullRegistrationDetected()
 {
     var registry = new ComponentRegistry();
     var args = new ComponentRegisteredEventArgs(registry, null);
 }
Exemplo n.º 17
0
        public void WhenRegistrationsAddedBeforeAndAfterSource_BothAreSeenBySource()
        {
            var r1 = Factory.CreateSingletonObjectRegistration();
            var r2 = Factory.CreateSingletonObjectRegistration();

            var registry = new ComponentRegistry();
            registry.Register(r1);
            registry.AddRegistrationSource(new GeneratedFactoryRegistrationSource());
            registry.Register(r2);

            var wrappedObjects = registry.RegistrationsFor(new TypedService(typeof(Func<object>)));

            Assert.AreEqual(2, wrappedObjects.Count());
        }
Exemplo n.º 18
0
        public void WhenRegistrationProvidedExplicitlyAndThroughRegistrationSource_ExplicitRegistrationIsDefault()
        {
            var r = Factory.CreateSingletonObjectRegistration();

            var registry = new ComponentRegistry();
            registry.Register(r);
            registry.AddRegistrationSource(new ObjectRegistrationSource());

            IComponentRegistration defaultForObject;
            registry.TryGetRegistration(new TypedService(typeof(object)), out defaultForObject);

            Assert.AreSame(r, defaultForObject);
        }
Exemplo n.º 19
0
        public void WhenRegistrationProvidedExplicitlyAndThroughRegistrationSource_Reordered_BothAreReturnedFromRegistrationsFor()
        {
            var r = Factory.CreateSingletonObjectRegistration();

            var registry = new ComponentRegistry();
            registry.AddRegistrationSource(new ObjectRegistrationSource());
            registry.Register(r);

            var forObject = registry.RegistrationsFor(new TypedService(typeof(object)));

            Assert.AreEqual(2, forObject.Count());
        }
Exemplo n.º 20
0
 public void WhenNoImplementers_TryGetRegistrationReturnsFalse()
 {
     var registry = new ComponentRegistry();
     IComponentRegistration unused;
     Assert.IsFalse(registry.TryGetRegistration(new TypedService(typeof(object)), out unused));
 }
Exemplo n.º 21
0
        public void LastRegistrationSourceRegisteredIsTheDefault()
        {
            var first = new object();
            var second = new object();
            var registry = new ComponentRegistry();

            registry.AddRegistrationSource(new ObjectRegistrationSource(first));
            registry.AddRegistrationSource(new ObjectRegistrationSource(second));

            IComponentRegistration def;
            registry.TryGetRegistration(new TypedService(typeof(object)), out def);

            var result = def.Activator.ActivateInstance(new ContainerBuilder().Build(), Enumerable.Empty<Parameter>());

            Assert.AreEqual(result, second);
        }
Exemplo n.º 22
0
        public void WhenRegistrationProvidedExplicitlyAndThroughRegistrationSource_BothAreReturnedFromRegistrationsFor()
        {
            var r = Factory.CreateSingletonObjectRegistration();

            var registry = new ComponentRegistry();
            registry.Register(r);
            registry.AddRegistrationSource(new ObjectRegistrationSource());

            var forObject = registry.RegistrationsFor(new TypedService(typeof(object)));

            Assert.Equal(2, forObject.Count());

            // Just paranoia - make sure we don't regenerate
            forObject = registry.RegistrationsFor(new TypedService(typeof(object)));

            Assert.Equal(2, forObject.Count());
        }
 public void NullRegistrationDetected()
 {
     var registry = new ComponentRegistry();
     Assert.Throws<ArgumentNullException>(() => new ComponentRegisteredEventArgs(registry, null));
 }
Exemplo n.º 24
0
 public void WhenARegistrationSourceQueriesForTheSameService_ItIsNotRecursivelyQueried()
 {
     var registry = new ComponentRegistry();
     registry.AddRegistrationSource(new RecursiveRegistrationSource());
     Assert.False(registry.IsRegistered(new UniqueService()));
 }
Exemplo n.º 25
0
 public void WhenAdaptersAreAppliedButNoRegistrationsCreated_AddingAdapteesAddsAdapters()
 {
     var registry = new ComponentRegistry();
     registry.AddRegistrationSource(new GeneratedFactoryRegistrationSource());
     var adapterService = new TypedService(typeof(Func<object>));
     registry.RegistrationsFor(adapterService);
     registry.Register(RegistrationBuilder.ForType<object>().CreateRegistration());
     var adapters = registry.RegistrationsFor(adapterService);
     Assert.AreEqual(1, adapters.Count());
 }
Exemplo n.º 26
0
 public void Register_DoesNotAcceptNull()
 {
     var registry = new ComponentRegistry();
     Assertions.AssertThrows<ArgumentNullException>(() => registry.Register(null));
 }
Exemplo n.º 27
0
 public void LoadsRegistrations()
 {
     var cr = new ComponentRegistry();
     new ObjectModule().Configure(cr);
     Assert.True(cr.IsRegistered(new TypedService(typeof(object))));
 }
Exemplo n.º 28
0
 public void WhenNoImplementerIsDirectlyRegistered_RegistrationCanBeProvidedDynamically()
 {
     var registry = new ComponentRegistry();
     registry.AddRegistrationSource(new ObjectRegistrationSource());
     IComponentRegistration registration;
     Assert.IsTrue(registry.TryGetRegistration(new TypedService(typeof(object)), out registration));
 }