コード例 #1
0
        public void RegistrationsMadeInConfigureExpressionAreAddedToContainer()
        {
            var ls = new Container()
                .BeginLifetimeScope(b => b.RegisterType<MyComponent>().As<IMyService>());

            var component = ls.Resolve<IMyService>();
            Assert.IsTrue(component is MyComponent);
        }
コード例 #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));
        }
コード例 #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));
        }
コード例 #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);
        }
コード例 #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);
 }
コード例 #6
0
        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));
        }
コード例 #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));
        }
コード例 #8
0
        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)));
        }
コード例 #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>());
        }
コード例 #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));
        }
コード例 #11
0
ファイル: ContainerTests.cs プロジェクト: arronchen/Autofac
        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>());
        }
コード例 #12
0
ファイル: ContainerBuilder.cs プロジェクト: kcuzner/Autofac
 /// <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;
 }
コード例 #13
0
ファイル: ContainerTests.cs プロジェクト: RoymanJing/Autofac
 public void ResolvingLifetimeScopeProvidesCurrentScope()
 {
     var c = new Container();
     var l = c.BeginLifetimeScope();
     Assert.AreSame(l, l.Resolve<ILifetimeScope>());
 }
コード例 #14
0
ファイル: ContainerTests.cs プロジェクト: RoymanJing/Autofac
 public void ContainerProvidesILifetimeScopeAndIContext()
 {
     var container = new Container();
     Assert.IsTrue(container.IsRegistered<ILifetimeScope>());
     Assert.IsTrue(container.IsRegistered<IComponentContext>());
 }
コード例 #15
0
 public void WhenServiceNotRegistered_ResolveOptionalReturnsNull()
 {
     var target = new Container();
     var inst = target.ResolveOptional<string>();
     Assert.Null(inst);
 }
コード例 #16
0
ファイル: ContainerTests.cs プロジェクト: arronchen/Autofac
 public void ResolvingComponentContextProvidesCurrentScope()
 {
     var c = new Container();
     var l = c.BeginLifetimeScope();
     Assert.Same(l, l.Resolve<IComponentContext>());
 }
コード例 #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"));
 }