当前位置: 首页>>代码示例>>C#>>正文


C# ContainerBuilder类代码示例

本文整理汇总了C#中ContainerBuilder的典型用法代码示例。如果您正苦于以下问题:C# ContainerBuilder类的具体用法?C# ContainerBuilder怎么用?C# ContainerBuilder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


ContainerBuilder类属于命名空间,在下文中一共展示了ContainerBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Load

 protected override void Load(ContainerBuilder builder)
 {
     builder.Register(c => new WorkingDirectory(c.Resolve<DirectoryInfo>())).As<IWorkingDirectory>().ContainerScoped();
     builder.Register(c => new Repository(c.Resolve<IWorkingDirectory>())).As<IRepository>().ContainerScoped();
     builder.Register(c => new Index(c.Resolve<IRepository>())).As<IIndex>();
     builder.Register(c => new GitLib()).As<IGit>();
 }
开发者ID:kthompson,项目名称:gitty_old,代码行数:7,代码来源:GitCommandLine.cs

示例2: Can_register_and_use_generics_with_configuration_api

        public void Can_register_and_use_generics_with_configuration_api()
        {
            var configuration = new LightCoreConfiguration();
            var registrations = new List<Registration>
                                    {
                                        new Registration
                                            {
                                                ContractType = typeof(IFoo).AssemblyQualifiedName,
                                                ImplementationType = typeof(Foo).AssemblyQualifiedName,
                                                Arguments = new List<Argument>
                                                                {
                                                                    new Argument
                                                                        {
                                                                            Type = "Guid",
                                                                            Value = "354c11f1-94e5-41b8-9a13-122e2df2b0c7"
                                                                        }
                                                                }
                                            }
                                    };

            configuration.Registrations = registrations;

            var builder = new ContainerBuilder();

            RegistrationLoader.Instance.Register(builder, configuration);

            var container = builder.Build();

            Guid expected = new Guid("354c11f1-94e5-41b8-9a13-122e2df2b0c7");

            Assert.AreEqual(expected, ((Foo)container.Resolve<IFoo>()).Arg4);
        }
开发者ID:peterbucher,项目名称:LightCore,代码行数:32,代码来源:AliasTests.cs

示例3: Instance_is_reused_on_same_thread_when_controlled_by_threadsingleton_lifecycle

        public void Instance_is_reused_on_same_thread_when_controlled_by_threadsingleton_lifecycle()
        {
            var builder = new ContainerBuilder();

            builder.DefaultControlledBy<ThreadSingletonLifecycle>();
            builder.Register<IFoo, Foo>();
            builder.Register<IBar, Bar>();

            var container = builder.Build();

            var threadData = new ThreadData(container);
            var thread = new Thread(threadData.ResolveFoosWithContainer);

            var threadDataTwo = new ThreadData(container);
            var threadTwo = new Thread(threadDataTwo.ResolveFoosWithContainer);

            thread.Start();
            threadTwo.Start();

            thread.Join();
            threadTwo.Join();

            Assert.IsTrue(ReferenceEquals(threadData.FooOne, threadData.FooTwo));
            Assert.IsFalse(ReferenceEquals(threadData.FooOne, threadDataTwo.FooOne));
        }
开发者ID:AlexZeitler,项目名称:LightCore,代码行数:25,代码来源:LifecycleTests.cs

示例4: WhenTIsRegisteredByName_CanResolveLazyTByName

 public void WhenTIsRegisteredByName_CanResolveLazyTByName()
 {
     var builder = new ContainerBuilder();
     builder.RegisterType<object>().Named<object>("foo");
     var container = builder.Build();
     Assert.True(container.IsRegisteredWithName<Lazy<object>>("foo"));
 }
开发者ID:arronchen,项目名称:Autofac,代码行数:7,代码来源:LazyRegistrationSourceTests.cs

示例5: SetUp

 public void SetUp()
 {
     var builder = new ContainerBuilder();
     builder.RegisterMetadataRegistrationSources();
     builder.RegisterType<object>();
     _container = builder.Build();
 }
开发者ID:RoymanJing,项目名称:Autofac,代码行数:7,代码来源:LazyWithMetadataRegistrationSourceTests.cs

示例6: ConcurrentResolveOperationsForNonSharedInstancesFromDifferentLifetimes_DoNotBlock

        public void ConcurrentResolveOperationsForNonSharedInstancesFromDifferentLifetimes_DoNotBlock()
        {
            var evt = new ManualResetEvent(false);

            var builder = new ContainerBuilder();
            builder.Register((c, p) =>
                {
                    if (p.TypedAs<bool>())
                        evt.WaitOne();
                    return new object();
                });

            var container = builder.Build();

            var unblocked = 0;
            var blockedScope = container.BeginLifetimeScope();
            var blockedThread = new Thread(() =>
            {
                blockedScope.Resolve<object>(TypedParameter.From(true));
                Interlocked.Increment(ref unblocked);
            });
            blockedThread.Start();
            Thread.Sleep(500);

            container.Resolve<object>(TypedParameter.From(false));
            container.BeginLifetimeScope().Resolve<object>(TypedParameter.From(false));

            Thread.MemoryBarrier();
            Assert.Equal(0, unblocked);
            evt.Set();
            blockedThread.Join();
        }
开发者ID:arronchen,项目名称:Autofac,代码行数:32,代码来源:ConcurrentTests.cs

示例7: DefaultLifetimeForMefComponentsIsSingleton

 public void DefaultLifetimeForMefComponentsIsSingleton()
 {
     var builder = new ContainerBuilder();
     var catalog = new TypeCatalog(typeof(HasDefaultCreationPolicy));
     builder.RegisterComposablePartCatalog(catalog);
     AssertDisposalTrackerIsSingleton(builder);
 }
开发者ID:RoymanJing,项目名称:Autofac,代码行数:7,代码来源:DisposalRegistrationTests.cs

示例8: EnsureWebTypeIsRegistered

 public void EnsureWebTypeIsRegistered(Type serviceType)
 {
     ContainerBuilder builder = new ContainerBuilder();
     builder.RegisterModule(new AutofacWebTypesModule());
     IContainer container = builder.Build();
     Assert.That(container.IsRegistered(serviceType), Is.True);
 }
开发者ID:RoymanJing,项目名称:Autofac,代码行数:7,代码来源:AutofacWebTypesModuleFixture.cs

示例9: Initialise

 public void Initialise()
 {
     var builder = new ContainerBuilder();
     builder.RegisterType<Logger>().As<ILogger>();
     builder.RegisterType<Repository>().As<IRepository>();
     builder.Build();
 }
开发者ID:darrenscrine,项目名称:AutofacDI,代码行数:7,代码来源:RepositoryTests.cs

示例10: LightCoreDelegateUseCase

        public LightCoreDelegateUseCase()
        {
            var builder = new ContainerBuilder();

            builder.Register<IWebApp>(
                c => new WebApp(
                         c.Resolve<IAuthenticator>(),
                         c.Resolve<IStockQuote>()));

            builder.Register<IAuthenticator>(
                c => new Authenticator(
                         c.Resolve<ILogger>(),
                         c.Resolve<IErrorHandler>(),
                         c.Resolve<IDatabase>()));

            builder.Register<IStockQuote>(
                c => new StockQuote(
                         c.Resolve<ILogger>(),
                         c.Resolve<IErrorHandler>(),
                         c.Resolve<IDatabase>()));

            builder.Register<ILogger>(
                c => new Logger());

            builder.Register<IErrorHandler>(
                c => new ErrorHandler(
                         c.Resolve<ILogger>()));

            builder.Register<IDatabase>(
                c => new Database(
                         c.Resolve<ILogger>(),
                         c.Resolve<IErrorHandler>()));

            this._container = builder.Build();
        }
开发者ID:peterbucher,项目名称:LightCore,代码行数:35,代码来源:LightCoreDelegateUseCase.cs

示例11: Can_register_and_use_closed_to_closed_generic_with_configuration_api

        public void Can_register_and_use_closed_to_closed_generic_with_configuration_api()
        {
            var configuration = new LightCoreConfiguration();
            var registrations = new List<Registration>
                                    {
                                        new Registration
                                            {
                                                // LightCore.TestTypes.IRepository`1, LightCore.TestTypes
                                                ContractType = typeof (IRepository<Bar>).AssemblyQualifiedName,
                                                ImplementationType = typeof (BarRepository).AssemblyQualifiedName
                                            }
                                    };

            configuration.Registrations = registrations;

            var builder = new ContainerBuilder();

            RegistrationLoader.Instance.Register(builder, configuration);

            var container = builder.Build();

            var repository = container.Resolve<IRepository<Bar>>();

            Assert.That(repository, Is.Not.Null);
            Assert.That(repository, Is.InstanceOf<BarRepository>());
        }
开发者ID:peterbucher,项目名称:LightCore,代码行数:26,代码来源:ConfigurationTests.cs

示例12: ContainerScope_DefaultServiceRegistrationUsingPreservation

 public void ContainerScope_DefaultServiceRegistrationUsingPreservation()
 {
     var builder = new ContainerBuilder();
     builder.RegisterInstance("s1").PreserveExistingDefaults();
     var container = builder.Build();
     Assert.Equal("s1", container.Resolve<string>());
 }
开发者ID:GitHuang,项目名称:Autofac,代码行数:7,代码来源:PreserveExistingDefaultsTests.cs

示例13: run

        static void run()
        {
            var builder = new ContainerBuilder();
            var registry = new AutofacDependencyRegistryBuilder(builder).build();
            Resolve.initialize_with(registry);
            builder.Register(x => registry).As<DependencyRegistry>().SingletonScoped();

            //needs startups
            builder.Register<StartServiceBus>().As<NeedStartup>();

            // infrastructure

            var manager = new QueueManager(new IPEndPoint(IPAddress.Loopback, 2201), "client.esent");
            manager.CreateQueues("client");
            builder.Register(x => new RhinoPublisher("server", 2200, manager)).As<ServiceBus>().SingletonScoped();
            builder.Register(x => new RhinoReceiver(manager.GetQueue("client"), x.Resolve<CommandProcessor>())).As<RhinoReceiver>().As<Receiver>().SingletonScoped();


            // commanding
            //builder.Register<AsynchronousCommandProcessor>().As<CommandProcessor>().SingletonScoped();
            builder.Register<SynchronousCommandProcessor>().As<CommandProcessor>().SingletonScoped();

            builder.Register<RequestHandler>().As<Handler>();

            Resolve.the<IEnumerable<NeedStartup>>().each(x => x.run());
            Resolve.the<CommandProcessor>().run();
            "started".log();
        }
开发者ID:mokhan,项目名称:ping.pong,代码行数:28,代码来源:Client.cs

示例14: ContainerBuilder_can_initialize_container

        public void ContainerBuilder_can_initialize_container()
        {
            var builder = new ContainerBuilder();
            var container = builder.Build();

            Assert.IsNotNull(container);
        }
开发者ID:AlexZeitler,项目名称:LightCore,代码行数:7,代码来源:ContainerTests.cs

示例15: OnCircularDependency_MessageDescribesCycle

        public void OnCircularDependency_MessageDescribesCycle()
        {
            try
            {
                var builder = new ContainerBuilder();
                builder.Register(c => c.Resolve<object>());

                var target = builder.Build();
                target.Resolve<object>();
            }
            catch (DependencyResolutionException de)
            {
                Assert.IsNull(de.InnerException);
                Assert.IsTrue(de.Message.Contains("System.Object -> System.Object"));
                Assert.IsFalse(de.Message.Contains("System.Object -> System.Object -> System.Object"));
                return;
            }
            catch (Exception ex)
            {
                Assert.Fail("Expected a DependencyResolutionException, got {0}.", ex);
                return;
            }

            Assert.Fail("Expected a DependencyResolutionException.");
        }
开发者ID:RoymanJing,项目名称:Autofac,代码行数:25,代码来源:CircularDependencyDetectorTests.cs


注:本文中的ContainerBuilder类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。