public void RegistrationsMadeInConfigureExpressionAreAddedToContainer()
        {
            var ls = new Container()
                .BeginLifetimeScope(b => b.RegisterType<MyComponent>().As<IMyService>());

            var component = ls.Resolve<IMyService>();
            Assert.IsTrue(component is MyComponent);
        }
Ejemplo n.º 2
0
        public void MatchesAgainstSingleTaggedScope()
        {
            const string tag = "Tag";
            var msl = new MatchingScopeLifetime(tag);
            var container = new Container();
            var lifetimeScope = (ISharingLifetimeScope)container.BeginLifetimeScope(tag);

            Assert.Equal(lifetimeScope, msl.FindScope(lifetimeScope));
        }
Ejemplo n.º 3
0
        public void WhenNoMatchingScopeIsPresent_TheExceptionMessageIncludesTheTag()
        {
            var container = new Container();
            const string tag = "abcdefg";
            var msl = new MatchingScopeLifetime(tag);
            var rootScope = (ISharingLifetimeScope)container.Resolve<ILifetimeScope>();

            var ex = Assert.Throws<DependencyResolutionException>(() => msl.FindScope(rootScope));
            Assert.True(ex.Message.Contains(tag));
        }
Ejemplo n.º 4
0
        public void WhenServiceIsRegistered_ResolveOptionalReturnsAnInstance()
        {
            var target = new Container();
            target.ComponentRegistry.Register(Factory.CreateSingletonRegistration(
                new[] { new TypedService(typeof(string)) },
                new ProvidedInstanceActivator("Hello")));

            var inst = target.ResolveOptional<string>();

            Assert.Equal("Hello", inst);
        }
Ejemplo n.º 5
0
 public void AdaptersInNestedScopeOverrideAdaptersInParent()
 {
     const string parentInstance = "p";
     const string childInstance = "c";
     var parent = new Container();
     parent.ComponentRegistry.AddRegistrationSource(new ObjectRegistrationSource(parentInstance));
     var child = parent.BeginLifetimeScope(builder =>
             builder.RegisterSource(new ObjectRegistrationSource(childInstance)));
     var fromChild = child.Resolve<object>();
     Assert.Same(childInstance, fromChild);
 }
        public void OnlyServicesAssignableToASpecificTypeAreRegisteredFromTypeList()
        {
            var container = new Container().BeginLifetimeScope(b =>
                b.RegisterTypes(Assembly.GetExecutingAssembly().GetLoadableTypes().ToArray())
                    .AssignableTo(typeof(IMyService)));

            Assert.AreEqual(1, container.ComponentRegistry.Registrations.Count());
            Object obj;
            Assert.IsTrue(container.TryResolve(typeof(MyComponent), out obj));
            Assert.IsFalse(container.TryResolve(typeof(MyComponent2), out obj));
        }
Ejemplo n.º 7
0
        public void OnlyServicesAssignableToASpecificTypeAreRegisteredFromAssemblies()
        {
            var container = new Container().BeginLifetimeScope(b =>
                b.RegisterAssemblyTypes(GetType().GetTypeInfo().Assembly)
                    .AssignableTo(typeof(IMyService)));

            Assert.Equal(1, container.ComponentRegistry.Registrations.Count());
            Object obj;
            Assert.True(container.TryResolve(typeof(MyComponent), out obj));
            Assert.False(container.TryResolve(typeof(MyComponent2), out obj));
        }
        public void WhenNoMatchingScopeIsPresent_TheExceptionMessageIncludesTheTags()
        {
            var container = new Container();
            const string tag1 = "abc";
            const string tag2 = "def";
            var msl = new MatchingScopeLifetime(tag1, tag2);
            var rootScope = (ISharingLifetimeScope)container.Resolve<ILifetimeScope>();

            var ex = Assert.Throws<DependencyResolutionException>(() => msl.FindScope(rootScope));
            Assert.That(ex.Message.Contains(string.Format("{0}, {1}", tag1, tag2)));
        }
Ejemplo n.º 9
0
        public void WhenComponentIsRegistered_IsRegisteredReturnsTrueForAllServices()
        {
            var registration = Factory.CreateSingletonRegistration(
                new[] { new TypedService(typeof(object)), new TypedService(typeof(string)) },
                Factory.CreateProvidedInstanceActivator("Hello"));

            var target = new Container();

            target.ComponentRegistry.Register(registration);

            Assert.True(target.IsRegistered<object>());
            Assert.True(target.IsRegistered<string>());
        }
Ejemplo n.º 10
0
        public void MatchesAgainstMultipleTaggedScopes()
        {
            const string tag1 = "Tag1";
            const string tag2 = "Tag2";

            var msl = new MatchingScopeLifetime(tag1, tag2);
            var container = new Container();

            var tag1Scope = (ISharingLifetimeScope)container.BeginLifetimeScope(tag1);
            Assert.Equal(tag1Scope, msl.FindScope(tag1Scope));

            var tag2Scope = (ISharingLifetimeScope)container.BeginLifetimeScope(tag2);
            Assert.Equal(tag2Scope, msl.FindScope(tag2Scope));
        }
Ejemplo n.º 11
0
        public void ResolveByName()
        {
            string name = "name";

            var r = Factory.CreateSingletonRegistration(
                new Service[] { new KeyedService(name, typeof(string)) },
                Factory.CreateReflectionActivator(typeof(object)));

            var c = new Container();
            c.ComponentRegistry.Register(r);

            object o;

            Assert.True(c.TryResolveNamed(name, typeof(string), out o));
            Assert.NotNull(o);

            Assert.False(c.IsRegistered<object>());
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Create a new container with the component registrations that have been made.
 /// </summary>
 /// <param name="options">Options that influence the way the container is initialised.</param>
 /// <remarks>
 /// Build can only be called once per <see cref="ContainerBuilder"/>
 /// - this prevents ownership issues for provided instances.
 /// Build enables support for the relationship types that come with Autofac (e.g.
 /// Func, Owned, Meta, Lazy, IEnumerable.) To exclude support for these types,
 /// first create the container, then call Update() on the builder.
 /// </remarks>
 /// <returns>A new container with the configured component registrations.</returns>
 public IContainer Build(ContainerBuildOptions options = ContainerBuildOptions.None)
 {
     var result = new Container();
     Build(result.ComponentRegistry, (options & ContainerBuildOptions.ExcludeDefaultModules) != ContainerBuildOptions.None);
     if ((options & ContainerBuildOptions.IgnoreStartableComponents) == ContainerBuildOptions.None)
         StartStartableComponents(result);
     return result;
 }
Ejemplo n.º 13
0
 public void ResolvingLifetimeScopeProvidesCurrentScope()
 {
     var c = new Container();
     var l = c.BeginLifetimeScope();
     Assert.AreSame(l, l.Resolve<ILifetimeScope>());
 }
Ejemplo n.º 14
0
 public void ContainerProvidesILifetimeScopeAndIContext()
 {
     var container = new Container();
     Assert.IsTrue(container.IsRegistered<ILifetimeScope>());
     Assert.IsTrue(container.IsRegistered<IComponentContext>());
 }
Ejemplo n.º 15
0
 public void WhenServiceNotRegistered_ResolveOptionalReturnsNull()
 {
     var target = new Container();
     var inst = target.ResolveOptional<string>();
     Assert.Null(inst);
 }
Ejemplo n.º 16
0
 public void ResolvingComponentContextProvidesCurrentScope()
 {
     var c = new Container();
     var l = c.BeginLifetimeScope();
     Assert.Same(l, l.Resolve<IComponentContext>());
 }
Ejemplo n.º 17
0
 public void ResolvingUnregisteredService_ProvidesDescriptionInException()
 {
     var target = new Container();
     var ex = Assert.Throws<ComponentNotRegisteredException>(() => target.Resolve<object>());
     Assert.True(ex.Message.Contains("System.Object"));
 }