• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# DependencyInjectionContainer类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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

示例1: CombinedSpecialTest

		public void CombinedSpecialTest()
		{
			DependencyInjectionContainer container = new DependencyInjectionContainer();

			container.Configure(c => c.Export<LazyService>().As<ILazyService>().WithMetadata("Hello", "World"));

			LazyService.Created = false;

			Lazy<Meta<ILazyService>> lazy = container.Locate<Lazy<Meta<ILazyService>>>();

			Assert.NotNull(lazy);
			Assert.False(LazyService.Created);

			Assert.NotNull(lazy.Value);
			Assert.NotNull(lazy.Value.Value);

			ILazyService service = lazy.Value.Value;

			Assert.NotNull(service);
			Assert.True(LazyService.Created);

			KeyValuePair<string, object> metadata = lazy.Value.Metadata.First();

			Assert.Equal("Hello", metadata.Key);
			Assert.Equal("World", metadata.Value);
		}
开发者ID:ricardoshimoda,项目名称:Grace,代码行数:26,代码来源:LazyTests.cs


示例2: ConfigureDefaultDependencies

        /// <summary>
        /// Configures the default dependencies.
        /// </summary>
        /// <param name="dependencyInjectionContainer">The dependency injection container.</param>
        public void ConfigureDefaultDependencies(DependencyInjectionContainer dependencyInjectionContainer)
        {
            ExceptionUtilities.CheckArgumentNotNull(dependencyInjectionContainer, "dependencyInjectionContainer");

            foreach (var param in this.TestParameters)
            {
                dependencyInjectionContainer.TestParameters[param.Key] = param.Value;
            }

            // set up dependency injector
            dependencyInjectionContainer.RegisterCustomResolver(typeof(Logger), this.GetLoggerForType).Transient();
            dependencyInjectionContainer.RegisterInstance<IDependencyInjector>(dependencyInjectionContainer);
            dependencyInjectionContainer.InjectDependenciesInto(dependencyInjectionContainer);
            dependencyInjectionContainer.InjectDependenciesInto(this.implementationSelector);

            this.implementations = this.implementationSelector.GetImplementations(this.TestParameters).ToList();

            // only register dependencies that cannot already be resolved (unless the dependency was specified as a
            // test parameter, in which case override the default)
            foreach (var implInfo in this.implementations.Where(i => i.IsTestParameterSpecified || !dependencyInjectionContainer.CanResolve(i.ContractType)))
            {
                var options = dependencyInjectionContainer.Register(implInfo.ContractType, implInfo.ImplementationType);
                options.IsTransient = implInfo.IsTransient;
            }
        }
开发者ID:larsenjo,项目名称:odata.net,代码行数:29,代码来源:DependencyInjectionConfigurator.cs


示例3: BeginLifetimeScope

        public void BeginLifetimeScope()
        {
            DependencyInjectionContainer container = new DependencyInjectionContainer();

            container.Configure(c => c.Export<DisposableService>().As<IDisposableService>().Lifestyle.SingletonPerScope());

            IDisposableService service = container.Locate<IDisposableService>();

            Assert.NotNull(service);

            bool called = false;

            using (var scope = container.BeginLifetimeScope())
            {
                var secondService = scope.Locate<IDisposableService>();

                Assert.NotNull(secondService);
                Assert.NotSame(service, secondService);
                Assert.Same(secondService, scope.Locate<IDisposableService>());

                secondService.Disposing += (sender, args) => called = true;
            }

            Assert.True(called);
        }
开发者ID:ricardoshimoda,项目名称:Grace,代码行数:25,代码来源:AdvancedContainerTests.cs


示例4: FactoryFiveArgWithOutBasicAndOutOfOrderTest

        public void FactoryFiveArgWithOutBasicAndOutOfOrderTest()
        {
            DependencyInjectionContainer container = new DependencyInjectionContainer();

            BasicService basicService = new BasicService();

            container.Configure(c =>
                                {
                                    c.Export<FiveArgParameterService>().As<IArrayOfObjectsPropertyService>();
                                    c.ExportInstance(basicService).As<IBasicService>();
                                });

            FiveArgParameterService.ActivateWithOutBasicServiceAndOutOfOrder factory =
                container.Locate<FiveArgParameterService.ActivateWithOutBasicServiceAndOutOfOrder>();

            Assert.NotNull(factory);

            IArrayOfObjectsPropertyService instance = factory(14.0m, "Blah", 9.0, 5);

            Assert.NotNull(instance);
            Assert.Equal(5, instance.Parameters.Length);
            Assert.Equal("Blah", instance.Parameters[0]);
            Assert.Equal(5, instance.Parameters[1]);
            Assert.Equal(9.0, instance.Parameters[2]);
            Assert.Equal(14.0m, instance.Parameters[3]);
            Assert.Equal(basicService, instance.Parameters[4]);
        }
开发者ID:jrjohn,项目名称:Grace,代码行数:27,代码来源:DelegateFactoryTests.cs


示例5: OwnedLazyMetaTest

        public void OwnedLazyMetaTest()
        {
            DependencyInjectionContainer container = new DependencyInjectionContainer();

            container.Configure(c => c.Export<DisposableService>().As<IDisposableService>().WithMetadata(
                "Hello",
                "World"));

            Owned<Lazy<Meta<IDisposableService>>> ownedLazy =
                container.Locate<Owned<Lazy<Meta<IDisposableService>>>>();

            Assert.NotNull(ownedLazy);
            Assert.NotNull(ownedLazy.Value);
            Assert.NotNull(ownedLazy.Value.Value);
            Assert.NotNull(ownedLazy.Value.Value.Value);

            bool disposedCalled = false;

            ownedLazy.Value.Value.Value.Disposing += (sender, args) => disposedCalled = true;

            ownedLazy.Dispose();

            Assert.True(disposedCalled);

            KeyValuePair<string, object> metadata = ownedLazy.Value.Value.Metadata.First();

            Assert.Equal("Hello", metadata.Key);
            Assert.Equal("World", metadata.Value);
        }
开发者ID:jrjohn,项目名称:Grace,代码行数:29,代码来源:OwnedTests.cs


示例6: BulkLocateWithKey

        public void BulkLocateWithKey()
        {
            DependencyInjectionContainer container = new DependencyInjectionContainer();

            container.Configure(c => c.Export(Types.FromThisAssembly())
                                      .ByInterface<ISimpleObject>()
                                      .WithKey(t => t.Name.Last()));

            container.Configure(c =>
            {
                foreach (char ch in "ABCDE")
                {
                    c.Export<ImportSingleSimpleObject>()
                     .WithKey(ch)
                     .WithCtorParam<ISimpleObject>()
                     .LocateWithKey(ch);
                }
            });

            ImportSingleSimpleObject single = container.Locate<ImportSingleSimpleObject>(withKey: 'A');

            Assert.NotNull(single);
            Assert.IsType<SimpleObjectA>(single.SimpleObject);

            Assert.Equal(3, container.LocateAll<ImportSingleSimpleObject>(withKey: new[] { 'A', 'C', 'E' }).Count);
        }
开发者ID:ricardoshimoda,项目名称:Grace,代码行数:26,代码来源:KeyedTests.cs


示例7: Main

		static int Main(string[] args)
		{
			DependencyInjectionContainer container = new DependencyInjectionContainer();

			container.Configure(c => c.Export(Types.FromThisAssembly()).
											   ByInterface(typeof(IExampleModule)).
											   ByInterface(typeof(IExampleSubModule<>)).
											   ByInterface(typeof(IExample<>)));

			IEnumerable<IExampleModule> exampleModules = container.LocateAll<IExampleModule>();

			try
			{
				foreach (IExampleModule exampleModule in exampleModules)
				{
					exampleModule.Execute();
				}
			}
			catch (Exception exp)
			{
				Console.WriteLine("Exception thrown");
				Console.WriteLine(exp.Message);

			    return -1;
			}

		    return 0;
		}
开发者ID:ricardoshimoda,项目名称:Grace,代码行数:28,代码来源:Program.cs


示例8: ConfigureDependencies

        /// <summary>
        /// Configures the dependencies for the test case.
        /// </summary>
        /// <param name="container">The container (private to the test case).</param>
        protected override void ConfigureDependencies(DependencyInjectionContainer container)
        {
            base.ConfigureDependencies(container);
#if WINDOWS_PHONE
            container.Register<IAuthenticationProvider, AnonymousAuthenticationProvider>();
#endif
            AstoriaTestServices.ConfigureDependencies(container);
        }
开发者ID:larsenjo,项目名称:odata.net,代码行数:12,代码来源:AstoriaTestCaseBase.cs


示例9: ImportIInjectionScopeDiagnostic

		public void ImportIInjectionScopeDiagnostic()
		{
			DependencyInjectionContainer container = new DependencyInjectionContainer();

			InjectionScopeDiagnostic diag = container.Locate<InjectionScopeDiagnostic>();

			Assert.Equal(0, diag.PossibleMissingDependencies.Count());
		}
开发者ID:jrjohn,项目名称:Grace,代码行数:8,代码来源:IInjectionScopeDiagnosticTests.cs


示例10: GraceServiceLocator

 public GraceServiceLocator(DependencyInjectionContainer container)
 {
     if (container == null)
     {
         throw new ArgumentNullException("container");
     }
     this.container = container;
 }
开发者ID:ricardoshimoda,项目名称:Grace,代码行数:8,代码来源:GraceServiceLocator.cs


示例11: InjectionKernelManager

        /// <summary>
        /// Default constructor
        /// </summary>
        /// <param name="container">container for the kernel manager</param>
        /// <param name="comparer">used to compare to export strategies for which one should be used</param>
        /// <param name="blackList">export strategy black list</param>
        public InjectionKernelManager(DependencyInjectionContainer container,
			ExportStrategyComparer comparer,
			BlackList blackList)
        {
            Container = container;

            this.comparer = comparer;
            this.blackList = blackList;
        }
开发者ID:jrjohn,项目名称:Grace,代码行数:15,代码来源:InjectionKernelManager.cs


示例12: ConfigureWithXmlModulePropertySetTest

        public void ConfigureWithXmlModulePropertySetTest()
        {
            DependencyInjectionContainer container = new DependencyInjectionContainer();

            container.ConfigureWithXml();

            int intProperty = (int)container.Locate("IntProperty");

            Assert.Equal(5, intProperty);
        }
开发者ID:jrjohn,项目名称:Grace,代码行数:10,代码来源:AppConfigTests.cs


示例13: ConfigureWithXmlModuleTest

        public void ConfigureWithXmlModuleTest()
        {
            DependencyInjectionContainer container = new DependencyInjectionContainer();

            container.ConfigureWithXml();

            IBasicService basicService = container.Locate<IBasicService>();

            Assert.NotNull(basicService);
        }
开发者ID:jrjohn,项目名称:Grace,代码行数:10,代码来源:AppConfigTests.cs


示例14: AutoCreateTest

        public void AutoCreateTest()
        {
            DependencyInjectionContainer container = new DependencyInjectionContainer();

            container.Configure(c => c.Export<BasicService>().As<IBasicService>());

            ImportConstructorService constructorService = container.Locate<ImportConstructorService>();

            Assert.NotNull(constructorService);
        }
开发者ID:ricardoshimoda,项目名称:Grace,代码行数:10,代码来源:BasicContainerTests.cs


示例15: ConfigureWithXmlExportTest

        public void ConfigureWithXmlExportTest()
        {
            DependencyInjectionContainer container = new DependencyInjectionContainer();

            container.ConfigureWithXml();

            IConstructorImportService importService = container.Locate<IConstructorImportService>();

            Assert.NotNull(importService);
        }
开发者ID:jrjohn,项目名称:Grace,代码行数:10,代码来源:AppConfigTests.cs


示例16: InEnvironment

        public void InEnvironment()
        {
            DependencyInjectionContainer container = new DependencyInjectionContainer(ExportEnvironment.UnitTest);

            container.Configure(c => c.ExportAssembly(GetType().Assembly).InEnvironment(ExportEnvironment.RunTimeOnly));

            IEnumerable<ISimpleObject> simpleObjects = container.LocateAll<ISimpleObject>();

            Assert.NotNull(simpleObjects);
            Assert.Equal(0, simpleObjects.Count());
        }
开发者ID:ricardoshimoda,项目名称:Grace,代码行数:11,代码来源:ExportAssemblyConfigurationTests.cs


示例17: CreateDIContainer

        private static IDependencyInjectionContainer CreateDIContainer()
        {
            var container = new DependencyInjectionContainer { ThrowExceptions = false };

            container.Configure(
                registration => registration
                    .Export<PeopleService>()
                    .As<IPeopleService>());

            return container;
        }
开发者ID:modulexcite,项目名称:OmniXAML,代码行数:11,代码来源:App.xaml.cs


示例18: DebugConsoleLog

        public void DebugConsoleLog()
        {
            Logger.SetLogService(new DebugConsoleLogService());

            DependencyInjectionContainer container = new DependencyInjectionContainer();

            container.Configure(c => c.Export<BasicService>().As<IBasicService>());

            IBasicService basicService = container.Locate<IBasicService>();

            Assert.NotNull(basicService);
        }
开发者ID:jrjohn,项目名称:Grace,代码行数:12,代码来源:DebugConsoleLogServiceTests.cs


示例19: SelectTypes

        public void SelectTypes()
        {
            DependencyInjectionContainer container = new DependencyInjectionContainer();

            container.Configure(
                c => c.ExportAssembly(GetType().Assembly).ByInterface(typeof(ISimpleObject)).Select(TypesThat.EndWith("C")));

            IEnumerable<ISimpleObject> simpleObjects = container.LocateAll<ISimpleObject>();

            Assert.NotNull(simpleObjects);
            Assert.Equal(1, simpleObjects.Count());
        }
开发者ID:ricardoshimoda,项目名称:Grace,代码行数:12,代码来源:ExportAssemblyConfigurationTests.cs


示例20: ConfigureWithXmlThirdSectionWithShortNames

        public void ConfigureWithXmlThirdSectionWithShortNames()
        {
            DependencyInjectionContainer container = new DependencyInjectionContainer();

            container.ConfigureWithXml("thirdGrace");

            IConstructorImportService importService = container.Locate<IConstructorImportService>();

            Assert.NotNull(importService);

            Assert.Equal(5, container.Locate("IntProperty"));
        }
开发者ID:jrjohn,项目名称:Grace,代码行数:12,代码来源:AppConfigTests.cs



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# DependencyObject类代码示例发布时间:2022-05-24
下一篇:
C# Dependency类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap